繁体   English   中英

R中的glm,进行所有比较

[英]glm in R, give all comparisons

简单的逻辑回归示例。

set.seed(1)
df <- data.frame(out=c(0,1,0,1,0,1,0,1,0), 
           y=rep(c('A', 'B', 'C'), 3))

result <-glm(out~factor(y), family = 'binomial', data=df)
summary(result)

#Call:
#glm(formula = out ~ factor(y), family = "binomial", data = df)

#Deviance Residuals: 
#    Min       1Q   Median       3Q      Max  
#-1.4823  -0.9005  -0.9005   0.9005   1.4823  

#Coefficients:
#              Estimate Std. Error z value Pr(>|z|)
#(Intercept) -6.931e-01  1.225e+00  -0.566    0.571
#factor(y)B   1.386e+00  1.732e+00   0.800    0.423
#factor(y)C   3.950e-16  1.732e+00   0.000    1.000

#(Dispersion parameter for binomial family taken to be 1)

#     Null deviance: 12.365  on 8  degrees of freedom
#Residual deviance: 11.457  on 6  degrees of freedom
#AIC: 17.457

#Number of Fisher Scoring iterations: 4

我的参考类别现在是A; 给出了B和C相对于A的结果。 当B和C作为参考时,我也想得到结果。 可以通过在factor()使用levels =来手动更改参考factor() 但这需要安装3个模型。 一口气可以做到吗? 还是哪种方法更有效?

如果要进行所有成对比较,通常还应该对由于多次测试而导致的α误差膨胀进行校正。 您可以使用软件包multcomp轻松进行Tukey测试。

set.seed(1)
df <- data.frame(out=c(0,1,0,1,0,1,0,1,0), 
                 y=rep(c('A', 'B', 'C'), 3))

#y is already a factor, if not, coerce before the model fit
result <-glm(out~y, family = 'binomial', data=df)
summary(result)

library(multcomp)
comps <- glht(result, linfct = mcp(y = "Tukey"))
summary(comps)
#Simultaneous Tests for General Linear Hypotheses
#
#Multiple Comparisons of Means: Tukey Contrasts
#
#
#Fit: glm(formula = out ~ y, family = "binomial", data = df)
#
#Linear Hypotheses:
#  Estimate Std. Error z value Pr(>|z|)
#B - A == 0  1.386e+00  1.732e+00     0.8    0.703
#C - A == 0  1.923e-16  1.732e+00     0.0    1.000
#C - B == 0 -1.386e+00  1.732e+00    -0.8    0.703
#(Adjusted p values reported -- single-step method)

#letter notation often used in graphs and tables
cld(comps)
#  A   B   C 
#"a" "a" "a"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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