Skip to content

Instantly share code, notes, and snippets.

@mikelove
Last active October 9, 2015 14:54
Show Gist options
  • Save mikelove/5f6ecd6238f675f5b918 to your computer and use it in GitHub Desktop.
Save mikelove/5f6ecd6238f675f5b918 to your computer and use it in GitHub Desktop.
# the task here is just to compare the mean and median as estimators
# for the location of the distribution. I want to try 100 reps for different
# sample size, and also for different types of distribution (normal vs t)
nrep <- 100
d <- expand.grid(n=c(3,5,10,20), type=c("normal","t"))
d <- d[rep(seq_len(nrow(d)),each=nrep),]
res <- lapply(seq_len(nrow(d)), function(i) {
if (d$type[i] == "normal") {
dat <- rnorm(d$n[i])
} else {
dat <- rt(d$n[i], df=3)
}
meanerr <- abs(mean(dat) - 0)
medianerr <- abs(median(dat) - 0)
data.frame(n=d$n[i], type=d$type[i], mean=meanerr, median=medianerr)
})
res.df <- do.call(rbind, res)
library(reshape)
m <- melt(res.df, id.vars=c("n","type"))
m$n <- factor(m$n)
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