rpredictmixed-modelslasso-regressionglmm

Predicting on a glmmLasso model


I'm trying to use the glmmLasso pacakage and am able to run a model and get a summary output and extract the fitted values, but I'm wondering if/how I can make predictions with it?

Am I missing a step?

For example:

library(glmmLasso)
library(tidyverse)

mt_tbl <- mtcars %>% as_tibble() %>% 
  mutate(cyl = factor(cyl))

glm_cars <- glmmLasso(mpg ~  hp + drat + wt,
                        data=mt_tbl,
                        rnd = list(cyl=~1),
                        family = gaussian(link = "identity"),
                        lambda = .9,
                        switch.NR = TRUE,
                        final.re = TRUE)

# These work
summary(glm_cars)
glm_cars$fitted.values

# I want this to work
predict(glm_cars, mt_tbl)


Solution

  • If you do not convert mtcars to tibble, the code works fine with data.frame like

    library(glmmLasso)
    library(tidyverse)
    
    #Convert cyl to factor
    mtcars$cyl <- factor(mtcars$cyl)
    
    #Run the model using 'data.frame'
    glm_cars <- glmmLasso(mpg ~  hp + drat + wt,
                          data=mtcars,
                          rnd = list(cyl=~1),
                          family = gaussian(link = "identity"),
                          lambda = .9,
                          switch.NR = TRUE,
                          final.re = TRUE)
    
    # These work
    summary(glm_cars)
    glm_cars$fitted.values
    
    # Predict using 'data.frame' works
    predict(glm_cars, mtcars)