没错,R的输出通常仅包含基本信息,并且需要单独计算更多信息。
N <- 100 # generate some data
X1 <- rnorm(N, 175, 7)
X2 <- rnorm(N, 30, 8)
X3 <- abs(rnorm(N, 60, 30))
Y <- 0.5*X1 - 0.3*X2 - 0.4*X3 + 10 + rnorm(N, 0, 12)
# dichotomize Y and do logistic regression
Yfac <- cut(Y, breaks=c(-Inf, median(Y), Inf), labels=c("lo", "hi"))
glmFit <- glm(Yfac ~ X1 + X2 + X3, family=binomial(link="logit"))
coefficients()
给您估计的回归参数。不过,解释更容易(截距除外)。 e x p (b j)bjexp(bj)
> exp(coefficients(glmFit))
(Intercept) X1 X2 X3
5.811655e-06 1.098665e+00 9.511785e-01 9.528930e-01
为了获得优势比,我们需要原始二分DV的分类交叉表和根据需要首先选择的某些概率阈值进行预测的分类。您还可以ClassLog()
在包中看到函数QuantPsyc
(如相关问题中提到的chl )。
# predicted probabilities or: predict(glmFit, type="response")
> Yhat <- fitted(glmFit)
> thresh <- 0.5 # threshold for dichotomizing according to predicted probability
> YhatFac <- cut(Yhat, breaks=c(-Inf, thresh, Inf), labels=c("lo", "hi"))
> cTab <- table(Yfac, YhatFac) # contingency table
> addmargins(cTab) # marginal sums
YhatFac
Yfac lo hi Sum
lo 41 9 50
hi 14 36 50
Sum 55 45 100
> sum(diag(cTab)) / sum(cTab) # percentage correct for training data
[1] 0.77
对于优势比,您可以使用打包vcd
或手动进行计算。
> library(vcd) # for oddsratio()
> (OR <- oddsratio(cTab, log=FALSE)) # odds ratio
[1] 11.71429
> (cTab[1, 1] / cTab[1, 2]) / (cTab[2, 1] / cTab[2, 2])
[1] 11.71429
> summary(glmFit) # test for regression parameters ...
# test for the full model against the 0-model
> glm0 <- glm(Yfac ~ 1, family=binomial(link="logit"))
> anova(glm0, glmFit, test="Chisq")
Analysis of Deviance Table
Model 1: Yfac ~ 1
Model 2: Yfac ~ X1 + X2 + X3
Resid. Df Resid. Dev Df Deviance P(>|Chi|)
1 99 138.63
2 96 110.58 3 28.045 3.554e-06 ***
cbind( exp(coef(x)), exp(summary(x)$coefficients[,1] - 1.96*summary(x)$coefficients[,2]), exp(summary(x)$coefficients[,1] + 1.96*summary(x)$coefficients[,2]) )
。还有一种增量方法:ats.ucla.edu/stat/r/faq/deltamethod.htm