mkshaw / r2mlm

r2mlm: R Package for Calculating R-Squared in Multilevel Models
27 stars 1 forks source link

Comparability of R2 Values in MLMs with Time as a Factor vs. Time as a Numeric Variable #65

Open LudvigWR opened 1 year ago

LudvigWR commented 1 year ago

Hi,

I'm currently trying to calculate R2 for my three-level MLM using the r2mlm3_manual() function. The problem I have is that one of my predictors, "Time" (Pre, Post & Follow up measurements measured at different time intervals), needs to be a factor variable for interpretative reasons.

My question is (and I realize this might be a bit far fetched): If I change "Time" into a numeric variable, will the output from r2mlm3_manual() be comparable in any way to my original MLM where "Time" is a factor? In other words, will the output from r2mlm3_manual() provide additional explanatory value, or will it simply describe a "different" model?

I'm aware of how "Time" as a numerical variable changes the MLM summary results (but not how this relates to the R2s).

My model looks something like this: lmer(Outcome ~ A* Time + B + C + (1 | Group/Subject_ID), data = long_data, REML = FALSE)

The interaction term A*Time is the focus of my research question.

Many thanks, Ludvig

mkshaw commented 1 year ago

Hi Ludvig,

The r2mlm function doesn't "know" what your variables are, it just looks at the variance of your outcome explained by your different predictors/random effects. Is your plan here to re-run your MLM with time as a numeric variable, or to run it with time as a factor and then just change the column "time" to be numeric only for r2mlm3_manual?

If you run your model with time as a numeric variable and your results change, and you use those different results in r2mlm, you'll get different R2 values than you would if you ran the model with time as a factor.

If you run your model with time as a factor and use those results for r2mlm, but just change the column in the dataset to be numeric when using r2mlm (so your MLM results haven't changed at all, time is still a factor for those results), that shouldn't affect the R2 values.

Is what I'm saying clear?

Mairead

LudvigWR commented 1 year ago

Hi Mairead,

Thank you for your reply.

I ran the MLM now with time as a factor variable, and then tried to input this data into r2mlm3_manual. The problem I run into is with the input of the estimates in gamma_1. With time as a factor in the MLM you get two estimates for the variable time (one for post, and one for follow up).

Is this fixable you think, or does this mean it might not be possible to use r2mlm3_manual in my case? (since I need to keep time as a factor variable)

Summary(model) ... Time(post): 5 Time (follow up): 12 ...

r2mlm3_manual( ... l1_covs = c("Time"), ... gamma_1 = c(?), ...)

/Ludvig