Skip to content

Instantly share code, notes, and snippets.

@AkselA
Last active August 21, 2018 09:45
Show Gist options
  • Save AkselA/83c1ae36fed815fd0cab6e63070b432d to your computer and use it in GitHub Desktop.
Save AkselA/83c1ae36fed815fd0cab6e63070b432d to your computer and use it in GitHub Desktop.
library(caret)
set.seed(1)
val <- runif(200)
# val <- rnorm(200)
k <- 35
length(val)/k
# 5.714286
ll <- list()
ngroups <- 1:15
for (i in ngroups) {
lli <- replicate(200, {
if (i > 1) {
gr <- cut(val, i)
} else {
gr <- val
}
fold <- createFolds(gr, k, list=FALSE)
a <- aggregate(val, list(fold), mean)[,2]
round(diff(range(a)), 4)
})
ll[[i]] <- lli
}
ll.mat <- simplify2array(ll)
par(mar=c(3, 3, 1, 1), mgp=c(1.8, 0.6, 0), xaxs="i", family="PT Sans")
matplot(ngroups, t(ll.mat), type="l", lty=1, lwd=0.2, col="#00000044",
xaxt="n", xlab="Number of strata", ylab="Max absolute difference")
lines(ngroups, apply(ll.mat, 2, mean), col="#2078FA", lwd=4)
axis(1, at=ngroups)
legend("topleft", legend="Mean", bty="n", lwd=4, col="#2078FA")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment