[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. IntroductionThere are many approaches to modeling time series data in R. One of the types of data that we might come across is a nested time series. This means the data is grouped simply by one or more keys. There are many methods in which to accomplish this task. This will be a quick post, but if you want a longer more detailed and quite frankly well written out one, then this is a really good article

ExampmleLet’s just get to it with a very simple example, the motivation here isn’t to be all encompassing, but rather to just showcase it is possible for those who may not know it is.

library(healthyR.data)library(dplyr)library(timetk)ts\_tbl <- healthyR\_data |> filter(ip\_op\_flag == "I") |> select(visit\_end\_date\_time, service\_line, length\_of\_stay) |> mutate(visit\_end\_date\_time = as.Date(visit\_end\_date\_time)) |> group\_by(service\_line) |> summarise\_by\_time( .date\_var = visit\_end\_date\_time, .by = "month", los = mean(length\_of\_stay) ) |> ungroup()glimpse(ts\_tbl)

Rows: 2,148Columns: 3$ service\_line <chr> "Alcohol Abuse", "Alcohol Abuse", "Alcohol Abuse",…$ visit\_end\_date\_time <date> 2011-09-01, 2011-10-01, 2011-11-01, 2011-12-01, 2…$ los <dbl> 3.666667, 3.181818, 4.380952, 3.464286, 3.677419, …

library(forecast)library(broom)library(tidyr)glanced\_models <- ts\_tbl |> nest\_by(service\_line) |> mutate(AA = list(auto.arima(data$los))) |> mutate(perf = list(glance(AA))) |> unnest(cols = c(perf))glanced\_models |> select(-data)

```

A tibble: 23 × 7# Groups: service_line [23] service_line AA sigma logLik AIC BIC nobs 1 Alcohol Abuse 2.22 -241. 493. 506. 109 2 Bariatric Surgery For Obesity 0.609 -80.1 168. 178. 88 3 CHF 0.963 -152. 309. 314. 110 4 COPD 0.987 -155. 315. 320. 110 5 CVA 1.50 -201. 407. 412. 110 6 Carotid Endarterectomy 6.27 -166. 335. 339. 51 7 Cellulitis 1.07 -163. 329. 335. 110 8 Chest Pain 0.848 -139. 281. 287. 110 9 GI Hemorrhage 1.21 -179. 361. 366. 11110 Joint Replacement 1.65 -196. 396. 401. 102# … with 13 more rows

``` Voila!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.


R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.


Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.Continue reading: Quickly Generate Nested Time Series Models