Last active
August 21, 2018 09:45
-
-
Save AkselA/83c1ae36fed815fd0cab6e63070b432d to your computer and use it in GitHub Desktop.
Example code for https://aksela.wordpress.com/2018/07/17/stratified-partitioning-uniform-and-normal-test/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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