Zeta10
Zeta10

Reputation: 113

R - Loop Need Assistance with writing a loop and storing the results in a data frame in R

I have an enormous data set. I want to sample it K times, run a linear regression and extract the RMSE each time to store in a data frame.

pseudo code: rmse <- emptyDataFrame{}

for (i in 1:100)

sample_n(df, n, replace=True)

model <- lm(y ~ ., data = df)

rmse <- sqrt(mean(y_pred - y)^2))

Can anyone give me the missing details?

Upvotes: 0

Views: 37

Answers (1)

Ricardo Semi&#227;o
Ricardo Semi&#227;o

Reputation: 4456

You can add a [i] after rmse to store the values in different indexes. Also, i don't know how your function sample_n works, but perhaps you need to save its output in a new variable to pass it to lm.

Also, the formula for RMSE is sqrt(mean((y_pred - y)^2)).

rmse <- c()

for (i in 1:100){
  df_sampled <- sample_n(df, n, replace=True) 
  
  model <- lm(y ~ ., data = df_sampled) 
  
  rmse[i] <- sqrt(mean((y_pred  -  y)^2))
}

as.data.frame(rmse)

Upvotes: 1

Related Questions