【发布时间】:2015-09-27 05:46:41
【问题描述】:
我想模拟多级逻辑分布 1000 次和每次估计参数的数据并计算估计值的平均值。但似乎,在glmer() 函数中的结果不能像lm() 函数那样提取,比如lm(y~x)$coef。如何从glmer() 函数中提取结果?
这里是 R 代码:
#Simulating data from multilevel logistic distribution
library(mvtnorm)
library(lme4)
set.seed(1234)
## J = number of groups
## n = group size
## g00,g10,g01,g11 = fixed effect parameters .
## s2_0,s2_1,s01 = variance values for the group level random effect .
simu <- function(J,n,g00,g10,g01,g11,s2_0,s2_1,s01){
n_j <- rep(n,J) ## number of individuals in jth group
N <- sum(n_j) ## sample size
#Simulate the covariate value for this sample size .
z <- rnorm(J)
x <- rnorm(N)
#Generate (u_0j,u_1j) from a bivariate normal .
mu <- c(0,0)
sig <- matrix(c(s2_0,s01,s01,s2_1),ncol=2)
u <- rmvnorm(J,mean=mu,sigma=sig,method="chol")
#Now form the linear predictor .
pi_0 <- g00 +g01*z + u[,1]
pi_1 <- g10 + g11*z + u[,2]
eta <- rep(pi_0,n_j)+rep(pi_1,n_j)*x
#Transform back to the probability scale .
p <- exp(eta)/(1+exp(eta))
#Simulate a bernoulli from each individual distribution .
y <- rbinom(N,1,p)
# estimating parameters
sim_data_mat <- matrix(c(y,x,rep(z,n_j),rep(1:30,n_j)),ncol=4)
sim_data <- data.frame(sim_data_mat)
colnames(sim_data) <- c("Y","X","Z","cluster")
res <-summary(glmer(Y~X*Z+(1|cluster),family=binomial,data=sim_data,nAGQ=10))
res$coef
}
out <- replicate(10,simu(30,5,-1,.3,.3,.3,.13,1,0))
##Error in res$coef : $ operator not defined for this S4 class
如何从glmer() 函数中提取结果?
sessionInfo()
R version 2.14.0 (2011-10-31)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lme4_0.999999-0 Matrix_1.0-1 lattice_0.20-10 mvtnorm_0.9-9994
loaded via a namespace (and not attached):
[1] grid_2.14.0 nlme_3.1-102 stats4_2.14.0 tools_2.14.0
【问题讨论】:
-
我无法重现错误。
-
对我来说很好,并返回系数表以及标准误差、z 值、p 值等。
-
@mathematical.coffee 我不想要所有结果。相反,我只想从输出中提取系数。当我运行代码
summary(glmer(Y~X*Z+(1|cluster),family=binomial,data=sim_data,nAGQ=10))$coef时发生错误。它说$ operator not defined for this S4 class -
你的意思是
summary(glmer(Y~X*Z+(1|cluster),family=binomial,data=sim_data,nAGQ=10))$coef?你的评论中有一个错字就在我的上面。这个对我有用。请发布sessionInfo()的输出 -
非常旧的 R 版本。请升级所有内容。
标签: r statistics regression logistic-regression