Write a function to compute the mean and standard deviation of a time series, and apply it to the PBS data. Plot the series with the highest mean, and the series with the lowest standard deviation.

PBS
## # A tsibble: 67,596 x 9 [1M]
## # Key:       Concession, Type, ATC1, ATC2 [336]
##       Month Concession   Type      ATC1  ATC1_desc ATC2  ATC2_desc Scripts  Cost
##       <mth> <chr>        <chr>     <chr> <chr>     <chr> <chr>       <dbl> <dbl>
##  1 1991 Jul Concessional Co-payme… A     Alimenta… A01   STOMATOL…   18228 67877
##  2 1991 Aug Concessional Co-payme… A     Alimenta… A01   STOMATOL…   15327 57011
##  3 1991 Sep Concessional Co-payme… A     Alimenta… A01   STOMATOL…   14775 55020
##  4 1991 Oct Concessional Co-payme… A     Alimenta… A01   STOMATOL…   15380 57222
##  5 1991 Nov Concessional Co-payme… A     Alimenta… A01   STOMATOL…   14371 52120
##  6 1991 Dec Concessional Co-payme… A     Alimenta… A01   STOMATOL…   15028 54299
##  7 1992 Jan Concessional Co-payme… A     Alimenta… A01   STOMATOL…   11040 39753
##  8 1992 Feb Concessional Co-payme… A     Alimenta… A01   STOMATOL…   15165 54405
##  9 1992 Mar Concessional Co-payme… A     Alimenta… A01   STOMATOL…   16898 61108
## 10 1992 Apr Concessional Co-payme… A     Alimenta… A01   STOMATOL…   18141 65356
## # ℹ 67,586 more rows
meanstd <- function(x) {
  c(
    mean = mean(x, na.rm = TRUE),
    sd = sd(x, na.rm = TRUE)
  )
}

pbs_features <- PBS |>
  features(Cost, features = meanstd)

pbs_features
## # A tibble: 336 × 6
##    Concession   Type        ATC1  ATC2       mean       sd
##    <chr>        <chr>       <chr> <chr>     <dbl>    <dbl>
##  1 Concessional Co-payments A     A01      67673.   14763.
##  2 Concessional Co-payments A     A02   16455044. 7498596.
##  3 Concessional Co-payments A     A03     476221.  370696.
##  4 Concessional Co-payments A     A04     463392.  154020.
##  5 Concessional Co-payments A     A05     147604.   74190.
##  6 Concessional Co-payments A     A06     417889.  163040.
##  7 Concessional Co-payments A     A07     917795.  338081.
##  8 Concessional Co-payments A     A09     343881.   89815.
##  9 Concessional Co-payments A     A10    5680010. 3180294.
## 10 Concessional Co-payments A     A11     651863.  387439.
## # ℹ 326 more rows
pbs_features |>
  filter(mean == max(mean))
## # A tibble: 1 × 6
##   Concession   Type        ATC1  ATC2       mean        sd
##   <chr>        <chr>       <chr> <chr>     <dbl>     <dbl>
## 1 Concessional Co-payments C     C10   24845501. 18384824.
pbs_features |>
  filter(sd == min(sd))
## # A tibble: 2 × 6
##   Concession Type        ATC1  ATC2   mean    sd
##   <chr>      <chr>       <chr> <chr> <dbl> <dbl>
## 1 General    Co-payments R     R         0     0
## 2 General    Co-payments S     S         0     0

Use GGally::ggpairs() to look at the relationships between the STL-based features for the holiday series in the tourism data. Change seasonal_peak_year and seasonal_trough_year to factors, as shown in Figure 4.3. Which is the peak quarter for holidays in each state?

tourism |>
  features(Trips, feat_stl) |>
  select(-Region, -State, -Purpose) |>
  mutate(
    seasonal_peak_year = factor(seasonal_peak_year),
    seasonal_trough_year = factor(seasonal_trough_year),
  ) |>
  GGally::ggpairs()
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

tourism %>%
  group_by(State) %>%
  summarise(Trips = sum(Trips)) %>%
  features(Trips, feat_stl) %>%
  select(State, seasonal_peak_year)
## # A tibble: 8 × 2
##   State              seasonal_peak_year
##   <chr>                           <dbl>
## 1 ACT                                 2
## 2 New South Wales                     1
## 3 Northern Territory                  3
## 4 Queensland                          3
## 5 South Australia                     1
## 6 Tasmania                            1
## 7 Victoria                            1
## 8 Western Australia                   1

Personally found the last chapter incredibly confusing.