ggplot中的幂曲线打印空白图

时间:2018-07-09 20:10:56

标签: r ggplot2 inference sample-size

我想生成一些功效曲线,其效果尺寸在x轴上,功效在y轴上。我仅对该源代码稍作修改: https://moderndata.plot.ly/power-curves-r-plotly-ggplot2/创建功率曲线。作者正在使用双面t检验,而我正在使用两个比例检验。

我运行了作者的代码以进行测试,并且工作正常。当我运行自己的代码并稍作修改时(我仅修改了循环中的零件以进行两个比例检验,而不是t检验),图中没有任何内容。我无法弄清楚我在做什么错。请注意,我现在只保留作者的列名,因为我只想测试我的代码。

library(pwr) # for power calcs
library(dplyr) # for data manipulation
library(tidyr) # for data manipulation
library(ggplot2) # for plotting power curves
library(plotly) # for interactive power curves

# Generate power calculations
ptab <- cbind(NULL, NULL)       

for (i in seq(0,1, length.out = 200)){
  pwrt1 <- pwr.2p.test(h = ES.h(p1 = 0.3, p2 = 0.05),n=16,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

  pwrt2 <- pwr.2p.test(h = ES.h(p1 = 0.3, p2 = 0.10),n=16,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

  pwrt3 <- pwr.2p.test(h = ES.h(p1 = 0.3, p2 = 0.15),n=16,power = NULL, 
                    sig.level = 0.01,alternative=c("greater"))

  pwrt4 <- pwr.2p.test(h = ES.h(p1 = 0.3, p2 = 0.20),n=16,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

  pwrt5 <- pwr.2p.test(h = ES.h(p1 = 0.3, p2 = 0.25),n=16,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

  pwrt6 <- pwr.2p.test(h = ES.h(p1 = 0.3, p2 = 0.30),n=16,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

  ptab <- rbind(ptab, cbind(pwrt1$h, pwrt1$power,
                        pwrt2$h, pwrt2$power,
                        pwrt3$h, pwrt3$power,
                        pwrt4$h, pwrt4$power,
                        pwrt5$h, pwrt5$power,
                        pwrt6$h, pwrt6$power))
}

ptab <- cbind(seq_len(nrow(ptab)), ptab)

colnames(ptab) <- c("id","n1=28, n2=1406.effect size","n1=28, 
n2=1406.power",
                "n1=144, n2=1290.effect size","n1=144, n2=1290.power",
                "n1=287, n2=1147.effect size","n1=287, n2=1147.power",
                "n1=430, n2=1004.effect size","n1=430, n2=1004.power",
                "n1=574, n2=860.effect size","n1=574, n2=860.power",
                "n1=717, n2=717.effect size","n1=717, n2=717.power")

# get data into right format for ggplot2
temp <- ptab %>%
  as.data.frame() %>%
  gather(key = name, value = val, 2:13) %>%
  separate(col = name, into = c("group", "var"), sep = "\\.") %>%
  spread(key = var, value = val)

# factor group
temp$group <- factor(temp$group, 
                 levels = c("n1=28, n2=1406", "n1=144, n2=1290", 
                            "n1=287, n2=1147", "n1=430, n2=1004",
                            "n1=574, n2=860", "n1=717, n2=717"))


# plot
p <- ggplot(temp, aes(x = size, y = power, color = group))+ 
geom_line(size=2) + 
theme_bw() + 
theme(axis.text=element_text(size=14), 
    axis.title=element_text(size=14), 
    legend.text=element_text(size=14)) +
geom_vline(xintercept = .54, linetype = 2) +
geom_hline(yintercept = 0.80, linetype = 2)

p

编辑:我弄清楚我做错了什么,我在不需要的时候指定了效果大小选项,因为循环将遍历介于0和1之间的所有效果大小。循环解决了我的问题:

for (i in seq(0,1, length.out = 200)){
  pwrt1 <- pwr.2p.test(h = i,n=16,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

  pwrt2 <- pwr.2p.test(h = i,n=32,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

  pwrt3 <- pwr.2p.test(h = i,n=48,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

  pwrt4 <- pwr.2p.test(h = i,n=64,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

  pwrt5 <- pwr.2p.test(h = i,n=80,power = NULL, 
                   sig.level = 0.01,alternative=c("greater"))

0 个答案:

没有答案