Power and precision planning

Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.

ciwidth

Calculate the anticipated full width of a mean interval

Stata

ciwidth onemean, sd(2) n(50)

R Package:stats

2*stats::qt(0.975,df=49)*2/sqrt(50)

Anticipated-width workflow substituting the design SD for the future sample SD; interval width is upper minus lower. Stata also supports probability-of-width planning, which is not implemented here.

Plan n for a target mean-interval width

Stata

ciwidth onemean, sd(2) width(1)

R Package:stats

ceiling(stats::uniroot(function(n) 2*stats::qt(0.975,n-1)*2/sqrt(n)-1,c(3,1000))$root)

Anticipated-width workflow substituting the design SD for the future sample SD; interval width is upper minus lower. Stata also supports probability-of-width planning, which is not implemented here.

Calculate a paired mean-difference interval width

Stata

ciwidth pairedmeans, sddiff(2) n(50)

R Package:stats

2*stats::qt(0.975,49)*2/sqrt(50)

Anticipated-width workflow substituting the design SD for the future sample SD; interval width is upper minus lower. Stata also supports probability-of-width planning, which is not implemented here.

Calculate an equal-variance difference-of-means interval width

Stata

ciwidth twomeans, sd(2) n1(40) n2(60)

R Package:stats

2*stats::qt(0.975,98)*2*sqrt(1/40+1/60)

Anticipated-width workflow substituting the design SD for the future sample SD; interval width is upper minus lower. Stata also supports probability-of-width planning, which is not implemented here.

power

Calculate power for a fixed-size onemean t test

Stata

power onemean 0 0.5, sd(1) n(40)

R Package:stats

stats::power.t.test(n=40,delta=0.5,sd=1,type="one.sample",sig.level=0.05)

Differs from Stata Computes the displayed normal-theory t-test or two-proportion power design. R two-sample n is per group, whereas Stata n() is total; alternative, allocation and strict two-sided rejection conventions must match.

Plan the sample size for a onemean t test

Stata

power onemean 0 0.5, sd(1) power(0.8)

R Package:stats

stats::power.t.test(delta=0.5,sd=1,type="one.sample",sig.level=0.05,power=0.8)

Differs from Stata R reports n per group for an independent two-sample test; round sample sizes upward.

Find the detectable difference for a onemean t test

Stata

power onemean 0, sd(1) n(40) power(0.8)

R Package:stats

stats::power.t.test(n=40,delta=NULL,sd=1,power=0.8,type="one.sample")

Differs from Stata R returns a positive detectable difference; add it to the null mean to recover the alternative mean.

Calculate power for a fixed-size pairedmeans t test

Stata

power pairedmeans 0 0.5, sddiff(1) n(40)

R Package:stats

stats::power.t.test(n=40,delta=0.5,sd=1,type="paired",sig.level=0.05)

Differs from Stata Computes the displayed normal-theory t-test or two-proportion power design. R two-sample n is per group, whereas Stata n() is total; alternative, allocation and strict two-sided rejection conventions must match.

Plan the sample size for a pairedmeans t test

Stata

power pairedmeans 0 0.5, sddiff(1) power(0.8)

R Package:stats

stats::power.t.test(delta=0.5,sd=1,type="paired",sig.level=0.05,power=0.8)

Differs from Stata R reports n per group for an independent two-sample test; round sample sizes upward.

23 more examples and alternatives

Calculate correlation-test power at fixed n

Stata

power onecorrelation 0 0.3, n(100)

R Package:pwr

pwr::pwr.r.test(r=0.3,n=100,sig.level=0.05)

Differs from Stata pwr uses its documented approximation and effect-size parameterization; numerical results need not equal Stata.

Plan sample size for detecting a correlation

Stata

power onecorrelation 0 0.3, power(0.8)

R Package:pwr

pwr::pwr.r.test(r=0.3,power=0.8,sig.level=0.05)

Differs from Stata pwr uses its documented approximation and effect-size parameterization; numerical results need not equal Stata.

Find a detectable correlation at fixed n

Stata

power onecorrelation 0, n(100) power(0.8)

R Package:pwr

pwr::pwr.r.test(r=NULL,n=100,power=0.8,sig.level=0.05)

Differs from Stata pwr uses its documented approximation and effect-size parameterization; numerical results need not equal Stata.

Calculate one-mean z-test power with known SD

Stata

power onemean 0 0.5, sd(1) knownsd n(40)

R Package:pwr

pwr::pwr.norm.test(d=0.5,n=40,sig.level=0.05)

Differs from Stata pwr reports the mean difference in SD units; knownsd distinguishes this from a t test.

Plan a one-mean z-test sample size with known SD

Stata

power onemean 0 0.5, sd(1) knownsd power(0.8)

R Package:pwr

pwr::pwr.norm.test(d=0.5,power=0.8,sig.level=0.05)

Differs from Stata pwr reports the mean difference in SD units; knownsd distinguishes this from a t test.

Calculate one-sided one-mean t-test power

Stata

power onemean 0 0.5, sd(1) n(40) onesided

R Package:stats

stats::power.t.test(n=40,delta=0.5,sd=1,type="one.sample",alternative="one.sided")

Differs from Stata Computes the displayed normal-theory t-test or two-proportion power design. R two-sample n is per group, whereas Stata n() is total; alternative, allocation and strict two-sided rejection conventions must match.

Find a detectable one-mean z-test effect

Stata

power onemean 0, sd(1) knownsd n(40) power(0.8)

R Package:pwr

pwr::pwr.norm.test(d=NULL,n=40,power=0.8,sig.level=0.05)

Differs from Stata pwr reports the mean difference in SD units; knownsd distinguishes this from a t test.

Approximate one-proportion power for a fixed sample

Stata

power oneproportion 0.3 0.5, n(100)

R Package:pwr

pwr::pwr.p.test(h=pwr::ES.h(0.5,0.3),n=100,sig.level=0.05)

Uses the arcsine effect h, an alternative to Stata default score-test calculations.

Approximate a one-proportion design using Cohen h

Stata

power oneproportion 0.3 0.5, power(0.8)

R Package:pwr

pwr::pwr.p.test(h=pwr::ES.h(0.5,0.3),power=0.8,sig.level=0.05)

Uses the arcsine effect h, an alternative to Stata default score-test calculations.

Calculate balanced one-way ANOVA power

Stata

power oneway 0 0.5 1, varerror(1) n(90)

R Package:pwr

pwr::pwr.anova.test(k=3,n=30,f=sqrt(mean((c(0,0.5,1)-0.5)^2)))

Differs from Stata pwr uses its documented approximation and effect-size parameterization; numerical results need not equal Stata.

Plan balanced one-way ANOVA group size

Stata

power oneway 0 0.5 1, varerror(1) power(0.8)

R Package:pwr

pwr::pwr.anova.test(k=3,f=sqrt(mean((c(0,0.5,1)-0.5)^2)),power=0.8)

Differs from Stata pwr uses its documented approximation and effect-size parameterization; numerical results need not equal Stata.

Find the detectable difference for a pairedmeans t test

Stata

power pairedmeans 0, sddiff(1) n(40) power(0.8)

R Package:stats

stats::power.t.test(n=40,delta=NULL,sd=1,power=0.8,type="paired")

Differs from Stata R returns a positive detectable difference; add it to the null mean to recover the alternative mean.

Calculate power for an overall three-predictor R squared test

Stata

power rsquared 0.1, n(100) ntested(3)

R Package:pwr

pwr::pwr.f2.test(u=3,v=96,f2=0.1/(1-0.1))

Differs from Stata f2=(R2_full-R2_reduced)/(1-R2_full); residual df is N minus all predictors minus one.

Plan total n for an overall R squared test

Stata

power rsquared 0.1, power(0.8) ntested(3)

R Package:pwr

{ design <- pwr::pwr.f2.test(u=3,f2=0.1/(1-0.1),power=0.8); ceiling(design$v+3+1) }

Differs from Stata f2=(R2_full-R2_reduced)/(1-R2_full); residual df is N minus all predictors minus one.

Calculate power for incremental explained variance

Stata

power rsquared 0.2 0.3, n(120) ntested(2) ncontrol(3)

R Package:pwr

pwr::pwr.f2.test(u=2,v=114,f2=(0.3-0.2)/(1-0.3))

Differs from Stata f2=(R2_full-R2_reduced)/(1-R2_full); residual df is N minus all predictors minus one.

Calculate power for a fixed-size twomeans t test

Stata

power twomeans 0 0.5, sd(1) n(80)

R Package:stats

stats::power.t.test(n=40.0,delta=0.5,sd=1,type="two.sample",sig.level=0.05)

Differs from Stata Computes the displayed normal-theory t-test or two-proportion power design. R two-sample n is per group, whereas Stata n() is total; alternative, allocation and strict two-sided rejection conventions must match.

Calculate power with unequal independent group sizes

Stata

power twomeans 0 0.5, sd(1) n1(30) n2(50)

R Package:pwr

pwr::pwr.t2n.test(n1=30,n2=50,d=0.5,sig.level=0.05)

Differs from Stata Uses the pooled-variance standardized mean-difference model; this is not a Welch heteroskedastic design.

Plan the sample size for a twomeans t test

Stata

power twomeans 0 0.5, sd(1) power(0.8)

R Package:stats

stats::power.t.test(delta=0.5,sd=1,type="two.sample",sig.level=0.05,power=0.8)

Differs from Stata R reports n per group for an independent two-sample test; round sample sizes upward.

Find the detectable difference for a twomeans t test

Stata

power twomeans 0, sd(1) n(80) power(0.8)

R Package:stats

stats::power.t.test(n=40.0,delta=NULL,sd=1,power=0.8,type="two.sample")

Differs from Stata R returns a positive detectable difference; add it to the null mean to recover the alternative mean.

Calculate power for two fixed-size proportion samples

Stata

power twoproportions 0.3 0.5, n(200)

R Package:stats

stats::power.prop.test(n=100,p1=0.3,p2=0.5,sig.level=0.05)

Differs from Stata Computes the displayed normal-theory t-test or two-proportion power design. R two-sample n is per group, whereas Stata n() is total; alternative, allocation and strict two-sided rejection conventions must match.

Calculate one-sided two-proportion power

Stata

power twoproportions 0.3 0.5, n(200) onesided

R Package:stats

stats::power.prop.test(n=100,p1=0.3,p2=0.5,alternative="one.sided")

Differs from Stata Computes the displayed normal-theory t-test or two-proportion power design. R two-sample n is per group, whereas Stata n() is total; alternative, allocation and strict two-sided rejection conventions must match.

Plan sample size for two independent proportions

Stata

power twoproportions 0.3 0.5, power(0.8)

R Package:stats

stats::power.prop.test(p1=0.3,p2=0.5,power=0.8,sig.level=0.05)

Differs from Stata R reports sample size per group. Normal-approximation conventions and continuity corrections may differ.

Find a detectable alternative proportion

Stata

power twoproportions 0.3, n(200) power(0.8)

R Package:stats

stats::power.prop.test(n=100,p1=0.3,p2=NULL,power=0.8,sig.level=0.05)

Differs from Stata Computes the displayed normal-theory t-test or two-proportion power design. R two-sample n is per group, whereas Stata n() is total; alternative, allocation and strict two-sided rejection conventions must match.