R中的glm,进行所有比较

时间:2018-08-06 07:05:35

标签: r model regression glm

简单的逻辑回归示例。

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作为参考时,我也想得到结果。可以使用levels =中的factor()手动更改参考;但这需要安装3个模型。一口气可以做到吗?还是哪种方法更有效?

1 个答案:

答案 0 :(得分: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"