[R] Problem with Loops

Erik Iverson eriki at ccbr.umn.edu
Fri Apr 9 05:28:49 CEST 2010


Hello,

mtesche wrote:
> Hello,
> 
> I am trying to write a script with the end goal of graphing power (y) as a
> result of sample size (x) at a variety of effects sizes.  I am new to loops,
> and I think my problem is there.  Here's the script, which is modified from
> the script found at the bootom of
> http://www.statmethods.net/stats/power.html.  ANy help would be much
> appreciated!mtesche at ualberta.ca
> 
> library(pwr)
> library(lattice)
> 
> # range of sample sizes
> r <- seq(5,300,5)
> nr <- length(r)
> 
> # effect sizes
> p <- seq(.4,1.2,.2)
> np <- length(p)
> 
> # obtain power
> power <- array(numeric(nr*np), dim=c(nr,np))
> for (i in 1:np){
>   for (j in 1:nr){
>     result <- pwr.t.test(n = r[j], d = p[i],
>     sig.level = .05, power = NULL,
>     alternative = "two.sided")
>     power[j,i] <- ceiling(result$power)
>   }
> }

First problem.  Why are you calling ceiling, which will return 1 for any value 
of a power?  What were you thinking that line was doing?



More information about the R-help mailing list