Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tidymodels/multilevelmod

Parsnip wrappers for mixed-level and hierarchical models
https://github.com/tidymodels/multilevelmod

Last synced: 9 days ago
JSON representation

Parsnip wrappers for mixed-level and hierarchical models

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r}
#| include: false
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# multilevelmod 3 nesting dolls on an orange background

[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![Codecov test coverage](https://codecov.io/gh/tidymodels/multilevelmod/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidymodels/multilevelmod?branch=main)
[![R-CMD-check](https://github.com/tidymodels/multilevelmod/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidymodels/multilevelmod/actions/workflows/R-CMD-check.yaml)

multilevelmod enables the use of multi-level models (a.k.a mixed-effects models, Bayesian hierarchical models, etc.) with the parsnip package.

A sweaty Comic character trying to decide which button to push. The buttons read: 'mixed effect model', 'hierarchical linear model', 'random effects model', 'variance component model', 'mixed model', 'random intercepts/slopes', 'regularized regression', 'multilevel model', 'nested data model', and 'random parameter model'

(meme courtesy of [`@ChelseaParlett`](https://twitter.com/ChelseaParlett))

## Installation

You can install the released version of multilevelmod from [CRAN](https://cran.r-project.org) with:

``` r
install.packages("multilevelmod")
```

For the development version:

``` r
# install.packages("pak")
pak::pak("tidymodels/multilevelmod")
```

## Available Engines

The multilevelmod package provides engines for the models in the following table.

```{r}
#| 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()
```

## Example

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:

```{r}
#| 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:

```{r}
#| 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
```

## Contributing

This project is released with a [Contributor Code of Conduct](https://www.contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). 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](https://community.rstudio.com/new-topic?category_id=15&tags=tidymodels,question).

- If you think you have encountered a bug, please [submit an issue](https://github.com/tidymodels/multilevelmod/issues).

- Either way, learn how to create and share a [reprex](https://community.rstudio.com/new-topic?category_id=15&tags=tidymodels,question) (a minimal, reproducible example), to clearly communicate about your code.

- Check out further details on [contributing guidelines for tidymodels packages](https://www.tidymodels.org/contribute/) and [how to get help](https://www.tidymodels.org/help/).