Skip to content

Instantly share code, notes, and snippets.

@mittenchops
Last active December 16, 2015 01:28
Show Gist options
  • Save mittenchops/5354856 to your computer and use it in GitHub Desktop.
Save mittenchops/5354856 to your computer and use it in GitHub Desktop.
Found on SO somewhere, most useful testing function for sampling I've ever seen
f_K_fold <- function(Nobs,K=5){
rs <- runif(Nobs)
id <- seq(Nobs)[order(rs)]
k <- as.integer(Nobs*seq(1,K-1)/K)
k <- matrix(c(0,rep(k,each=2),Nobs),ncol=2,byrow=TRUE)
k[,1] <- k[,1]+1
l <- lapply(seq.int(K),function(x,k,d)
list(train=d[!(seq(d) %in% seq(k[x,1],k[x,2]))],
test=d[seq(k[x,1],k[x,2])]),k=k,d=id)
return(l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment