I am working in R. I am doing some simulations of sample sizes by drawing random samples from two normal distributions.
I am calculating Cohen's $d$ effect size with:
library(esc)
esc_mean_sd(grp1m = ..., grp1sd = ..., grp1n = ..., # mean, sd, n of group CONTROL
grp2m = ..., grp2sd = ..., grp2n = ..., # mean, sd, n of group TEST
es.type = "d")
Note: from package documentation https://cran.r-project.org/web/packages/esc/esc.pdf: $d$ is standardized mean difference effect size $d$. From the function's source code: (grp1m - grp2m)/sd_pooled
.
Then I am calculating sample size with:
library(pwr)
pwr.t.test(n=NULL, d=..., sig.level=0.05, power=0.8)
It happens that I get the following error:
Error in uniroot(function(n) eval(p.body) - power, c(2 + 1e-10, 1e+09)) : f() values at end points not of opposite sign
By trial and error, I found it throws the error when $d > 5.65348...$ Anything below is fine. I also noticed sample size at this effect size (and sig.level=0.05
, power=0.8
) is exactly $2$.
My current solution is to artificially bring down to $5.6$ any effect size higher than that, and report $2$ as sample size. Is $2$ the theoretical smallest sample size anyway? Is this an accurate fix?
Reproducible example
library(esc)
library(pwr)
Case1: $d=2$: does not throw an error
ESC <- esc_mean_sd(grp1m = 3, grp1sd = 1, grp1n = 20,
grp2m = 1, grp2sd = 1, grp2n = 20,
es.type = "d")
pwr.t.test(n=NULL, d=ESC$es, sig.level=0.05, power=0.8)
Case2: $d=6$: throws an error
ESC <- esc_mean_sd(grp1m = 7, grp1sd = 1, grp1n = 20,
grp2m = 1, grp2sd = 1, grp2n = 20,
es.type = "d")
pwr.t.test(n=NULL, d=ESC$es, sig.level=0.05, power=0.8)