I have written R
codes for simulating data from Multilevel logistic regression model .
I focus on the following multilevel logistic model with one explanatory variable at level 1 (individual level) and one explanatory variable at level 2 (group level) :
$$\text{logit}(p_{ij})=\pi_{0j}+\pi_{1j}x_{ij}\ldots (1)$$ $$\pi_{0j}=\gamma_{00}+\gamma_{01}z_j+u_{0j}\ldots (2)$$ $$\pi_{1j}=\gamma_{10}+\gamma_{11}z_j+u_{1j}\ldots (3)$$
where , $u_{0j}\sim N(0,\sigma_0^2)$ , $u_{1j}\sim N(0,\sigma_1^2)$ , $\text{cov}(u_{0j},u_{1j})=\sigma_{01}$
In this paper in equation (2) , they assumed $\text{cov}(u_{0j},u_{1j})=\sigma_{01}$ , that is not independent . But also they mentioned in the methodology section that :
The group random components $u_{0j}$ and $u_{1j}$ are "independent" normal variables with mean zero and standard deviations $σ_0$ and $σ_1$.
So I assumed $\text{cov}(u_{0j},u_{1j})=0$ .
R code :
## Simulating data from multilevel logistic regression
set.seed(1234)
x <- rnorm(1000) ### individual level variable
z <- rnorm(1000) ### group level variable
##fixed effect parameter
g_00 <- -1
g_01 <- 0.3
g_10 <- 0.3
g_11 <- 0.3
g <- matrix(c(g_00,g_01,g_10,g_11),ncol=1)
require(mvtnorm)
##need variance values as input
s2_0 <- 0.36
s2_1 <- 1
s01 <- 0
##generate bi-variate normal rv for u0, u1
avg <- c(0,0) ##mean
sigma <- matrix(c(s2_0,s01,s01,s2_1),ncol=2)
u <- rmvnorm(1000,mean=avg,sigma=sigma,method="chol")
pi_0j <- g_00 +g_01*z + as.vector(u[,1])
pi_1j <- g_10 +g_11*z + as.vector(u[,2])
p <- exp(pi_0j+pi_1j*x)/(1+exp(pi_0j+pi_1j*x))
y <- rbinom(1000,1,p)
But i am not understanding where is to consider the group ? If i select number of groups to be $100$ $(j=1,2,\ldots, 100)$, then will I assign the groups randomly against each $y_{i,j}$ ?
- Have i correctly simulated data from
Multilevel Logistic Distribution
?