American-Institutes-for-Research / WeMix

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

New function to calculate conditional and marginal R2 for MixPV /WeMix #17

Open joaomaroco opened 4 months ago

joaomaroco commented 4 months ago

Hi Paul et al

I developed a function to calculate marginal R2 and conditional R2 from mixPV function (https://doi.org/10.1186/s40536-024-00192-0 ) on WeMix. For the r2 formulae I am using https://doi.org/10.1111/j.2041-210x.2012.00261.x eq 26 and eq 27. I am not sure I am getting the variance of fixed effects correctly, but here is a first trial:

r2 <- function(m1,df) {
  TotV <- numeric(length(m1))
  VarRcnt <- numeric(length(m1))
  VarRsch <- numeric(length(m1))
  VarF <- numeric(length(m1))
  R2cnt <- numeric(length(m1))
  R2sch <- numeric(length(m1))
  R2marg <- numeric(length(m1))
  R2cond <- numeric(length(m1))
  CoefsF <- vector()
  yFit <- vector()

  for (i in 1:length(m1)) {

    CoefsF <- m1[[i]]$coef[-1]

    X <- dplyr::select(df, all_of(names(CoefsF[i])))|>as.matrix()
    wX <- X*df$nwt
    yFit <- as.vector(wX%*%CoefsF)
    VarF[i] <- var(yFit, na.rm=T)

    TotV[i] <- sum(m1[[i]]$varDF[4])+VarF[i]
    VarRcnt[i] <- m1[[i]]$varDF[1,4]
    VarRsch[i] <- m1[[i]]$varDF[2,4]
    #VarF[i] <- sum(diag(m1[[i]]$cov_mat[-1,-1]))

    R2cnt[i] <- VarRcnt[i]/TotV[i]
    R2sch[i] <- VarRsch[i]/TotV[i]
    R2marg[i] <- VarF[i]/TotV[i]
    R2cond[i] <- (VarF[i]+VarRcnt[i]+VarRsch[i])/TotV[i]
  }

  R2m <- round(mean(R2marg), 3)
  R2c <- round(mean(R2cond), 3)

  Rdf <- data.frame(R2m, R2c)

  return(Rdf)
}

m1 is a wemix results object, and df is the original dataframe. R2m is the marginal R2, and R2c is the condtional.

pdbailey0 commented 4 months ago

@joaomaroco you can test it against that package you shared. The most basic test is when all the weights are one, lmer should agree with mix.

joaomaroco commented 4 months ago

lmer only allows weights at level 1... I guess level 2 and level 3 weights in WeMix will change the variance estimates for the random effects...

SarahElgerby commented 2 months ago

can any one help me with mixPV Im working with TIMMS data not Pisa

joaomaroco commented 2 months ago

Hi Sara, What kind of help do you need? Best regards, João Marôco

A segunda, 1/07/2024, 01:35, SarahElgerby @.***> escreveu:

can any one help me with mixPV Im working with TIMMS data not Pisa

— Reply to this email directly, view it on GitHub https://github.com/American-Institutes-for-Research/WeMix/issues/17#issuecomment-2198833849, or unsubscribe https://github.com/notifications/unsubscribe-auth/BERQLLAY6N43GI7AGDDMSCDZKCP55AVCNFSM6AAAAABF6AFYOOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJYHAZTGOBUHE . You are receiving this because you were mentioned.Message ID: @.***>

pdbailey0 commented 1 month ago

what package is mixPV in?

joaomaroco commented 1 month ago

Hi Paul, MixPV uses your package WeMix... see       Huang, F. L. (2024). Using plausible values when fitting multilevel models with large-scale assessment data using R. \textit{Large-scale Assessments in Education, 12}(7). https://doi.org/10.1186/s40536-024-00192-0

Best regards, Jº


From: Paul Bailey @.> Sent: Monday, July 1, 2024 4:00 PM To: American-Institutes-for-Research/WeMix @.> Cc: João Marôco @.>; Mention @.> Subject: Re: [American-Institutes-for-Research/WeMix] New function to calculate conditional and marginal R2 for MixPV /WeMix (Issue #17)

what package is mixPV in?

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