Skip to content

Instantly share code, notes, and snippets.

@jcreinhold
Last active April 13, 2022 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcreinhold/30e1a689d55eef35f728b4f5d7490a24 to your computer and use it in GitHub Desktop.
Save jcreinhold/30e1a689d55eef35f728b4f5d7490a24 to your computer and use it in GitHub Desktop.
Explore the distribution of p-values
---
title: "On the distribution of p-values"
output: html_notebook
---
```{r}
sample.sizes <- seq(5, 100, 10)
num.trials <- 1000
num.experiments <- 1
null.mu <- 0.0
alternative.mu <- 0.1
sig.level <- 0.05
p.values <- array(data=NA, dim=num.trials)
for (n in sample.sizes) {
for (i in 1:num.trials) {
p.values.all.experiments <- array(data=NA, dim=num.experiments)
for (j in 1:num.experiments) {
x <- rnorm(n, mean=alternative.mu)
p.values.all.experiments[j] <- t.test(
x, alternative = "greater", var.equal=TRUE
)$p.value
}
p.values[i] <- min(p.values.all.experiments)
}
mc.prob.reject <- mean(p.values < sig.level)
power.results <- power.t.test(
n = n,
delta = alternative.mu - null.mu,
sig.level = sig.level,
type = "one.sample",
alternative = "one.sided"
)
title <- paste(
"Min. p-values for sample size =", n, "over", num.experiments, "experiments",
"\nPower =", power.results$power, "; MC prob. reject =", mc.prob.reject
)
hist(p.values, main=title)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment