R语言实战(第3版)第10章代码

#-------------------------------------------------------------------#
# R in Action (3rd ed): Chapter 10                                  #
# Power analysis                                                    #
# requires the pwr and ggplot2 packages                             #
# install.packages(c("pwr", "ggplot2"))                             #
#-------------------------------------------------------------------#

# t-tests
library(pwr)
pwr.t.test(d=.8, sig.level=.05, power=.9, type="two.sample",         
           alternative="two.sided")
pwr.t.test(n=20, d=.5, sig.level=.01, type="two.sample", 
           alternative="two.sided")

# ANOVA
pwr.anova.test(k=5, f=.25, sig.level=.05, power=.8)

# correlations
pwr.r.test(r=.25, sig.level=.05, power=.90, alternative="greater")

# linear models
pwr.f2.test(u=3, f2=0.0769, sig.level=0.05, power=0.90)

# tests of proportions
pwr.2p.test(h=ES.h(.65, .6), sig.level=.05, power=.9, 
            alternative="greater")

# chi-square tests
prob <- matrix(c(.42, .28, .03, .07, .10, .10), byrow=TRUE, nrow=3)
ES.w2(prob)
pwr.chisq.test(w=.1853, df=2, sig.level=.05, power=.9)

# Listing 10.1 Sample sizes for detecting significant effects in a one-way ANOVA
library(pwr)
es <- seq(.1, .5, .01)                                               
nes <- length(es)

samsize <- NULL                                                      
for (i in 1:nes){                                                    
  result <- pwr.anova.test(k=5, f=es[i], sig.level=.05, power=.9)  
  samsize[i] <- ceiling(result$n)                                  
}                                                                    

plotdata <- data.frame(es, samsize)
library(ggplot2)
ggplot(plotdata, aes(x=samsize, y=es)) +
  geom_line(color="red", size=1) +
  theme_bw() +
  labs(title="One Way ANOVA (5 groups)",
       subtitle="Power = 0.90,  Alpha = 0.05",
       x="Sample Size (per group)",
       y="Effect Size") 

# Listing 10.2 Sample size curves for detecting correlations of various sizes
library(pwr)
r <- seq(.1,.5,.01)                                         
p <- seq(.4,.9,.1)                                        

df <- expand.grid(r, p)
colnames(df) <- c("r", "p")

for (i in 1:nrow(df)){                                       
  result <- pwr.r.test(r = df$r[i],
                       sig.level = .05, power = df$p[i],
                       alternative = "two.sided")
  df$n[i] <- ceiling(result$n)
}

library(ggplot2)                                             
ggplot(data=df,
       aes(x=r, y=n, color=factor(p))) +
  geom_line(size=1) +
  theme_bw() +
  labs(title="Sample Size Estimation for Correlation Studies",
       subtitle="Sig=0.05 (Two-tailed)",
       x="Correlation Coefficient (r)",
       y="Samsple Size (n)",
       color="Power")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值