Student201
Student201

Reputation: 45

R: Plotting sample size against power

Hi I am trying to plot sample size against power in R. I think it would require a for loop but do not know how to implement this correctly. Any help would be appreciated, thank you.

 for (i in c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1)){
   graph<-power.prop.test(p1=0.4,p2=0.24,power=c(0.1:1),sig.level=0.05)}

    plot(graph$n,graph$power,type="b",xlab="sample size",ylab="power")

Upvotes: 0

Views: 1278

Answers (1)

ekstroem
ekstroem

Reputation: 6191

The power.t.test and power.prop.test functions in R don't behave nicely with vectorized arguments so you need to wrap the call in a loop-like construct as you suggest.

Below I use the sapply function for iterations and extract the sample size, which is then used for plotting.

powrange <- seq(0.4, .9, .1)
n <- sapply(powrange, function(i) power.prop.test(p1=0.4,p2=0.24,power=i,sig.level=0.05)$n)
plot(n, powrange, type="b",xlab="sample size",ylab="power")

If you don't extract n then you end up with a list of output results and you'd need to iterate over the output to get n from that.

Upvotes: 2

Related Questions