rrandomlme4lmertest

rand() from lmerTest: how to deal with missing values?


I am working with a large dataset and analyzing a continuous dependent variable with a linear mixed effects model using the R package lme4. I am also using the extension lmerTest, which allows to compute various plots and the p-values associated with fixed and random terms.

When I run the rand() to obtain a p-value associated with each random term, I obtain the following error:

Error in anova.merMod(object = object, ... = ...) : models were not all fitted to the same size of dataset

This is because one of my random terms includes missing values, while others don't.

My question is: within the rand function, how can I deal with differences in dataset sizes? Is there an argument that allows to automatically omit NAs? I tried to look at the help page for that function but the documentation is very limited.

Thanks!


Solution

  • Here's an example using the data example from the lmerTest package. In the example we wish to run this code

    library(lmerTest)
    m <- lmer(Preference ~ sens2+Homesize+(1+sens2|Consumer), data=carrots)
    rand(m)
    

    First we identify which variables are available for the largest of the models. I use the pipe and functions from tidyverse below but you could do the same with with. All the variable from the full model should be included here

    cc <- carrots %>% select(Preference, sens2, Homesize, Consumer) %>% complete.cases()
    

    cc now contains a vector of logicals with the rows that contain full sets of observations. Those are the ones we should use throughout the analyses. We make sure this is true by adding the subset argument

    m <- lmer(Preference ~ sens2+Homesize+(1+sens2|Consumer), subset=cc, data=carrots)