[R] Using power.t.test over a range of conditions

Inman, Brant A. M.D. Inman.Brant at mayo.edu
Fri Apr 20 19:20:03 CEST 2007


I would like to perform sample size calculations for an experiment.  As
part of this process, I would like to know how various assumptions
affect the sample size calculation.  For instance, one thing that I
would like to know is how the calculated sample size changes as I vary
the difference that I would like to detect.  I tried the following
first, but got the associated error message.


> power.t.test(delta=seq(500,2000,100), sd=1000, sig.level=0.05,
+ type='two.sample', alt='two.sided')

Error in uniroot(function(n) eval(p.body) - power, c(2, 1e+07)) : 
        invalid function value in 'zeroin'
In addition: Warning message:
the condition has length > 1 and only the first element will be used in:
if (f(lower, ...) 
* f(upper, ...) >= 0) stop("f() values at end points not of opposite


>From the error message I suspected that the function did not handle
vectors as arguments.  I therefore tried the following looping structure
to solve the problem:


DELTA  <- seq(500,2000,250)
SD	 <- seq(1000,2500,250)
result <- matrix(nrow=length(DELTA), ncol=length(SD))
	colnames(result) <- paste('SD=',SD, sep='')
	rownames(result) <- paste('Delta=',DELTA, sep='')

for(i in 1:length(DELTA)){
	for(j in 1:length(SD)){
		result[i,j] <- power.t.test(delta=DELTA[i], sd=SD[j],
sig.level=0.05, power=0.8,
			type='two.sample', alt='two.sided')

Error in result[i, j] <- power.t.test(delta = DELTA[i], sd = SD[j],
sig.level = 0.05,  : 
        number of items to replace is not a multiple of replacement


Can some one tell me what I am doing wrong here?

Thanks in advance for your help,

Brant Inman

More information about the R-help mailing list