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(tidyverse)
library(tidymodels)
library(multilevelmod) # Installed current devel version
library(lme4)
tidymodels::tidymodels_prefer()
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)
head(resampled_data)
lme_spec <-
linear_reg() %>%
set_mode("regression") %>%
set_engine("lmer")
lme_wflow_2 <-
workflow() %>%
add_recipe(
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()) %>%
step_novel(ID)
) %>%
add_model(lme_spec, formula = DifferentEnd ~ Drug:Putamen_ki.c + Session + (1 | ID))
fit_lm <- lme_wflow_2 %>%
tune::fit_resamples(
resamples = resampled_data,
metrics = performance_metrics,
control = tune::control_resamples(save_pred = TRUE)
)