[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)
```
``` 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