Site icon R-bloggers

Quickly Generate Nested Time Series Models

[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.
< section id="introduction" class="level1">

Introduction

There 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

< section id="exampmle" class="level1">

Exampmle

Let’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,148
Columns: 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
   <chr>                         <list>     <dbl>  <dbl> <dbl> <dbl> <int>
 1 Alcohol Abuse                 <fr_ARIMA> 2.22  -241.   493.  506.   109
 2 Bariatric Surgery For Obesity <fr_ARIMA> 0.609  -80.1  168.  178.    88
 3 CHF                           <fr_ARIMA> 0.963 -152.   309.  314.   110
 4 COPD                          <fr_ARIMA> 0.987 -155.   315.  320.   110
 5 CVA                           <fr_ARIMA> 1.50  -201.   407.  412.   110
 6 Carotid Endarterectomy        <fr_ARIMA> 6.27  -166.   335.  339.    51
 7 Cellulitis                    <fr_ARIMA> 1.07  -163.   329.  335.   110
 8 Chest Pain                    <fr_ARIMA> 0.848 -139.   281.  287.   110
 9 GI Hemorrhage                 <fr_ARIMA> 1.21  -179.   361.  366.   111
10 Joint Replacement             <fr_ARIMA> 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.
Exit mobile version