#| include: false knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
multilevelmod enables the use of multi-level models (a.k.a mixed-effects models, Bayesian hierarchical models, etc.) with the parsnip package.
(meme courtesy of @ChelseaParlett
)
You can install the released version of multilevelmod from CRAN with:
install.packages("multilevelmod")
For the development version:
# install.packages("pak") pak::pak("tidymodels/multilevelmod")
The multilevelmod package provides engines for the models in the following table.
#| echo: false #| message: false library(parsnip) parsnip_models <- get_from_env("models") %>% setNames(., .) %>% purrr::map_dfr(get_from_env, .id = "model") library(multilevelmod) multilevelmod_models <- get_from_env("models") %>% setNames(., .) %>% purrr::map_dfr(get_from_env, .id = "model") dplyr::anti_join( multilevelmod_models, parsnip_models, by = c("model", "engine", "mode") ) %>% knitr::kable()
Loading mixedlevelmod will trigger it to add a few modeling engines to the parsnip model database. For Bayesian models, there are now stan-glmer
engines for linear_reg()
, logistic_reg()
, and poisson_reg()
.
To use these, the function parsnip::fit()
function should be used instead of parsnip::fit_xy()
so that the model terms can be specified using the lme
/lme4
syntax.
The sleepstudy
data is used as an example:
#| label: sleep-lme library(multilevelmod) set.seed(1234) data(sleepstudy, package = "lme4") mixed_model_spec <- linear_reg() %>% set_engine("lmer") mixed_model_fit <- mixed_model_spec %>% fit(Reaction ~ Days + (Days | Subject), data = sleepstudy) mixed_model_fit
For a Bayesian model:
#| label: sleep-stan hier_model_spec <- linear_reg() %>% set_engine("stan_glmer") hier_model_fit <- hier_model_spec %>% fit(Reaction ~ Days + (Days | Subject), data = sleepstudy) hier_model_fit
This project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
For questions and discussions about tidymodels packages, modeling, and machine learning, please post on RStudio Community.
If you think you have encountered a bug, please submit an issue.
Either way, learn how to create and share a reprex (a minimal, reproducible example), to clearly communicate about your code.
Check out further details on contributing guidelines for tidymodels packages and how to get help.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.