mastoffel / partR2

R package to partition R2 among predictors in Generalized linear mixed models
Other
21 stars 3 forks source link

Three levels of nesting: Error in "mismatch" of number of rows to rows in data #9

Closed emurbanw closed 1 year ago

emurbanw commented 1 year ago

Hello! I am running a 3-level mixed model, where timepoint (1 or 2) is nested within participant (Long_ID), which is nested within family (Family_ID). Some participants have both timepoints of data, some have only one. Furthermore, some participants are the only ones in their families with data, some have multiple siblings with data.

When I run the same model just choosing 1 timepoint of data (i.e., there are only 2 levels in my nested model), there is no problem when I run partR2. But when I run using the 3-level model I get the error "Error in [<-.data.frame(*tmp*, , rcol, value = c(0.595482973393883, : replacement has 616 rows, data has 807". I'm guessing the problem is in the missing data that some families or individuals have.

My lme4 model looks like this:

model <- lmer(Outcome ~ IV + Age + Age_sq + Sex2 + Timepoint + (1|Family_ID) + (1|Long_ID), data = Long_data)

I then run the following code:

R2_model <- partR2(model, partvars=c("IV" + "Age" + "Age_sq" + "Sex2" + Timepoint), max_level = 1, R2_type="marginal", nboot = 10, data = BPI_long)

I wonder if I am doing something wrong or if partR2 is not equipped for more than 2 levels of nesting? Thank you in advance for any assistance you may be able to provide!

Emily

mastoffel commented 1 year ago

Hi Emily, thanks for that! lme4 will remove rows which contain NA's in any of the variables in the model anyway. Can you try removing any rows containing NA's before using partR2 and then re-run?

emurbanw commented 1 year ago

Thanks for the quick reply! I did not realize lme4 was doing that. Removing rows with NAs did the trick and the model output was identical. Thanks very much for the insight!

Emily