# Confidence interval for Cohen's d
# R. Chris Fraley | Oct 27, 2003
# This program computes an approximation to the 95%
confidence interval
# for Cohen's d
# Because S-Plus/R don't have an easy to way to
generate
# non-central t distributions, I have simulated a
sampling distribution
# for d based on hypothesized group means and
SDs. Assuming
# backwards inference is valid, the sample means and
SDs can be used
# as proxies and the resulting sampling distribution
# can be used to estimate the CI.
# -- User provided parameters ---
m1 <- 10 #
hypothesized mean group 1
m2 <- 18 #
hypothesized mean group 2
n1 <- 30 #
sample size group 1
n2 <- 30 #
sample size group 2
sp <- 3 #
hypothesized sd for both groups
trials <- 1000 # number of simulated trials
# ----- Program does the rest ------------
cohend<-rep(9999,trials)
xdiff<-rep(9999,trials)
pVec<-rep(9999,trials)
# -------------
# loop through sampling process
for(i in 1:trials){
x1<-rnorm(n1,m1,sp)
x2<-rnorm(n2,m2,sp)
xdiff[i]
<- mean(x2)-mean(x1)
spo <-
sqrt(mean(var(x1),var(x2)))
cohend[i]
<- xdiff[i]/spo
pVec[i]<-t.test(x1,x2)$p.value
}
# -----------------------
# compute CI's as lower 5% and upper 95% values of d
ciL <- sort(cohend)[round(.05*trials)]
ciU <- sort(cohend)[round(.95*trials)]
z<-table(pVec<=.05)
if(dim(z)>1){
spower<-z[2]/trials
} else{
spower<-1
}
# ----------------------------
# plot and summarize results
hist(cohend,xlab="Cohen's
d",main="Sampling distribution of Cohen's d")
cat("average cohen d
=",round(mean(cohend),3),"\n")
cat("average mean difference
=",round(mean(xdiff),3),"\n")
cat("Approximate lower bound 95% CI for Cohen's
d",round(ciL,3),"\n")
cat("Approximate upper bound 95% CI for Cohen's
d",round(ciU,3),"\n")
cat("Approximate
power",spower,"\n")