简体   繁体   中英

How to determine the trend and seasonality of an entire data set using time series in R?

I'm doing time series analysis using R with the fpp3 package, and don't see a way to calculate trend and seasonality for the entire data set. For example, this data set looks like it has trend and seasonality, but how can the values be found for trend and seasonality for the entire data set?

library(tidyverse)
library(fpp3)
us_gasoline %>% 
  autoplot(Barrels)

汽油销售图

If a decomposition is used, it returns trend and seasonality numbers for each row, but what is the seasonality for the entire data set? Is it monthly? Quarterly? Annually? Some unusual value? Same question for the trend for the entire data set. For example, this code:

us_gasoline %>% 
  model(
    STL(Barrels)
  ) %>% 
  components()

returns this data:

汽油数据的趋势和季节性

How are the trend (or trends, if more than one) and seasonality (or seasonalities, if more than one) calculated for an entire data set?

An STL decomposition will produce a single trend for the time series, and one or more seasonalities. You can visualise these decomposed components with autoplot(<dable>) , for example:

library(fpp3)
#> ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
#> ✔ tibble      3.1.8          ✔ tsibble     1.1.1     
#> ✔ dplyr       1.0.9          ✔ tsibbledata 0.4.0     
#> ✔ tidyr       1.2.0          ✔ feasts      0.2.2     
#> ✔ lubridate   1.8.0          ✔ fable       0.3.1.9000
#> ✔ ggplot2     3.3.6
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> ✖ lubridate::date()    masks base::date()
#> ✖ dplyr::filter()      masks stats::filter()
#> ✖ tsibble::intersect() masks base::intersect()
#> ✖ tsibble::interval()  masks lubridate::interval()
#> ✖ dplyr::lag()         masks stats::lag()
#> ✖ tsibble::setdiff()   masks base::setdiff()
#> ✖ tsibble::union()     masks base::union()
us_gasoline %>% 
  model(
    STL(Barrels)
  ) %>% 
  components() %>%
  autoplot()

Created on 2022-08-22 by the reprex package (v2.0.1)

The default seasonal periods used by STL are based on common seasonal periods and are not specific to any particular dataset. For data that is observed weekly, the sensible seasonal period is annual. This is indicated in the column name of the seasonal component: season_year .

You can specify one or more seasonal periods using the season() model special. For example, decomposing an annual and 4-week pattern:

library(fpp3)
#> ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
#> ✔ tibble      3.1.8          ✔ tsibble     1.1.1     
#> ✔ dplyr       1.0.9          ✔ tsibbledata 0.4.0     
#> ✔ tidyr       1.2.0          ✔ feasts      0.2.2     
#> ✔ lubridate   1.8.0          ✔ fable       0.3.1.9000
#> ✔ ggplot2     3.3.6
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> ✖ lubridate::date()    masks base::date()
#> ✖ dplyr::filter()      masks stats::filter()
#> ✖ tsibble::intersect() masks base::intersect()
#> ✖ tsibble::interval()  masks lubridate::interval()
#> ✖ dplyr::lag()         masks stats::lag()
#> ✖ tsibble::setdiff()   masks base::setdiff()
#> ✖ tsibble::union()     masks base::union()
us_gasoline %>% 
  model(
    STL(Barrels ~ season(period = "year") + season(period = "4 weeks"))
  ) %>% 
  components() %>%
  autoplot()

Created on 2022-08-22 by the reprex package (v2.0.1)

Note that this data does not exhibit a 4-week pattern, so you wouldn't usually want to extract a 4 week seasonal pattern from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM