'Simulating hierarchical data (nested structure), containing categorical variable
I am simulating dyadic data, which is cluster size is fixed to two.
I have continuous IV (e.g.,education score) and categorical variable (e.g., gender)
these two variable is level-1 variable.
target ICC is 0.3 (ICC=between cluster variable / (within+ between))
so, when I creat education, it was fine. what I did is,
genderating between-cluster column, and within-cluster column for member 1, and within-cluster column for member 2.
so, education for member 1= ICC weight * between cluster + icc weight * within 1 member 2= ICC weight *between cluster + iccc weight * within 2
cn <- 100 #number of pairs
mu <-c(0,0,0,0)
vcovmatrix <- matrix(c(1,0,0,0,
0,1,0,1,
0,0,1,0,
0,1,0,1),
nrow = 4,
byrow = TRUE,
dimnames = list(c("withn_1","btw_1","withn_2","btw_2"),
c("withn_1","btw_1","withn_2","btw_2")))
dat<-mvrnorm(cn, # sample size
mu=mu, # Mu
Sigma = vcovmatrix) # Covariance matrix
dat<-as.data.frame(dat)
dat$pid<-1:cn # pair ID (group ID)
dat%>%
dplyr::select(c(withn_1,btw_1,withn_2,btw_2))%>%
cor()
## Let's say that ICC is 0.3, This means that between:within ratio is 3:7.
## 3/(7+3)=0.3
withn_weight=sqrt(0.3)
btw_weight=sqrt(0.7)
dat <- dat %>%
mutate(
edu_1=withn_weight*withn_1+btw_weight*btw_1,
edu_2=withn_weight*withn_2+btw_weight*btw_2
)
However, I cannot easily envisioning how I can creat gender.
gender have two levels.
as I did in the education,
I can create between-cluster effect in gender, and within_1 and within_2
and gender_1 <-ICCweightgenderbetween + Iccweightgenderwithin1 gender_2<-ICCweightgnederbetween+ICCweightgenderwithin2
but gender is categorical variable, so how I can create my gender variable?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
