testname123
testname123

Reputation: 1091

lapply and passing arguments

I'm trying to learn how to effectivly use the apply family in R. I have the following numeric vector

>aa
 [1] 0.047619 0.000000      NaN 0.000000 0.000000 0.000000 0.000000 0.000000
 [9]      NaN      NaN 0.000000      NaN      NaN      NaN      NaN      NaN
[17] 0.000000 0.000000      NaN      NaN      NaN      NaN      NaN      NaN
[25]      NaN 0.100000 0.000000 0.000000 0.000000 0.000000 1.000000      NaN
[33]      NaN      NaN      NaN      NaN      NaN      NaN 0.133333      NaN
[41]      NaN 0.000000 0.000000 0.000000      NaN      NaN      NaN      NaN
[49]      NaN

and I'm trying to get the n factor out of pwr.t.test with each of these as an input to the d argument.

My attempt(s) have yielded this as the latest result, and frankly, I'm stumped...> lapply(aa,function(x) pwr.t.test(d=x,power=.8,sig.level=.05,type="one.sample",alternative="two.sided"))

with the following error message:

Error in uniroot(function(n) eval(p.body) - power, c(2 + 1e-10, 1e+07)) : 
  f() values at end points not of opposite sign

Any ideas on the right way to do this?

Upvotes: 0

Views: 270

Answers (1)

Christopher Louden
Christopher Louden

Reputation: 7592

Short answer: The number of subjects needed is greater than the maximum that R will check for. Add some checks so that you don't run the function when d == 0 and it will work.

When d = 0, you need an infinite number of subjects to detect the difference. The error you are seeing is because R tries to calculate power numerically. The algorithm R uses first checks the bounds of the interval over which the possible values for N lie (about 2 to 1e+07). Because the function for power has the same sign at both endpoints of the interval and is monotonic in N, R throws an error saying that the root (the value of N you are looking for) cannot be found.

Upvotes: 0

Related Questions