Skip to content

Commit

Permalink
Revised Article 5 code
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmyleswhite committed Jul 17, 2012
1 parent 58b2118 commit 790022b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Binary file added article5/cache.Rdata
Binary file not shown.
51 changes: 51 additions & 0 deletions article5/monte_carlo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Assume mean of Group 1 known to be 0
# Therefore we only measure Group 2 data
# Let eps = true difference in means
# Let sigma = true variance of data
# We wish to find smallest sample size, N, such that
# E[p] < 0.05

find.minimum.n <- function(eps, sigma, n.sims = 10000)
{
n <- 2

n.too.small <- TRUE

while (n.too.small)
{
p <- rep(NA, n.sims)

for (i in 1:n.sims)
{
x <- rnorm(n, eps, sigma)
p[i] <- t.test(x)$p.value
}

if (mean(p) < 0.05)
{
n.too.small <- FALSE
}
else
{
n <- n + 1
}
}

return(n)
}

set.seed(1)
epsilons <- 10 ^ seq(-1, 1, by = 0.01)

df <- data.frame(Effect = epsilons, N = NA)

for (i in 1:nrow(df))
{
eps <- df$Effect[i]
df$N[i] <- find.minimum.n(eps, 1, 2500)
}

ggplot(df, aes(x = Effect, y = N)) + geom_line() + scale_x_log10() + opts(title = "Minimum Sample Size Required to Pass p < .05 NHST in Expectation") + xlab("Effect Size") + ylab("Minimum N")
ggsave("article5/sample_size.png")

save(df, file = "cache.Rdata")
Binary file modified article5/sample_size.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 790022b

Please sign in to comment.