Vectorizing a for-loop for cross-validation in R
0
I'm trying to speed up a script that otherwise takes days to handle larger data sets. So, is there a way to completely vectorize the following script: # k-fold cross validation df <- trees # a data frame 'trees' from R. df <- df[sample(nrow(df)), ] # randomly shuffles the data. k <- 10 # Number of folds. Note k=nrow(df) in the leave-one-out cross validation. folds <- cut(seq(from=1, to=nrow(df)), breaks=k, labels=FALSE) # creates unique numbers for k equally size folds. df$ID <- folds # adds fold IDs. df[paste("pred", 1:10, sep="")] <- NA # adds multiple columns "pred1"..."pred10" to speed up the following loop. library(mgcv) for(i in 1:k) { # looping for different models: