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 = 手动更改引用;但这需要安装 3 个模型。有没有可能一次性做到这一点?或者什么是更有效的方法?

如果要进行所有成对比较,通常还应该对由于多重测试而导致的 alpha 错误 inflation 进行更正。您可以使用包 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"