fbprophet: is it possible to plot only a part of plot_categories?

73 Views Asked by At

Hi so I'm trying to do hyperparameter tuning for a prophet model, and I'd like to see the effect of tuning the hyperparameters to the trend, seasonality, and holiday by using plot_components() and it returns the visualization for all three components. Is there a way to only visualize one component, for example, only the seasonality, so that I can see how changing seasonality_prior_scale parameter changes the seasonality?

I tried using model.plot_components(forecast[['ds','weekly']]) but obviously it doesn't work..

1

There are 1 best solutions below

0
Russ Conte On

Is there a way to only visualize one component, for example, only the seasonality...

Absolutely yes. I do all of this work in R, so here is the R code.

  1. Load all packages that will be used.
library(tidyverse)
library(fpp3)
library(prophet)
library(fable.prophet)
  1. Break data into train and test

The fpp3 library includes a number of data sets and some amazing functions for working with time series, so we can make a reproducible example. We begin by splitting the data into training (60%) and testing (40%) amounts. The 60% and 40% are my choices, there is nothing special about those values.

time_series <- aus_retail %>% 
  filter(`Series ID` == "A3349849A") %>% 
  select(State, `Series ID`, Month, Turnover)

time_series_train <- time_series[1:310,]
time_series_test <- time_series[310:431,]

3, fit a model to the training data, using a prophet model:

fit <- time_series_train %>% 
  model(
    prophet = fable.prophet::prophet(Turnover)
  )
  1. Find all the components. A single line gives all the information you are asking about for this prophet model, such as trends, residuals, seasonality (yearly, weekly, daily), and more:
components(fit)

returns:

> components(fit)
# A dable: 310 x 11 [1M]
# Key:     State, .model [1]
# :        Turnover = trend * (1 + multiplicative_terms) + additive_terms + .resid
   State                        .model     Month Turnover additive_terms multiplicative_terms trend  yearly  weekly  daily  .resid
   <chr>                        <chr>      <mth>    <dbl>          <dbl>                <dbl> <dbl>   <dbl>   <dbl>  <dbl>   <dbl>
 1 Australian Capital Territory prophet 1982 Apr      4.4       -0.533                      0  4.35 -0.216  -0.140  -0.177  0.582 
 2 Australian Capital Territory prophet 1982 May      3.4       -0.0383                     0  4.41  0.324  -0.185  -0.177 -0.976 
 3 Australian Capital Territory prophet 1982 Jun      3.6       -0.199                      0  4.48 -0.282   0.260  -0.177 -0.680 
 4 Australian Capital Territory prophet 1982 Jul      4         -0.289                      0  4.54  0.0278 -0.140  -0.177 -0.253 
 5 Australian Capital Territory prophet 1982 Aug      3.6       -0.00124                    0  4.61  0.117   0.0585 -0.177 -1.01  
 6 Australian Capital Territory prophet 1982 Sep      4.2       -0.268                      0  4.67  0.132  -0.224  -0.177 -0.205 
 7 Australian Capital Territory prophet 1982 Oct      4.8        1.24                       0  4.74  1.14    0.278  -0.177 -1.18  
 8 Australian Capital Territory prophet 1982 Nov      5.4        0.669                      0  4.80  0.893  -0.0467 -0.177 -0.0710
 9 Australian Capital Territory prophet 1982 Dec      6.9        1.41                       0  4.86  1.81   -0.224  -0.177  0.623 
10 Australian Capital Territory prophet 1983 Jan      3.8       -1.85                       0  4.93 -1.49   -0.185  -0.177  0.721 
  1. Plot multiple seasonalities Plotting any one of those is only two lines of code, for example here is how the trend is plotted (but all the others are handled in exactly the same method):
components(fit) %>% 
  autoplot(trend)

You can then plot any of the other seasonality columns to see how that changes the results. I've attached the yearly and weekly seasonality as examples.Yearly seasonalityweekly seasonality