Simple question. A vector of ?number of observations? can be input to power.t.test, and a vector of ?power? s is output. But, inputting a vector of powers generates an error. Am I missing something? Vector of ?n? s power.t.test(n=c(28,29,30), delta=2, sd=3, sig.level=0.05, type="two.sample", alternative="one.sided") Two-sample t test power calculation n = 28, 29, 30 delta = 2 sd = 3 sig.level = 0.05 power = 0.7933594, 0.8058963, 0.8177506 alternative = one.sided NOTE: n is number in *each* group Vector of ?power? s power.t.test(power=c(0.7,0.8,0.9), delta=2, sd=3, sig.level=0.05, type="two.sample", alternative="one.sided") Error in uniroot(function(n) eval(p.body) - power, c(2, 1e+07)) : f() values at end points not of opposite sign In addition: Warning messages: 1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") : the condition has length > 1 and only the first element will be used 2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") : the condition has length > 1 and only the first element will be used Thanks, Steve
On 01.10.2014 14:29, Stephen Kennedy wrote:> Simple question. A vector of ?number of observations? can be input to power.t.test, and a vector of ?power? s is output. But, inputting a vector of powers generates an error. Am I missing something? > > Vector of ?n? s > > power.t.test(n=c(28,29,30), delta=2, sd=3, sig.level=0.05, type="two.sample", alternative="one.sided") > > Two-sample t test power calculation > > n = 28, 29, 30 > delta = 2 > sd = 3 > sig.level = 0.05 > power = 0.7933594, 0.8058963, 0.8177506 > alternative = one.sided > > NOTE: n is number in *each* group > > > > Vector of ?power? s > > power.t.test(power=c(0.7,0.8,0.9), delta=2, sd=3, sig.level=0.05, type="two.sample", alternative="one.sided") > Error in uniroot(function(n) eval(p.body) - power, c(2, 1e+07)) : > f() values at end points not of opposite sign > In addition: Warning messages: > 1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") : > the condition has length > 1 and only the first element will be used > 2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") : > the condition has length > 1 and only the first element will be used >power.t.test oes not work on vectors in general. Here, you want: lapply(c(0.7, 0.8, 0.9), function(power) power.t.test(power=power, delta=2, sd=3, sig.level=0.05, type="two.sample", alternative="one.sided") ) Best, Uwe Ligges> > Thanks, > > Steve > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On 01 Oct 2014, at 14:29 , Stephen Kennedy <stephen at prollenium.com> wrote:> Simple question. A vector of ?number of observations? can be input to power.t.test, and a vector of ?power? s is output. But, inputting a vector of powers generates an error. Am I missing something?Power.t.test was written for scalar arguments. If it happens to work with vector arguments, it is entirely coincidental. The essence of what you observe is that power is calculated by pt() which vectorizes, but n is calculated by numerical solution using uniroot() which does not vectorize. If you need a vectorized version, check out Vectorize(). -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com