tidymodels / tidymodels/multilevelmod
Incorrect predictions from fit_resamples() when applied to lmer model
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 74
- Forks
- 6
- Avg merge
- 1h 53m
- Merged PRs (30d)
- 1
Description
I am having trouble using the tune::fit_resamples() function on a lmer model (from the multilevelmod package).
In particular, it looks like that when the predictions for the assessment set are calculated, the model doesn't properly account for all the possible combinations of grouping levels.
I have included a reprex in which I show that the results of a predict() call on a lmer object are different than the predictions obtained from a fit_resamples() call (using collect_predictions()).
library(tidyverse)
library(tidymodels)
library(multilevelmod)
data(mpg, package = "ggplot2")
set.seed(123)
lmer_model = linear_reg() %>%
set_engine("lmer")
lmer_workflow = workflow() %>%
add_variables(outcomes = cty,
predictors = c(year, manufacturer, model)) %>%
add_model(lmer_model, formula = cty ~ year + (1|manufacturer/model))
mpg_split = mpg %>% validation_split(prop = 3/4)
analysis = mpg_split$splits[[1]] %>% analysis()
assessment = mpg_split$splits[[1]] %>% assessment()
# using predict() on the assessment dataset works as expected
predicted_via_workflow = lmer_workflow %>%
fit(analysis) %>%
extract_fit_engine() %>%
predict(assessment) %>%
plot()

# the predictions from the fit_resamples() function do not vary per group
predicted_via_tune = lmer_workflow %>%
fit_resamples(mpg_split, control = control_resamples(allow_par = FALSE,
save_pred = TRUE)) %>%
collect_predictions() %>%
pluck(".pred") %>%
plot()

Created on 2022-06-20 by the reprex package (v2.0.1)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the discrepancy between predict() on the fitted lmer engine and fit_resamples() followed by collect_predictions() using the supplied mpg workflow and validation split. Trace how assessment-set predictions handle grouping-level combinations; done when fit_resamples() produces group-varying predictions consistent with direct predict() results.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100