使用'bife'包的固定效果logit模型的拟合优度

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)

Jul*_*ora 6

让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)

用于对数似然提取.然后,我们可以比较modBife0modBife作为

1 - pchisq((-2 * logLik(modBife0)) - (-2 * logLik(modBife)), length(modBife$par$beta))
# [1] 1
Run Code Online (Sandbox Code Playgroud)

同时modGLM0modBife可以通过运行进行比较

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之下.

在此输入图像描述

  • @Eric,第一评论:`长度(唯一(Z))`207应该是有意义的(我将在今天或明天添加测试模拟结果).重新评论第二条评论:对,这是一个值得报道的事实,而且卡方在更大的自由度下获得更大的价值.我的id就像你的Z:它们是固定的效果(每个人的虚拟变量,或者在你的情况下,每个时间段),在`modBife $ par $ alpha`给出的估计值.Re R ^ 2:在逻辑回归中,不再有明确的R ^ 2; 有多个提案.一个是McFadden的R ^ 2,由`c(1- logLik(modBife)/ logLik(modGLM0))`给出. (2认同)