Skip to content

Instantly share code, notes, and snippets.

@mikelove
Last active October 9, 2015 14:53
Show Gist options
  • Save mikelove/0fb34f56ac051ea2076b to your computer and use it in GitHub Desktop.
Save mikelove/0fb34f56ac051ea2076b to your computer and use it in GitHub Desktop.
d <- expand.grid(n=c(3,5,10,20), type=c("normal","t"))
d <- d[rep(seq_len(nrow(d)),each=nrep),]
simulate <- function(n, type) {
if (type == "normal") {
dat <- rnorm(n)
} else {
dat <- rt(n, df=3)
}
dat
meanerr <- abs(mean(dat) - 0)
medianerr <- abs(median(dat) - 0)
c(mean=meanerr, median=medianerr)
}
library(plyr)
# mdply does everything: takes columns of d as multivariate input
# to simulate() and then adds the output as columns to d
d2 <- mdply(d, simulate)
d2$n <- factor(d2$n)
library(reshape)
m <- melt(d2, id.vars=c("n","type"))
library(ggplot2)
ggplot(m, aes(x=n, y=value, col=variable)) + geom_boxplot() +
facet_wrap(~ type) + ylim(0,2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment