Hausman针对"glmer"的规格测试来自lme4 [英] Hausman's specification test for "glmer" from lme4

查看:195
本文介绍了Hausman针对"glmer"的规格测试来自lme4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个广义线性模型的固定/随机模型"(family ="binomial"),因为我有一个数据库,其中的观察值来自人群,并且存在分组结构.然后,我使用了lme4包中的函数glmer,我也读到可以使用库MASS中的glmmPQL函数(Faraway,2006年).

I want to make a "Fixed/Random Models for Generalized linear model" (family="binomial"), because I have a data base where observations come from a population and there is a grouping structure. Then I use the function glmer from the lme4 package, also I have read that I can use the glmmPQL function from library MASS (Faraway,2006).

当我想通过Hausman检验证明使用随机模型与固定模型的合理性时,就会出现我的问题(Greene,2012年),我找不到一个类似于phtest测试功能的特定函数来执行此操作在软件包plm中.

My problem arises when I want to justify the use of random versus fixed model using the Hausman's test (Greene,2012), I don't find a specific function that allows me to do this similar to the phtest test featured in the package plm.

如何证明使用随机模型的合理性?

How to justify using the random model?

推荐答案

这是对plm::phtest函数的直接调整.我评论了我实际更改过的仅有的几行代码. 由您自担风险,并尽可能对plm::phtest的结果进行测试.我只是修改了代码,没有考虑它是否真的在做正确的事!

This is a straightforward tweaking of the plm::phtest function. I've commented on the only lines of code that I actually changed. USE AT YOUR OWN RISK and if at all possible test it against the results from plm::phtest. I have just adapted the code, not thought about whether it's really doing the right thing!

phtest_glmer <- function (glmerMod, glmMod, ...)  {  ## changed function call
    coef.wi <- coef(glmMod)
    coef.re <- fixef(glmerMod)  ## changed coef() to fixef() for glmer
    vcov.wi <- vcov(glmMod)
    vcov.re <- vcov(glmerMod)
    names.wi <- names(coef.wi)
    names.re <- names(coef.re)
    coef.h <- names.re[names.re %in% names.wi]
    dbeta <- coef.wi[coef.h] - coef.re[coef.h]
    df <- length(dbeta)
    dvcov <- vcov.re[coef.h, coef.h] - vcov.wi[coef.h, coef.h]
    stat <- abs(t(dbeta) %*% as.matrix(solve(dvcov)) %*% dbeta)  ## added as.matrix()
    pval <- pchisq(stat, df = df, lower.tail = FALSE)
    names(stat) <- "chisq"
    parameter <- df
    names(parameter) <- "df"
    alternative <- "one model is inconsistent"
    res <- list(statistic = stat, p.value = pval, parameter = parameter, 
        method = "Hausman Test",  alternative = alternative,
                data.name=deparse(getCall(glmerMod)$data))  ## changed
    class(res) <- "htest"
    return(res)
}

示例:

library(lme4)
gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
                   data = cbpp, family = binomial)
gm0 <- glm(cbind(incidence, size - incidence) ~ period +  herd,
                   data = cbpp, family = binomial)

phtest_glmer(gm1,gm0)
##  Hausman Test
## data:  cbpp
## chisq = 10.2747, df = 4, p-value = 0.03605
## alternative hypothesis: one model is inconsistent

这似乎也适用于lme模型:

library("nlme")
fm1 <- lme(distance ~ age, data = Orthodont) # random is ~ age
fm0 <- lm(distance ~ age*Subject, data = Orthodont)
phtest_glmer(fm1,fm0)

## Hausman Test 
## data:  Orthodont
## chisq = 0, df = 2, p-value = 1
## alternative hypothesis: one model is inconsistent

这篇关于Hausman针对"glmer"的规格测试来自lme4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆