For Loop Help In RStudio

I have these regressions being run 1000 times

  • y ~ x1
  • y ~ x1 + x2
  • y ~ x1 + x2 + x3
  • y ~ x1 + x2 + x3 + x4
  • y ~ x1 + x2 + x3 + x4 + x5
  • y ~ x1 + x2 + x3 + x4 + x5 + x6
  • y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7
  • y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8
  • y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9

I have the for loop calculating the RMSE for all of them which turns into 9 vectors with 1000 rows. How do I tell R to tell me which regression has the lowest RMSE for each simulation?

In doing stepwise addition of covariates like this it helps to write a function such as

do_regr <- function(xs) {lm(y ~ xs, data = my_data}

then create a list of vectors, l

v1 <- `x1`
...
v9 <- c(`x1` ... `x9`)

then use purrr:map to apply do_regr over the vector list into a list of lists and sweep that with another function with

sqrt(mean(res$residuals^2))
1 Like
summary(t(as.matrix(mtcars)))[1,]
#>           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
#>  "Min.   :  0.00  " "Min.   :  0.000  "  "Min.   :  1.00  " "Min.   :  0.000  " 
#>   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
#>  "Min.   :  0.00  "  "Min.   :  0.00  " "Min.   :  0.000  " "Min.   :  0.000  " 
#>            Merc 230            Merc 280           Merc 280C          Merc 450SE 
#> "Min.   :  0.000  "  "Min.   :  0.00  "  "Min.   :  0.00  "  "Min.   :  0.00  " 
#>          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
#>  "Min.   :  0.00  "  "Min.   :  0.00  " "Min.   :  0.000  " "Min.   :  0.000  " 
#>   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
#> "Min.   :  0.000  "   "Min.   : 1.00  "  "Min.   : 1.000  "  "Min.   : 1.000  " 
#>       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
#> "Min.   :  0.000  "  "Min.   :  0.00  " "Min.   :  0.000  " "Min.   :  0.000  " 
#>    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
#> "Min.   :  0.000  "  "Min.   : 1.000  "  "Min.   :  0.00  " "Min.   :  1.000  " 
#>      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
#> "Min.   :  0.000  " "Min.   :  0.000  " "Min.   :  0.000  "  "Min.   :  1.00  "

Created on 2020-03-23 by the reprex package (v0.3.0)

Is there any way i can make this easier for you?

That's something we all experience. The best way to get help getting through it is to set up the problem with limited data sets, fewer variables and fewer simulations. The next step is to create the proper objects, the a workflow

I have been doing that but I've just been stuck on getting the index number of the column with the minimum value :face_vomiting:

have you tried adapting

summary(t(as.matrix(mtcars)))[1,]

to your data?

yes, ive done something like this however it just gives the values of the minimum not value of the column it is in.

apply(train_selections, 1, FUN=min) this gives me the same thing. (Sorry I wasn't clear though I am going for minimums not maximums)

train_selections is my data.frame

That doesnt work though

library(tidyverse)
library(purrr)
set.seed(42)
df <- tibble( a = rnorm(12),
              b = rnorm(12),
              c = rnorm(12)
              )


#get miniminum values of the columns
varnames <- colnames(df)
min_vals_each_row <- df %>% mutate(minval = pmin(!!!syms(varnames))) 
min_vals_each_row$minorigin <- map_int(  1:nrow(min_vals_each_row),
                                        # 
# > min_vals_each_row
# # A tibble: 12 x 5
# a      b      c  minval minorigin
# <dbl>  <dbl>  <dbl>   <dbl>     <int>
# 1  1.37   -1.39   1.90  -1.39           2
# 2 -0.565  -0.279 -0.430 -0.565          1
# 3  0.363  -0.133 -0.257 -0.257          3
# 4  0.633   0.636 -1.76  -1.76           3
# 5  0.404  -0.284  0.460 -0.284          2
# 6 -0.106  -2.66  -0.640 -2.66           2
# 7  1.51   -2.44   0.455 -2.44           2
# 8 -0.0947  1.32   0.705 -0.0947         1
# 9  2.02   -0.307  1.04  -0.307          2
# 10 -0.0627 -1.78  -0.609 -1.78          2
# 11  1.30   -0.172  0.505 -0.172         2
# 12  2.29    1.21  -1.72  -1.72          3
1 Like

Im so sorry to be annoying but @nirgrahamuk my professor won't have those packages when grading our homework :(. is there another way using just R to do this?

i tried writing an else if tree

if (train_selections_min[i] == train_rmse_x1_1_sigma_1[i]){

train_lowest[i] = 1

} else if (train_selections_min[i] == train_rmse_x1_2_sigma_1[i]){

train_lowest[i] = 2

} else if (train_selections_min[i] == train_rmse_x1_3_sigma_1[i]){

train_lowest[i] = 3

} else if (train_selections_min[i] == train_rmse_x1_4_sigma_1[i]){

train_lowest[i] = 4

} else if (train_selections_min[i] == train_rmse_x1_5_sigma_1[i]){

train_lowest[i] = 5

} else if (train_selections_min[i] == train_rmse_x1_6_sigma_1[i]){

train_lowest[i] = 6

} else if (train_selections_min[i] == train_rmse_x1_7_sigma_1[i]){

train_lowest[i] = 7

} else if (train_selections_min[i] == train_rmse_x1_8_sigma_1[i]){

train_lowest[i] = 8

} else if (train_selections_min[i] == train_rmse_x1_9_sigma_1[i]){

train_lowest[i] = 9

}
where train_selections_min holds the minimum for each row and train_rmse_x1_x_sigma_1 is the value its checking against to see if its equal. It goes from side to side scanning but for some reason this doesnt work either

train_lowest is supposed to be a storage vector for the value of each column

So, I got the else if tree to work now when I try and input the else if results into train_lowest[i], nothing happens. What is wrong here?

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

Another question, how to i output the column number of the minimum value of each row in a data frame?

I tried using which.min(apply(train_selections,MARGIN=2,min)) but this just seems to give me the first column. I want the one with the min value and it to give it to me as the index number not the column name.

Time for a reprex, I think

Im sorry, I'm just stuck on this. Ive been working on it for so long and I can't figure out a way to do it :frowning:

1 Like