Eri*_*ric 4 statistics r logistic-regression goodness-of-fit log-likelihood
我正在使用'bife'包在R中运行固定效果logit模型.但是,根据我在下面的结果,我无法计算任何拟合优度来衡量模型的整体拟合度.如果我知道如何根据这些有限的信息来衡量拟合优度,我将不胜感激.我更喜欢卡方检验,但仍无法找到实现这一目的的方法.
---------------------------------------------------------------
Fixed effects logit model
with analytical bias-correction
Estimated model:
Y ~ X1 +X2 + X3 + X4 + X5 | Z
Log-Likelihood= -9153.165
n= 20383, number of events= 5104
Demeaning converged after 6 iteration(s)
Offset converged after 3 iteration(s)
Corrected structural parameter(s):
Estimate Std. error t-value Pr(> t)
X1 -8.67E-02 2.80E-03 -31.001 < 2e-16 ***
X2 1.79E+00 8.49E-02 21.084 < 2e-16 ***
X3 -1.14E-01 1.91E-02 -5.982 2.24E-09 ***
X4 -2.41E-04 2.37E-05 -10.171 < 2e-16 ***
X5 1.24E-01 3.33E-03 37.37 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
AIC= 18730.33 , BIC= 20409.89
Average individual fixed effects= 1.6716
---------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
让DGP成为
n <- 1000
x <- rnorm(n)
id <- rep(1:2, each = n / 2)
y <- 1 * (rnorm(n) > 0)
Run Code Online (Sandbox Code Playgroud)
这样我们就会处于零假设之下.正如它所说?bife,当没有偏差校正时glm,除速度外,一切都与之相同.让我们开始吧glm.
modGLM <- glm(y ~ 1 + x + factor(id), family = binomial())
modGLM0 <- glm(y ~ 1, family = binomial())
Run Code Online (Sandbox Code Playgroud)
执行LR测试的一种方法是使用
library(lmtest)
lrtest(modGLM0, modGLM)
# Likelihood ratio test
#
# Model 1: y ~ 1
# Model 2: y ~ 1 + x + factor(id)
# #Df LogLik Df Chisq Pr(>Chisq)
# 1 1 -692.70
# 2 3 -692.29 2 0.8063 0.6682
Run Code Online (Sandbox Code Playgroud)
但我们也可以手动完成,
1 - pchisq(c((-2 * logLik(modGLM0)) - (-2 * logLik(modGLM))),
modGLM0$df.residual - modGLM$df.residual)
# [1] 0.6682207
Run Code Online (Sandbox Code Playgroud)
现在让我们继续bife.
library(bife)
modBife <- bife(y ~ x | id)
modBife0 <- bife(y ~ 1 | id)
Run Code Online (Sandbox Code Playgroud)
这modBife是完整的规范,modBife0只有固定的效果.为方便起见,让我们
logLik.bife <- function(object, ...) object$logl_info$loglik
Run Code Online (Sandbox Code Playgroud)
用于对数似然提取.然后,我们可以比较modBife0有modBife作为
1 - pchisq((-2 * logLik(modBife0)) - (-2 * logLik(modBife)), length(modBife$par$beta))
# [1] 1
Run Code Online (Sandbox Code Playgroud)
同时modGLM0并modBife可以通过运行进行比较
1 - pchisq(c((-2 * logLik(modGLM0)) - (-2 * logLik(modBife))),
length(modBife$par$beta) + length(unique(id)) - 1)
# [1] 0.6682207
Run Code Online (Sandbox Code Playgroud)
它给出了与以前相同的结果,即使bife我们默认情况下也有偏差校正.
最后,作为奖励,我们可以模拟数据并看到测试按预期工作.下面的1000次迭代表明,两个测试(因为两个测试是相同的)确实经常拒绝,因为它们应该在null之下.