Skip to content

Instantly share code, notes, and snippets.

@nonsleepr
Last active August 29, 2015 14:06
Show Gist options
  • Save nonsleepr/ab5c72924768a286f9d4 to your computer and use it in GitHub Desktop.
Save nonsleepr/ab5c72924768a286f9d4 to your computer and use it in GitHub Desktop.
Implementation of K-fold cross validation
require(plyr)
kfold <- function(df, k, train.f, predict.f) {
progress.bar <- create_progress_bar("text")
progress.bar$init(k)
N <- nrow(df)
subsample.id <- sample(1:k, N, replace = TRUE)
r <- vector(length = N)
for (i in 1:k) {
m <- train.f(df[subsample.id != i,])
r[subsample.id == i] <- predict.f(m, df[subsample.id == i,])
progress.bar$step()
}
return(r)
}
resp <- kfold(df.train, 5,
function(d) {
cforest(fml, data = d, controls=cforest_control(ntree=1000, mtry=5, mincriterion = qnorm(0.95)))
},
function(m, d) {
predict(m, d, OOB=TRUE, type = "response")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment