Skip to content

Instantly share code, notes, and snippets.

@tmalsburg
Last active October 14, 2023 07:48
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 tmalsburg/3d8f71deadd2cdfb8baa179aeffb155e to your computer and use it in GitHub Desktop.
Save tmalsburg/3d8f71deadd2cdfb8baa179aeffb155e to your computer and use it in GitHub Desktop.
Quick simulation of 50 experiments and their means and 95% confidence intervals
set.seed(1)
mu <- 0
sigma <- 1
N.trials <- 20
N.sims <- 50
rnorm(N.trials*N.sims, mu, sigma) |>
matrix(nrow=N.sims) -> Y
d <- apply(Y, 1, \(y) c(mean(y), sd(y)/sqrt(length(y))))
plot(d[1,], pch=19, ylim=c(-1,1),
col=ifelse(d[1,]+2*d[2,]<0 | d[1,]-2*d[2,]>0, "red", "black"),
xlab="Simulation number", ylab="DV")
arrows(1:N.sims, d[1,]+2*d[2,], 1:N.sims, d[1,]-2*d[2,],
col=ifelse(d[1,]+2*d[2,]<0 | d[1,]-2*d[2,]>0, "red", "black"),
code=0, lwd=2)
abline(h=0, lty=2, col="red", lwd=3)
@tmalsburg
Copy link
Author

plot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment