简体   繁体   中英

Plotting power plot in R without using ggplot2()?

Having trouble creating a plot of different power functions for different alpha levels. This is what I have currently but I cannot figure out how to create the multiple lines representing the smooth power function across different alpha levels:

d <- data.frame()
for (s in seq(0,.5,.05)) {
  for (n in seq(20,500,by=20)){
    d <- rbind(d,power.t.test(n=n,delta = 11,sig.level=s,sd= 22.9))
  }
}

d$sig.level.factor <-as.factor(d$sig.level)
plot(d$power~d$n, col=d$sig.level.factor)

for i in length(sig.level.factor){
  lines(d$n[d$sig.level.factor==d$sig.level.factor[i]],d$power[d$sig.level.factor==d$sig.level.factor[i]], type="l", lwd=2, col=colors[n])
}


for (i in 1:length(seq(0,.5,.05))){
  lines(d$n[d$sig.level.factor==d$sig.level[i]], d$power, type="l", lwd=2, col=colors[i])
}
for (i in 1:length(d$sig.level.factor)){
  lines(d$n[d$sig.level.factor==i], d$power[d$sig.level.factor==i], type="l", lwd=2, col=colors[i])
}

My goal is to create the lines that will show the smooth curves connecting all the points that contain equivalent alpha values across different sample sizes.

Slightly late answer but hope you can still use it. You can create a matrix of results over n and significance levels using sapply and then plot everything in one go using the super useful function matplot :

n <- seq(20, 300, by=10)
alphas <- seq(0, .25, .05)
res <- sapply(alphas, function(s) {power.t.test(n=n, delta=11, sig.level=s, sd= 22.9)$power})
matplot(n, res, type="l", xlab="Sample size", ylab="Power")

There is one annoying "feature" of power.t.test and power.prop.test and that is that it is not fully vectorized over all arguments. However, your situation, where the output is the power makes it easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM