gbg
gbg

Reputation: 69

How to conduct a chi square power analysis testing multiple proportions using the R pwr package and a nested for loop?

I am trying to conduct a power analysis for a chi square test and am struggling quite a bit. I would like to see how many individuals are needed for a study, given that the expected proportion of people with an outcome is 0.1, 0.3, 0.5, and 0.7. I'd like to compare the number of individuals needed, when the difference between the expected proportion and proportion found in the study is 0.05, 0.1, 0.15, and .2.

That is, I'd like to compare:

0.1 (as the P0) to 0.15, 0.2, 0.25, and .3

0.3 to 0.35, 0.4, 0.45, and 0.5

0.5 to 0.55, 0.6, 0.65, and 0.7

0.7 to 0.75, 0.8, 0.85 and 0.9

I have written the following code to do so using a nested for loop:

library(pwr)

P0 <-seq(.1,.7, by=.2)
P1 <- c(0.05, .1, 0.15, .2)

for (i in P0){
  for(j in P1){
    effect.size3 <- pwr.chisq.test(w=ES.w1(i,(i+j)),df=1, sig.level = 0.05, power=0.8)$N
  }
}

However, when I print effect.size3, I only get one number (one N) instead of a list of 20 different Ns depending on the different proportions compared. Does anyone know why my nested for loop isn't working and how to conduct the analysis I want using the pwr package?

Upvotes: 0

Views: 118

Answers (0)

Related Questions