Loop for to record results in a tibble


I'm trying to build a code to record results from ANN's training in a tibble, but the code needs to loop over 3 different levels (4 networks, with 5 repetitions and 20 lines of the tibble for record).
Each network repetition corresponds to one tibble line (4x5)

I've been trying this for hours and it still didn't work.
Can someone help me please!

Here is the code:

for(k in 1:K) { (20 tibble lines)
for(i in 1:N) {4 networks)
for(j in 1:J) { (5 networks repetitions)


train_i <- redes[[i]]

Network prediction

Test result

output_j = compute(train_i, test, rep = j)

Train Result

train_j = compute(train_i, train, rep = j)



vr_j <- cor(x = test[,"biom_stem"], y = output_j$net.result)
vrmse_j <- Metrics::rmse(test$biom_stem, output_j$net.result)
vmae_j <- Metrics::mae(test$biom_stem, output_j$net.result)


tr_j <- cor(x = train[,"biom_stem"], y = train_j$net.result)
trmse_j <- Metrics::rmse(train$biom_stem, train_j$net.result)
tmae_j <- Metrics::mae(train$biom_stem, train_j$net.result)


results_ws[k, "Net"] <- j
results_ws[k, "r_validaĆ§Ć£o"] <- vr_j
results_ws[k, "RMSE_validaĆ§Ć£o"] <- vrmse_j
results_ws[k, "EMA_validaĆ§Ć£o"] <- vmae_j
results_ws[k, "r_treino"] <- tr_j
results_ws[k, "RMSE_treino"] <- trmse_j
results_ws[k, "EMA_treino"] <- tmae_j

Without a minimal working example see the FAQ, more concrete guidance would require reverse engineering the problem, so I'll offer some general guidance.

Every problem in R can be approached with advantage with school algebra in mind: f(x) = y, where the three objects, f, x, y (in R everything is an object) are

x what is at hand
y what is desired
f a function to convert x to y

Each of these objects may be, and in the case of this problem are, composite, consisting of other objects. In particular, f will be analogous to f(g(x).

For the problem

x the [unknown for purposes of the question] object [redes] on 20 tibbles for each of 4 networks [of unknown object type] repeated over 5 network objects
y is a table of results
g performs an operation [unknown for the purposes of this question] on each network using some or all parts of each tibble that extracts the results desired
f collects the return values of g and records them in y.

While it is possible to do this with the for function, procedural/imperative programming style is not R's long suit. For this type of problem, apply, sapply, lapply or its purrr front ends in its map family are preferred.

To proceed without a reprex, compose f and g to deal with the single case of one repetition of one network on one tibble. By running the ANN-related tests and placing them in the receiver object y.

Recall that, since functions are first-class objects, they may be given as arguments to other functions. This has two applications: first, h = f(g(x); second m(h(z)$ will operate over k_i ...k_n, j_i ... k_n where z is some object that encapsulates the indices i and j.

1 Like

Thanks for the help! I've tried lapply, it didn't work, but helped me to think in one solution.
The results is record in a list of tibbles, and I eliminate de k element to just bind the list outside the loop for. :smile:

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.