# Fraley, Oct 2003
# Exercise for R/S+ on confidence intervals around
correlations
# www.uic.edu/~fraley/classes/
# never fixed
# Initialize some variables. These can be changed.
n.studies<-20 # Number of samples to draw from population
n<-50 #
Number of people in each sample
pop.cor<-.5 # Correlation in the population (rho)
z.crit<-1.96 # Critical value for 95% of normal
distribution
# Set up some vectors for saving information
x.cor<-1:n.studies
ci.vec<-1:n.studies
# Begin loop
# In this loop, a number of samples of size n will
# be drawn from the same population. The program
# will calculate the sample mean (and save it as the
ith element
# of x.mean) and half the width of the confidence
interval
for(i in 1:n.studies){
x1<-rnorm(n)
x2<-pop.cor*x1
+ rnorm(n,0,sqrt(1-pop.cor^2))
x.cor[i]<-cor(x1,x2)
se<-(1 -
x.cor[i]^2)/sqrt(n - 0)
ci.vec[i]<-se*z.crit
}
# Plot results
# Plots the 95% CI for each study. A solid line
# demarcates the true parameter
plot(1:n.studies,x.cor,ylim=c(min(x.cor)-max(ci.vec),max(x.cor)+max(ci.vec)
))
abline(h=pop.cor,col=6)
for(i in 1:n.studies){
lines(c(i,i),c((x.cor[i]-ci.vec[i]),(x.cor[i]+ci.vec[i]))
)
}