# [R] stats::power.t.test error

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Tue Oct 15 17:49:34 CEST 2019

```You don't really want the diddling, since it gives meaningless values anyway...

(a) calculate the power at n=2, if bigger than target power, done, else
(b) calculate n to reach target power, now guaranteed to have n > 2. Round upwards.

Peter D.

> On 15 Oct 2019, at 12:07 , Witold E Wolski <wewolski using gmail.com> wrote:
>
> Dear Peter,
>
> Yes, It is a technical issue and a matter of diddling around. And I
> agree with your comment regarding the 2 observations.
> I have several thousands variance estimates for which I need to
> compute the sample sizes automatically. Using try statements is
> typically the last thing I would like to resort too.
> Is there an alternative implementation of power.t.test on CRAN which
> could the diddling for me and return plausible sample sizes i.e.
> integers.
>
> best regards
> Witek
>
> On Fri, 4 Oct 2019 at 16:28, peter dalgaard <pdalgd using gmail.com> wrote:
>>
>> This is mainly a technical issue with uniroot trying to go outside of its interval: (2, 1e7)
>>
>> It is fairly easy to find an approximate solution by diddling a little by hand:
>>
>>> power.t.test(delta = 0.5849625, sd=0.01, n=1.04, sig.level=0.05)\$power
>> [1] 0.8023375
>>
>> Notice, however, that 1.04 observations in each group makes no sense at all. In order to actually do a t-test you need at least 2 observations per group (since we assume equal group sizes) or you have no variance estimate. Already at sd=0.1, you are crossing the n=2 border, so for any smaller sd, you will just get higher power with n=2. (Also, anything with single-digit degrees of freedom for variance is probably expecting rather much regarding to Gaussian distribution of your data.)
>>
>> -pd
>>
>>> On 4 Oct 2019, at 14:30 , Witold E Wolski <wewolski using gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> power.t.test works for some range of input parameters but fails otherwise.
>>>
>>>> power.t.test(delta = 0.5849625, sd=0.1, power=0.8, sig.level=0.05)\$n
>>> [1] 1.971668
>>>> power.t.test(delta = 0.5849625, sd=0.05, power=0.8, sig.level=0.05)\$n
>>> [1] 1.620328
>>>> power.t.test(delta = 0.5849625, sd=0.01, power=0.8, sig.level=0.05)\$n
>>> Error in uniroot(function(n) eval(p.body) - power, c(2, 1e+07), tol = tol,  :
>>> did not succeed extending the interval endpoints for f(lower) * f(upper) <= 0
>>> In qt(sig.level/tside, nu, lower.tail = FALSE) : NaNs produced
>>>
>>> I guessing that sd is very small compared with delta, hence the
>>> problem. But what are allowed values (ratios) of delta and sd?
>>>
>>> Best
>>> Witek
>>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Witold Eryk Wolski
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> --
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School
>> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Office: A 4.23
>> Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
>
> --
> Witold Eryk Wolski

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com

```