American-Institutes-for-Research / WeMix

WeMix public repository
GNU General Public License v2.0
10 stars 2 forks source link

Conditional and marginal R2 from WeMix #16

Closed joaomaroco closed 5 months ago

joaomaroco commented 5 months ago

Hi there, Is there an easy way to get Conditional R2 and Marginal R2 for a 3-level HLM using WeMix? I would need the Variance of the fixed effects plus the variation of random effects at the two upper levels. Thanks!

pdbailey0 commented 5 months ago

@joaomaroco the variance of the fixed effects is in the documentation for mix

library(lme4)

data(sleepstudy)
ss1 <- sleepstudy

# Create weights
ss1$W1 <- ifelse(ss1$Subject %in% c(308, 309, 310), 2, 1)
ss1$W2 <- 1

# Run random-intercept 2-level model 
two_level <- mix(Reaction ~ Days + (1|Subject), data=ss1, weights=c("W1", "W2"))
two_level$cov_mat
#             (Intercept)      Days
# (Intercept)   40.178710 -2.535354
# Days          -2.535354  3.295401

for the variance of the random effects, that's requested in this issue.

Are you also requesting these R2 values? Do you have a citation that shows how they are calculated in the weighted case, or even in an unweighted case that I could look at?

joaomaroco commented 5 months ago

Thanks for the prompt repply Paul,

There's a package in R that does that from lme4 output: https://cran.r-project.org/web/packages/MuMIn/MuMIn.pdf , see p. 57 for the formulae.

I wonder, since WeMix also uses lmer4, if the adaptation is quite straightfoward.

Thanks again!

Warm regards, João


From: Paul Bailey @.> Sent: Wednesday, April 3, 2024 4:01 PM To: American-Institutes-for-Research/WeMix @.> Cc: João Marôco @.>; Mention @.> Subject: Re: [American-Institutes-for-Research/WeMix] Conditional and marginal R2 from WeMix (Issue #16)

@joaomarocohttps://www.google.com/url?q=https://github.com/joaomaroco&source=gmail-imap&ust=1712754119000000&usg=AOvVaw2M9Hwuqix4kUPRrnIiq6hF the variance of the fixed effects is in the documentation for mix

library(lme4)

data(sleepstudy) ss1 <- sleepstudy

Create weights

ss1$W1 <- ifelse(ss1$Subject %in% c(308, 309, 310), 2, 1) ss1$W2 <- 1

Run random-intercept 2-level model

two_level <- mix(Reaction ~ Days + (1|Subject), data=ss1, weights=c("W1", "W2")) two_level$cov_mat

(Intercept) Days

(Intercept) 40.178710 -2.535354

Days -2.535354 3.295401

for the variance of the random effects, that's requested in this issuehttps://www.google.com/url?q=https://github.com/American-Institutes-for-Research/WeMix/issues/15&source=gmail-imap&ust=1712754119000000&usg=AOvVaw1O3_Sdv1xq5VkjhGQSr2GJ.

Are you also requesting these R2 values? Do you have a citation that shows how they are calculated in the weighted case, or even in an unweighted case that I could look at?

— Reply to this email directly, view it on GitHubhttps://www.google.com/url?q=https://github.com/American-Institutes-for-Research/WeMix/issues/16%23issuecomment-2034545235&source=gmail-imap&ust=1712754119000000&usg=AOvVaw2C_gGYkTKtjSZmyNardtX5, or unsubscribehttps://www.google.com/url?q=https://github.com/notifications/unsubscribe-auth/BERQLLG7K2HZPGKZ7FC2YADY3P4ULAVCNFSM6AAAAABFVIERXWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZUGU2DKMRTGU&source=gmail-imap&ust=1712754119000000&usg=AOvVaw12evcVlMxUZPmGhldfd1Y9. You are receiving this because you were mentioned.Message ID: @.***>

pdbailey0 commented 5 months ago

This looks doable with what we currently return. Feel free to make a pull request with this improvement.