-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
92 lines (65 loc) · 2.72 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# nestedmodels <img src="man/figures/logo.png" align="right" height="139" />
<!-- badges: start -->
[![Project Status: Active](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R-CMD-check](https://github.com/ashbythorpe/nestedmodels/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ashbythorpe/nestedmodels/actions/workflows/R-CMD-check.yaml)
[![CRAN status](https://www.r-pkg.org/badges/version/nestedmodels)](https://CRAN.R-project.org/package=nestedmodels)
[![Codecov test coverage](https://codecov.io/gh/ashbythorpe/nestedmodels/branch/main/graph/badge.svg)](https://app.codecov.io/gh/ashbythorpe/nestedmodels?branch=main)
<!-- badges: end -->
The goal of nestedmodels is to allow the modelling of nested data.
Some models only accept certain predictors. For panel data, it is often desirable to create a model for each panel. nestedmodels enhances the '[tidymodels](https://www.tidymodels.org/)' set of packages by allowing the user to classify a model as 'nested'.
## Installation
``` r
# Install the released version on CRAN
install.packages("nestedmodels")
# Or install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("ashbythorpe/nestedmodels")
```
## Example
```{r setup}
library(nestedmodels)
```
Nested models are often best used on panel data.
```{r data}
data <- example_nested_data
nested_data <- tidyr::nest(example_nested_data, data = -id)
nested_data
```
The `nested_resamples()` function makes sure that the testing and training data
contain every unique value of 'id'.
```{r}
split <- nested_resamples(nested_data, rsample::initial_split())
data_tr <- rsample::training(split)
data_tst <- rsample::testing(split)
```
Fitting a nested model to this data is very simple.
```{r}
model <- parsnip::linear_reg() %>%
nested()
fit <- fit(model, z ~ x + y + a + b,
tidyr::nest(data_tr, data = -id))
predict(fit, data_tst)
```
If you don't want to nest your data manually, use `step_nest()` inside a workflow:
```{r}
recipe <- recipes::recipe(data_tr, z ~ x + y + a + b + id) %>%
step_nest(id)
wf <- workflows::workflow() %>%
workflows::add_model(model) %>%
workflows::add_recipe(recipe)
wf_fit <- fit(wf, data_tr)
predict(wf_fit, data_tst)
```
Please note that the nestedmodels project is released with a [Contributor Code of Conduct](https://ashbythorpe.github.io/nestedmodels/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.