# In SMAC-Group/simts: Time Series Analysis Tools

```knitr::opts_chunk\$set(
collapse = TRUE,
comment = "#>"
)
```
```library(tidyr)
library(dplyr)
library(purrr)
```

## Basics

A nested data frame is a data frame where one (or more) columns is a list of data frames. You can create simple nested data frames by hand:

```df1 <- tibble(
g = c(1, 2, 3),
data = list(
tibble(x = 1, y = 2),
tibble(x = 4:5, y = 6:7),
tibble(x = 10)
)
)

df1
```

(It is possible to create list-columns in regular data frames, not just in tibbles, but it's considerably more work because the default behaviour of `data.frame()` is to treat lists as lists of columns.)

But more commonly you'll create them with `tidyr::nest()`:

```df2 <- tribble(
~g, ~x, ~y,
1,  1,  2,
2,  4,  6,
2,  5,  7,
3, 10,  NA
)
df2 %>% nest(data = c(x, y))
```

`nest()` specifies which variables should be nested inside; an alternative is to use `dplyr::group_by()` to describe which variables should be kept outside.

```df2 %>% group_by(g) %>% nest()
```

I think nesting is easiest to understand in connection to grouped data: each row in the output corresponds to one group in the input. We'll see shortly this is particularly convenient when you have other per-group objects.

The opposite of `nest()` is `unnest()`. You give it the name of a list-column containing data frames, and it row-binds the data frames together, repeating the outer columns the right number of times to line up.

```df1 %>% unnest(data)
```

## Nested data and models

Nested data is a great fit for problems where you have one of something for each group. A common place this arises is when you're fitting multiple models.

```mtcars_nested <- mtcars %>%
group_by(cyl) %>%
nest()

mtcars_nested
```

Once you have a list of data frames, it's very natural to produce a list of models:

```mtcars_nested <- mtcars_nested %>%
mutate(model = map(data, function(df) lm(mpg ~ wt, data = df)))
mtcars_nested
```

And then you could even produce a list of predictions:

```mtcars_nested <- mtcars_nested %>%
mutate(model = map(model, predict))
mtcars_nested
```

This workflow works particularly well in conjunction with broom, which makes it easy to turn models into tidy data frames which can then be `unnest()`ed to get back to flat data frames. You can see a bigger example in the broom and dplyr vignette.

SMAC-Group/simts documentation built on Sept. 4, 2023, 5:25 a.m.