Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use multi-level model with predictors using ulam? #453

Open
HidekiOkabayashi opened this issue Jan 28, 2025 · 0 comments
Open

How to use multi-level model with predictors using ulam? #453

HidekiOkabayashi opened this issue Jan 28, 2025 · 0 comments

Comments

@HidekiOkabayashi
Copy link

I am very interested in your book, "Statistical Rethinking."
I would like to use ulam for multi-level model with several predictors.

I construct a multi-level model for 420 couples’ marital satisfaction.

There are 420 couples (husband and wife).
Each of two individuals within a couple responded to 3 items of marital satisfaction.
So, 420 couples x 2 (husband and wive) x 3 (items) = 2,520 rows of data were created.
About gender, husband = -0.5, wife = 0.5.

I referred to a univariate Hierarchical Linear Model for dyads (Sayer & Klute, 2005) and tried to construct a following model.

Marital Satisfaction ij = beta 0j + beta 1j * Gender ij + r ij,
beta 0j = gamma 00 + gamma 01 * Marital Hassle + u 0j

i = 6 = 2 * 3 items
j = 420

In ulam, I wrote a following code.
It worked successfully. But, is this model right?

level 1 linear model for within_dyads variation

mu <- (a_bar+v[CoupleID,1]) + (b_bar+v[CoupleID,2])*gender,

level 2 linear model for between-dyads variation

a_bar <- aa_bar + bMH*M_hassle,

Although I thought “a_bar” as a dependent variable of level 2 linear model. If so, it should be a vector [420]. But it was a vector [2520].
But, each consecutive 6 values are the same. So, actually there is 2520 / 6 = 420.

Is this model right or not? I am confused.
I attached the full model and data structure as follows.
If not, would you please tell me how to set multi-level model using ulam?

Thank you very much in advance.

< literature >
Sayer, A.G., & Klute, M. M. (2005). Analyzing Couples and Families: Multilevel Methods. In V. L. Bengston, A. C., Acock, K. R., Allen, Dilworth-Anderson, P., & Klein, D.M. (Eds.) Sourcebook of family theory and research (pp. 289-313).

full model description

multi-level model

m001nc <- ulam(
alist(
# likelihood
M_sat ~ dnorm(mu, sigma),
sigma ~ dexp(1),

# level 1 linear model for within_dyads variation
mu <- (a_bar+v[CoupleID,1]) + (b_bar+v[CoupleID,2])*gender,
  transpars> matrix[420,2]: v <-
  compose_noncentered(sigma_v, L_Rho_v, z_v),
matrix[2, 420]: z_v ~ dnorm(0,1),
vector[2]: sigma_v ~ dexp(1),
cholesky_factor_corr[2]: L_Rho_v ~ lkj_corr_cholesky(2),

# level 2 linear model for between-dyads variation
a_bar <- aa_bar + bMH*M_hassle,

# priors
aa_bar ~ dnorm(0,1),
bMH    ~ dnorm(0,0.5),
b_bar  ~ dnorm(0,0.5),

# generated quantities
gq> matrix[2,2]: Rho_v  <<- Chol_to_Corr(L_Rho_v),
gq> vector[420]: b <<- b_bar+v[,2]
), data=dat_list, chains=4, cores=4, log_lik=T)

data list

dat_list <- list(
CountryID = d5$Country,
CoupleID = d5$CoupleID,
TypeID = d5$Type,
gender = d5$gender,
H_Age = standardize(d5$H_Age),
W_Age = standardize(d5$W_Age),
H_health = standardize(d5$H_health),
W_health = standardize(d5$W_health),
income = standardize(d5$income),
M_hassle = standardize(d5$M_hassle),
item = d5$item,
M_sat = standardize(d5$M_sat)
)

data structure

List of 12
$ CountryID: int [1:2520] 1 1 1 1 1 1 1 1 1 1 ...
$ CoupleID : int [1:2520] 1 1 1 1 1 1 2 2 2 2 ...
$ TypeID : int [1:2520] 4 4 4 4 4 4 5 5 5 5 ...
$ gender : num [1:2520] -0.5 -0.5 -0.5 0.5 0.5 0.5 -0.5 -0.5 -0.5 0.5 ...
$ H_Age : num [1:2520] 0.852 0.852 0.852 0.852 0.852 ...
$ W_Age : num [1:2520] 0.558 0.558 0.558 0.558 0.558 ...
$ H_health : num [1:2520] -1.36 -1.36 -1.36 -1.36 -1.36 ...
$ W_health : num [1:2520] 1.95 1.95 1.95 1.95 1.95 ...
$ income : num [1:2520] 1.61 1.61 1.61 1.61 1.61 ...
$ M_hassle : num [1:2520] 0.719 0.719 0.719 0.719 0.719 ...
$ item : int [1:2520] 1 2 3 1 2 3 1 2 3 1 ...
$ M_sat : num [1:2520] -0.195 1.004 1.004 1.004 1.004 ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant