How to simulate significant data

153 views Asked by At

I would like to simulate significant distributions in R: For that I would like to specify a common M and SD (Group 1 in combined with Group 2), sample size and the effect size (Cohen's d) between two groups and get data that gets significant with a t-test when the effect size is sufficient. Can someone give me some advice on how to do this?

1

There are 1 answers

5
Ben Bolker On BEST ANSWER

We'll going to assume the same SD in each group and equal sample sizes (if you want to relax these assumptions you'll have to do the algebra yourself!), so pooled SD=within-group SD. Cohen's D is (M2-M1)/SD, or (delta_M/SD), so delta_M = SD*Cohen_D. M1=M-delta_M/2, M2=M+delta_M/2.

M <- 2         # grand mean
C <- 2         # Cohen's D
S <- 1         # pooled SD
delta_M <- S*C
m <- c(M-delta_M/2,M+delta_M/2)     # specify means for each group
n <- c(20,20)   # sample size for each group
set.seed(101)
## generate Normal deviates for each group
grp1 <- rnorm(n[1],mean=m[1],sd=S)
grp2 <- rnorm(n[2],mean=m[2],sd=S)
## run t-test
t.test(grp1,grp2)

    Welch Two Sample t-test

data:  grp1 and grp2
t = -7.1805, df = 37.503, p-value = 1.511e-08
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -2.682429 -1.502160
sample estimates:
mean of x mean of y 
0.9026826 2.9949771