Comments (4)
Thanks for including an example! I can't run it, though, so this is a bit of a general reply: if your feature selection step changes the names of the predictors, e.g., through transformations, you can't use the original predictor name in the model formula (that's the one that you use in add_model()
) because they will not be there anymore after the preprocessing.
You could use the dot notation in the formula, i.e., something similar to Disease ~ . + (1|ID) - ID
. You'd need to ensure only variables you want to use in the model are left after preprocessing and remove the fixed effect for the ID
variable. Here is an illustration of that idea
library(tidymodels)
library(multilevelmod)
data(sleepstudy, package = "lme4")
# we want to use the formula Reaction ~ Days + (1|Subject)
lmer_spec <-
linear_reg() %>%
set_engine("lmer")
# recipe here without any further preprocessing/feature engineering
# because the data already only contains the 3 variables we are going to use
rec <- recipe(Reaction ~ ., sleepstudy)
wflow <- workflow() %>%
add_recipe(rec) %>%
add_model(lmer_spec, formula = Reaction ~ . -Subject + (1|Subject))
fit(wflow, data = sleepstudy)
#> ══ Workflow [trained] ══════════════════════════════════════════════════════════
#> Preprocessor: Recipe
#> Model: linear_reg()
#>
#> ── Preprocessor ────────────────────────────────────────────────────────────────
#> 0 Recipe Steps
#>
#> ── Model ───────────────────────────────────────────────────────────────────────
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: Reaction ~ . - Subject + (1 | Subject)
#> Data: data
#> REML criterion at convergence: 1786.465
#> Random effects:
#> Groups Name Std.Dev.
#> Subject (Intercept) 37.12
#> Residual 30.99
#> Number of obs: 180, groups: Subject, 18
#> Fixed Effects:
#> (Intercept) Days
#> 251.41 10.47
# same fit as with Reaction ~ Days + (1|Subject)
lmer_spec %>%
fit(Reaction ~ Days + (1|Subject), data = sleepstudy)
#> parsnip model object
#>
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: Reaction ~ Days + (1 | Subject)
#> Data: data
#> REML criterion at convergence: 1786.465
#> Random effects:
#> Groups Name Std.Dev.
#> Subject (Intercept) 37.12
#> Residual 30.99
#> Number of obs: 180, groups: Subject, 18
#> Fixed Effects:
#> (Intercept) Days
#> 251.41 10.47
Created on 2024-01-24 with reprex v2.0.2
from multilevelmod.
Hi hfrick,
Thanks for your answer!
I tried it and the fit function is working with my dataset and workflow (as your example).
However, then when I use augment or predict I got the following error:
fitted_model <-
fit(
current_workflow,
data = data_training
)
prediction_fold <- fitted_model %>%
augment(new_data_fold)
Error in terms.formula(ff) : '.' in formula and no 'data' argument
I tried this but I get the same: fitted_model %>%
augment(data=data_training, new_data=new_data_fold)
Thanks again
Best
Alberto
from multilevelmod.
I can't help you with this one without a proper reprex. Please check out the reprex package for easily making those and the article on dos and donts. Github issues are best used for bug reports and feature requests; for general help in how to get a piece of code to run, Posit Community is the best place, also because more people see your question there and can chime in.
from multilevelmod.
Thank a lot for your support! I will see if in Posit Community I can get some tips as well.
Best
Alberto
from multilevelmod.
Related Issues (20)
- Installation of multilevelmod fails HOT 4
- Release multilevelmod 0.2.0 HOT 1
- bug in when using there is mismatch between number of labels in training and testing data set. HOT 4
- Require glmmLasso model HOT 2
- Release multilevelmod 1.0.0 HOT 1
- Incorrect predictions from fit_resamples() when applied to lmer model HOT 2
- New model request: gpboost Tree-Boosting with Gaussian Process and Mixed Effects Models HOT 3
- extracting parameter sets of MCMC iterations from `model_fit` object HOT 3
- New model request: glmmLasso HOT 1
- New model request: Mixed effect random forest (MERF) HOT 6
- Are multinom_reg() models supported? HOT 5
- Upkeep for multilevelmod 2023 HOT 1
- Use cli errors
- Default prediction mode for lmer - re.form = NA HOT 3
- Add issue templates HOT 1
- Use rlang type-checkers
- `logistic_reg(engine = "gee")` doesn't support dot in formula
- New Model Request: a spatial multilevel model HOT 1
- Linear Quantile Mixed Model (lqmm) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from multilevelmod.