Plotting time series

m4_daily %>% count(id)
## # A tibble: 4 × 2
##   id        n
##   <fct> <int>
## 1 D10     674
## 2 D160   4197
## 3 D410    676
## 4 D500   4196
m4_daily %>%
    group_by(id) %>%
    plot_time_series(
        .date_var     = date,
        .value        = value,
        .facet_ncol   = 2,
        .facet_scales = "free", 
        .interactive  = FALSE )

Visualizing Transformations & Sub-Groups

m4_hourly %>% count(id)
## # A tibble: 4 × 2
##   id        n
##   <fct> <int>
## 1 H10     700
## 2 H50     700
## 3 H150    700
## 4 H410    960
m4_hourly %>%
    group_by(id) %>%
    plot_time_series(
        .date_var     = date,
        .value        = log(value),
        .facet_ncol   = 2,
        .facet_scales = "free", 
        .color_var    = week(date) 
    )

Static ggplot2 Visualizations & Customization

taylor_30_min %>%
  plot_time_series(date, value, 
                   .color_var = month(date, label = TRUE),
                   
                   # Returns static ggplot
                   .interactive = FALSE,  
                   
                   # Customization
                   .title     = "Taylor's MegaWatt Data",
                   .x_lab     = "Date (30-min intervals)",
                   .y_lab     = "Energy Demand (MW)",
                   .color_lab = "Month") +
  scale_y_continuous(labels = scales::label_comma())

Box Plots

m4_monthly %>% count(id)
## # A tibble: 4 × 2
##   id        n
##   <fct> <int>
## 1 M1      469
## 2 M2      469
## 3 M750    306
## 4 M1000   330
m4_monthly %>%
    filter_by_time(.date_var = date,
                   .end_date = "1976") %>%
    group_by(id) %>%
    plot_time_series_boxplot(
        .date_var   = date,
        .value      = value,
        .period     = "1 year",
        .facet_ncol = 2
    )

Regression Plots

m4_monthly %>%
    group_by(id) %>%
    plot_time_series_regression(
        .date_var     = date,
        .facet_ncol   = 2,
        .formula      = log(value) ~ as.numeric(date) 
        + month(date, 
                label = TRUE),
        .show_summary = FALSE
    )

Plotting Seasonality and Correlation

Correlation Plots

walmart_sales_weekly %>%
    group_by(id) %>%
    plot_acf_diagnostics(
        Date, Weekly_Sales,
        .ccf_vars = c(Temperature, Fuel_Price),
        .lags = "3 months"
    )

Seasonality

taylor_30_min %>%
    plot_seasonal_diagnostics(
        date,
        value
    )
m4_hourly %>% count(id)
## # A tibble: 4 × 2
##   id        n
##   <fct> <int>
## 1 H10     700
## 2 H50     700
## 3 H150    700
## 4 H410    960
m4_hourly %>%
    group_by(id) %>%
    plot_seasonal_diagnostics(date, value)

STL Diognostics

m4_hourly %>%
    group_by(id) %>%
    plot_stl_diagnostics(
        date, value,
        .feature_set = c("observed", "season", "trend", "remainder")
    )
## frequency = 24 observations per 1 day
## trend = 336 observations per 14 days
## frequency = 24 observations per 1 day
## trend = 336 observations per 14 days
## frequency = 24 observations per 1 day
## trend = 336 observations per 14 days
## frequency = 24 observations per 1 day
## trend = 336 observations per 14 days

Time Series Data Wrangling

Summarize by Time

FANG %>%
    group_by(symbol) %>%
    summarise_by_time(.date_var = date,
                      adjusted = mean(adjusted),
                      .by = "month") %>%
    plot_time_series(date, adjusted, .facet_ncol = 2, .interactive = FALSE)

Filter by Time

FANG %>%
    group_by(symbol) %>%
    filter_by_time(.date_var = date,
                   .start_date = "2013-09",
                   .end_date = "2013") %>%
    plot_time_series(date, adjusted, .facet_ncol = 2)

Padding Data

FANG %>%
    group_by(symbol) %>%
    pad_by_time(date, .by = "day",
                 .pad_value = 0)
## # A tibble: 5,836 × 8
## # Groups:   symbol [4]
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 AMZN   2013-01-02  256.  258.  253.  257. 3271000     257.
##  2 AMZN   2013-01-03  257.  261.  256.  258. 2750900     258.
##  3 AMZN   2013-01-04  258.  260.  257.  259. 1874200     259.
##  4 AMZN   2013-01-05    0     0     0     0        0       0 
##  5 AMZN   2013-01-06    0     0     0     0        0       0 
##  6 AMZN   2013-01-07  263.  270.  263.  268. 4910000     268.
##  7 AMZN   2013-01-08  267.  269.  264.  266. 3010700     266.
##  8 AMZN   2013-01-09  268.  270.  265.  266. 2265600     266.
##  9 AMZN   2013-01-10  269.  269.  262.  265. 2863400     265.
## 10 AMZN   2013-01-11  265.  268.  264.  268. 2413300     268.
## # ℹ 5,826 more rows

Sliding (Rolling) Calculations

# Rolling regressions are easy to implement using `.unlist = FALSE`
lm_roll <- slidify(~ lm(..1 ~ ..2 + ..3), .period = 90, 
                   .unlist = FALSE, .align = "right")


FANG %>%
  select(symbol, date, adjusted, volume) %>%
  group_by(symbol) %>%
  mutate(numeric_date = as.numeric(date)) %>%
  # Apply rolling regression
  mutate(rolling_lm = lm_roll(adjusted, volume, numeric_date)) %>%
  filter(!is.na(rolling_lm))
## # A tibble: 3,676 × 6
## # Groups:   symbol [4]
##    symbol date       adjusted   volume numeric_date rolling_lm
##    <chr>  <date>        <dbl>    <dbl>        <dbl> <list>    
##  1 FB     2013-05-10     26.7 30847100        15835 <lm>      
##  2 FB     2013-05-13     26.8 29068800        15838 <lm>      
##  3 FB     2013-05-14     27.1 24930300        15839 <lm>      
##  4 FB     2013-05-15     26.6 30299800        15840 <lm>      
##  5 FB     2013-05-16     26.1 35499100        15841 <lm>      
##  6 FB     2013-05-17     26.2 29462700        15842 <lm>      
##  7 FB     2013-05-20     25.8 42402900        15845 <lm>      
##  8 FB     2013-05-21     25.7 26261300        15846 <lm>      
##  9 FB     2013-05-22     25.2 45314500        15847 <lm>      
## 10 FB     2013-05-23     25.1 37663100        15848 <lm>      
## # ℹ 3,666 more rows