heliosdrm/pwr

For two-sample, one-sided proportion and mean tests, `alternative = "greater"` works but `alternative = "less"` does not

bschneidr opened this issue · 3 comments

For some reason I'm able to specify alternative = 'greater' but not alternative = 'less' in pwr.2p.test(), although it seems to me like both options should be valid and return the same result. Perhaps this is a bug, or perhaps this is just a design decision that could be documented in the function documentation?

library(pwr)

# Works
pwr.2p.test(
  h = 0.1,
  n = NULL,
  sig.level = 0.04,
  power = 0.55,
  alternative = 'greater'
)
#> 
#>      Difference of proportion power calculation for binomial distribution (arcsine transformation) 
#> 
#>               h = 0.1
#>               n = 704.1359
#>       sig.level = 0.04
#>           power = 0.55
#>     alternative = greater
#> 
#> NOTE: same sample sizes

# Fails
pwr.2p.test(
  h = 0.1,
  n = NULL,
  sig.level = 0.04,
  power = 0.55,
  alternative = 'less'
)
#> Error in uniroot(function(n) eval(p.body) - power, c(2 + 1e-10, 1e+09)): f() values at end points not of opposite sign

Created on 2020-08-03 by the reprex package (v0.3.0)

It turns out the issue also applies to pwr.t2n.test():

library(pwr)

# 'greater' Works
pwr.t2n.test(n1 = NULL, n2 = 50, d = 0.5, power = 0.8, alternative = 'greater')
#> 
#>      t test power calculation 
#> 
#>              n1 = 50.30245
#>              n2 = 50
#>               d = 0.5
#>       sig.level = 0.05
#>           power = 0.8
#>     alternative = greater

# 'less' doesn't work
pwr.t2n.test(n1 = NULL, n2 = 50, d = 0.5, power = 0.8, alternative = 'less')
#> Error in uniroot(function(n1) eval(p.body) - power, c(2 + 1e-10, 1e+09)): f() values at end points not of opposite sign

Created on 2020-08-03 by the reprex package (v0.3.0)

Thank you for reporting the issue and the related PR. In my opinion it is rather a shortcoming of the documentation, rather than a bug. The option alternative = 'less' does work if the effect size is negative (i.e. if the first proportion is smaller than the second one) - and the same applies for the means in the T-test, etc.

Thus, these functions are taking a "directional" effect size - as Cohen calls it, which does not only take the absolute value of the difference, but also its sign. This might be explained better in the help files.

Ah, I see. Thanks for the quick reply!

Here's an example to illustrate the point you just made.

library(pwr)
# 'greater' Works
pwr.t2n.test(n1 = NULL, n2 = 50, d = 0.5, power = 0.8, alternative = 'greater')
#> 
#>      t test power calculation 
#> 
#>              n1 = 50.30245
#>              n2 = 50
#>               d = 0.5
#>       sig.level = 0.05
#>           power = 0.8
#>     alternative = greater

# 'less' will only work if `d < 0`
pwr.t2n.test(n1 = NULL, n2 = 50, d = 0.5, power = 0.8, alternative = 'less')
#> Error in uniroot(function(n1) eval(p.body) - power, c(2 + 1e-10, 1e+09)): f() values at end points not of opposite sign
pwr.t2n.test(n1 = NULL, n2 = 50, d = -0.5, power = 0.8, alternative = 'less')
#> 
#>      t test power calculation 
#> 
#>              n1 = 50.30245
#>              n2 = 50
#>               d = -0.5
#>       sig.level = 0.05
#>           power = 0.8
#>     alternative = less

Created on 2020-08-04 by the reprex package (v0.3.0)