我想制作一个"广义线性模型的固定/随机模型"(family ="binomial"),因为我有一个数据库,观察来自一个群体,并且有一个分组结构.然后,我使用的功能glmer从lme4包装,还我已阅读,我可以使用glmmPQL函数从库MASS(遥远,2006年).
当我想使用Hausman测试证明使用随机模型和固定模型时,我的问题出现了(Greene,2012),我没有找到允许我这样做的特定函数,类似于phtest包中的测试plm.
如何证明使用随机模型?
这是对plm::phtest函数的直接调整.我评论过我实际改变的唯一代码行.使用您自己承担的风险,如果可能的话,可以根据结果进行测试plm::phtest.我刚刚修改了代码,没想到它是否真的做得对!
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)
}
Run Code Online (Sandbox Code Playgroud)
例:
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
Run Code Online (Sandbox Code Playgroud)
这似乎也适用于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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1920 次 |
| 最近记录: |