以下多级逻辑模型,其中一个解释变量在级别1(个人级别),一个解释变量在级别2(组级别):
π 0 Ĵ = γ 00 + γ 01 ż Ĵ + ü 0 Ĵ ... (2 )
其中,假定组级别残差和具有期望值为零的多元正态分布。残留误差 u_ {0j}的方差指定为,残留误差u_ {1j}的方差 指定为。
我想估算模型的参数,并且喜欢使用 R
command glmmPQL
。
将方程式(2)和(3)代入方程式(1)可得出,
有30个组,每组5个。
R代码:
#Simulating data from multilevel logistic distribution
library(mvtnorm)
set.seed(1234)
J <- 30 ## number of groups
n_j <- rep(5,J) ## number of individuals in jth group
N <- sum(n_j)
g_00 <- -1
g_01 <- 0.3
g_10 <- 0.3
g_11 <- 0.3
s2_0 <- 0.13 ##variance corresponding to specific ICC
s2_1 <- 1 ##variance standardized to 1
s01 <- 0 ##covariance assumed zero
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")
pi_0 <- g_00 +g_01*z + as.vector(u[,1])
pi_1 <- g_10 + g_11*z + as.vector(u[,2])
eta <- rep(pi_0,n_j)+rep(pi_1,n_j)*x
p <- exp(eta)/(1+exp(eta))
y <- rbinom(N,1,p)
现在进行参数估计。
#### estimating parameters
library(MASS)
library(nlme)
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")
summary(glmmPQL(Y~X*Z,random=~1|cluster,family=binomial,data=sim_data,,niter=200))
输出:
iteration 1
Linear mixed-effects model fit by maximum likelihood
Data: sim_data
Random effects:
Formula: ~1 | cluster
(Intercept) Residual
StdDev: 0.0001541031 0.9982503
Variance function:
Structure: fixed weights
Formula: ~invwt
Fixed effects: Y ~ X * Z
Value Std.Error DF t-value p-value
(Intercept) -0.8968692 0.2018882 118 -4.442404 0.0000
X 0.5803201 0.2216070 118 2.618691 0.0100
Z 0.2535626 0.2258860 28 1.122525 0.2712
X:Z 0.3375088 0.2691334 118 1.254057 0.2123
Correlation:
(Intr) X Z
X -0.072
Z 0.315 0.157
X:Z 0.095 0.489 0.269
Number of Observations: 150
Number of Groups: 30
它为什么只拿当我提到采取迭代在函数内部迭代由参数?
glmmPQL
niter=200
组级别变量和跨级别交互作用 p值也表明它们并不重要。仍然为什么在本文中,他们保留组级别变量和跨级别交互进行进一步分析?(X :Z )(Z )(X :Z )
还有如何
DF
计算自由度?它与表的各种估计的相对偏差不匹配。我试图将相对偏差计算为:
#Estimated Fixed Effect parameters : hat_g_00 <- -0.8968692 #overall intercept hat_g_10 <- 0.5803201 # X hat_g_01 <-0.2535626 # Z hat_g_11 <-0.3375088 #X*Z fixed <-c(g_00,g_10,g_01,g_11) hat_fixed <-c(hat_g_00,hat_g_10,hat_g_01,hat_g_11) #Estimated Random Effect parameters : hat_s_0 <-0.0001541031 ##Estimated Standard deviation of random intercept hat_s_1 <- 0.9982503 std <- c(sqrt(0.13),1) hat_std <- c(0.0001541031,0.9982503) ##Relative bias of Fixed Effect : rel_bias_fixed <- ((hat_fixed-fixed)/fixed)*100 [1] -10.31308 93.44003 -15.47913 12.50293 ##Relative bias of Random Effect : rel_bias_Random <- ((hat_std-std)/std)*100 [1] -99.95726 -0.17497
- 为什么相对偏差与表格不匹配?
I need to run a large number of simulations and compute averages
。这是否意味着,我必须模拟多级Logistic分布中的数据次并每次估计其参数并取估计的平均值?但是如果我说,根据,估计参数的值是否等于参数的真实值?ë [ θ ] = θ