Defining lmer interaction terms in workflow recipes

Using lme4 package for recipe creation of variables but I haven't been able to figure out how to include interaction terms along with random effects. The model I'd like to cross-validate is:
DifferentEnd ~ Drug*Putamen_ki.c + Session + (1 | ID) but all models fail when I try to fit samples.

Below is my code:

library(multilevelmod) # Installed current devel version

options(contrasts = c("contr.sum", "contr.poly"))

set.seed(123) # set seed of RNG for reproducibility
resampled_data <- SelectData %>%
 rsample::vfold_cv(v = 10, repeats = 100)

lme_spec <-
  linear_reg() %>%
  set_mode("regression") %>%

lme_wflow_2 <-
  workflow() %>%
    recipe(DifferentEnd ~ Drug + Putamen_ki.c + Session + ID, data = SelectData) %>%
      step_ns(Drug, deg_free = tune()) %>%
      step_ns(Putamen_ki.c, deg_free = tune()) %>%
      step_ns(Session, deg_free = tune()) %>%
  ) %>%
  add_model(lme_spec, formula = DifferentEnd ~ Drug:Putamen_ki.c + Session + (1 | ID))

fit_lm <- lme_wflow_2 %>%
    resamples = resampled_data,
    metrics = performance_metrics,
    control = tune::control_resamples(save_pred = TRUE)

Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.