Search code examples
rtidyversepredictionlme4

estimate predicted values from multiple lme4 objects


I am running a mixed effects model on my dataset ,

library(lme4)
data(cake)

each dataset is a subset of a larger dataset

subset(cake, recipe=="A")
subset(cake, recipe=="B")
subset(cake, recipe=="C")

I am using dlply to run my mixed effects model on each subset

MxM1 <- plyr::dlply(cake,
                    "recipe",
                    function(x)
                      lmer(angle ~ 1 + (1|replicate) + temperature,
                           data=x))

How do I estimate the predicted value ?

I tried the ldply function,

cake$pred <- ldply(MxM1, as.data.frame(predict))$value

but I am not sure if the estimated predicted values are correctly merged with the observation in the dataset. Because if this my dataset

    head(cake)
       replicate       recipe      temperature    angle    
    1  1               A           175            42       
    2  1               A           185            46       
    3  1               A           195            47       
    4  1               A           205            39       
    5  1               A           215            53       
    6  1               A           225            42       

I like the predicted value to match the observation for which it is predicting the value, and I am not confident if the output from ldply(MxM1, as.data.frame(predict))$value aligns accurately with the observations in the dataset. In other words my worry is the output from ldply(MxM1, as.data.rame(predict))$value might be out of sequence with the observations in the dataset. Any suggestions how to resolve this issue and add the predicted value accurately back into the dataset is much appreciated.


Solution

  • I'm not sure if it's correct to calculate separate models for the recipes, but you may have your reason.

    You can fit and predict by recipe in one step. If you use na.action=na.exclude you can directly cbind the predictions.

    res <- by(cake, cake$recipe, \(x) {
      fit <- lmer(angle ~ (1 | replicate) + temperature, data=x, na.action=na.exclude)
      cbind(x, yhat=predict(fit, na.action=na.exclude))
    }) |> do.call(what=rbind)
    
    head(res)
    #     replicate recipe temperature angle temp     yhat
    # A.1         1      A         175    42  175 39.40195
    # A.2         1      A         185    46  185 41.80195
    # A.3         1      A         195    47  195 41.06861
    # A.4         1      A         205    39  205 43.80195
    # A.5         1      A         215    53  215 48.93528
    # A.6         1      A         225    42  225 45.33528