-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCI_width_by_sample_size.R
66 lines (50 loc) · 1.92 KB
/
CI_width_by_sample_size.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#CI for Cohen's d by sample size in independent samples and paired samples designs
#
#Author: Eric Fields
#Version Date: 22 February 2025
#Copyright (c) 2025, Eric Fields
#This code is free and open source software made available under the terms
#of the CC BY 4.0 license
#https://creativecommons.org/licenses/by/4.0/
library(MBESS)
proj_dir <- "C:/Users/fieldsec/OneDrive - Westminster College/Documents/ECF/Research/Bayes factor simulations"
setwd(proj_dir)
d.indsample.CI <- function(d, n1, n2, conf.level=0.95) {
#Calculate the confidence interval for Cohen's d for an independent samples design
#Formulas adapted from: https://effect-size-calculator.herokuapp.com/
se <- sqrt(1/n1 + 1/n2)
t <- d / se
df <- n1 + n2 -2
tCI <- conf.limits.nct(t, df, conf.level = conf.level)
dL <- tCI$Lower.Limit * se
dU <- tCI$Upper.Limit * se
return(c(dL, dU))
}
d.pairedsample.CI <- function(dav, N, r, conf.level=0.95) {
#Calculate the confidence interval for Cohen's d for a paired samples design
#Formulas adapted from: https://effect-size-calculator.herokuapp.com/
se <- sqrt( (2*(2 - 2*r)) / (N*2) )
t <- dav / se
df <- N - 1
tCI <- conf.limits.nct(t, df, conf.level=conf.level)
davL <- tCI$Lower.Limit * se
davU <- tCI$Upper.Limit * se
return(c(davL, davU))
}
#Sample sizes
ns <- c(20, 30, 40, 50, 75, 100, 200, 300, 400, 500, 750, 1000, 2000, 5000, 10000)
#Initialize data frame
CI_table <- data.frame()
#Calculate CIs for each sample size
for (n in ns) {
CI <- d.indsample.CI(0.0, n, n)
CI_table[sprintf("%d", n), "independent"] <- CI[2]
CI <- d.pairedsample.CI(0.0, n, 0.25)
CI_table[sprintf("%d", n), "paired (r=0.2)"] <- CI[2]
CI <- d.pairedsample.CI(0.0, n, 0.5)
CI_table[sprintf("%d", n), "paired (r=0.5)"] <- CI[2]
CI <- d.pairedsample.CI(0.0, n, 0.75)
CI_table[sprintf("%d", n), "paired (r=0.8)"] <- CI[2]
}
#Output results
write.csv(CI_table, file.path("output", "CI_width.csv"))