Skip to content

Instantly share code, notes, and snippets.

@Lakens
Created September 22, 2014 19:56
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 Lakens/26a27f4b146489a85bb2 to your computer and use it in GitHub Desktop.
Save Lakens/26a27f4b146489a85bb2 to your computer and use it in GitHub Desktop.
Simulate Z-scores
#####SIMULATE & PLOT Z-SCORES#########
nSims <- 100000 #number of simulated experiments
p <-numeric(nSims) #set up empty container for all simulated p-values
z <-numeric(nSims) #set up empty container for z-scores
for(i in 1:nSims){ #for each simulated experiment
x<-rnorm(n = 23, mean = 100, sd = 20) #produce simulated participants
y<-rnorm(n = 23, mean = 110, sd = 20) #produce simulated participants
t<-t.test(x,y) #perform the t-test
p[i]<-t$p.value #get the p-value and store it
z[i]<-qnorm(1-(t$p.value/2)) #convert to the z-score and store it
}
#now plot the histogram NOTE if you get error some 'x' not counted; maybe 'breaks' do not span range of 'x' increase max scale end from 8 to higher value
hist(p, main="Histogram of p-values", xlab=("Observed p-value"), breaks =20)
hist(z, main="Histogram of z-scores", xlab=("Observed z-score"), seq(0,8,by=0.245))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment