R:summary()随机截距的均值和方差为何与手动计算不同?

时间:2019-10-30 05:24:19

标签: r statistics lme4 variance

如果我用R的lme4::glmer()拟合随机截距模型,则可以提取截距的结果向量,并计算这些截距的均值和方差。但是,手动计算这些数字的结果似乎与在模型对象上使用summary()的结果不匹配。发生了什么事?

这是我正在查看的确切工作示例,表明存在分歧:

set.seed(5)
# Load packages
library(fabricatr)
library(lme4)

##############################################################    
# Make some fake data with 10 clusters, 50 observations each
# and the outcomes are correlated in clusters.
##############################################################    

clusters <- rep(1:10, 50)
y <- fabricatr::draw_binary_icc(prob = 0.2, clusters = clusters, ICC = 0.5)

x1 <- rnorm(n = 500)
x2 <- rnorm(n = 500) 
x3 <- rnorm(n = 500)

dat <- data.frame(clusters, y, x1, x2, x3)

##############################################################    
# Fit binary logistic hierarchical regression model with 
# random intercepts, interaction
##############################################################    

m1 <- lme4::glmer(formula = y ~ x1*x2 + x3 + (1|clusters),
              data = dat, family = binomial(link = "logit"),
              nAGQ = 10)

##############################################################    
# Show manual vs. summary() intercept means and variance
##############################################################    

# Variance of random intercepts is 4.28, mean (Intercept) is -1.80
summary(m1)

# But manually the variance is 3.89 and mean is -1.70
intercepts <- coef(m1)$clusters["(Intercept)"]$`(Intercept)`
mean(intercepts)
var(intercepts)

0 个答案:

没有答案