knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(HMSr)
library(tidyverse)
theme_set_hms()
# https://px.hagstofa.is/pxis/pxweb/is/Efnahagur/Efnahagur__utanrikisverslun__1_voruvidskipti__01_voruskipti/UTA06002.px/table/tableViewLayout1/?rxid=8966fb9b-6a92-41b6-8758-129d490bb573
trade <- read_hagstofan_alt("https://px.hagstofa.is:443/pxis/sq/8ad42406-35c1-442b-a556-1498992b56ed")

The data is different components of international trade:

trade %>% 
  head() %>% 
  knitr::kable()

Seasonal adjustment for one group:

trade %>%
  mutate(Mánuður = lubriYYYYMM(Mánuður)) %>%
  ################# Magic :) #################################################
  mutate(
    `Útflutningur fob - Árstíðarleiðrétt` = seas_seasonally_adjusted(`Útflutningur fob`, Mánuður)
    ) %>%
  ##############################################################################
  select(Mánuður, `Útflutningur fob`, `Útflutningur fob - Árstíðarleiðrétt` ) %>%
  gather(var, val, -Mánuður) %>%
  ggplot(aes(Mánuður, val, color = var)) +
  geom_line()

the trend component:

trade %>%
  mutate(Mánuður = lubriYYYYMM(Mánuður)) %>%
  ################# Magic :) #################################################
  mutate(
    `Útflutningur fob - Trend` = seas_trend(`Útflutningur fob`, Mánuður)
    ) %>%
  ##############################################################################
  select(Mánuður, `Útflutningur fob`,  `Útflutningur fob - Trend` ) %>%
  gather(var, val, -Mánuður) %>%
  ggplot(aes(Mánuður, val, color = var)) +
  geom_line()

Many groups

The data is transformed as suchs:

trade_long <- trade %>%
  mutate(Mánuður = lubriYYYYMM(Mánuður)) %>%
  gather(variable, value, -Mánuður) 

trade_long %>% 
  head() %>% 
  knitr::kable()

Must use group_by such that there is only one series per group:

trade_long %>% 
  group_by(variable) %>% 
  mutate(trend = seas_trend(value, Mánuður)) %>% 
  ggplot(aes(Mánuður, trend, color = variable)) +
  geom_line()

The following will throw an error:

trade_long %>% 
  # group_by(variable) %>% 
  mutate(trend = seas_trend(value, Mánuður)) %>% 
  ggplot(aes(Mánuður, trend, color = variable)) +
  geom_line()

It is possible to get all the different components from the seasonal::seas() function with map_seas() but it's

still experimental. It might result in unexpected behaviour if the data structure is different from this example.

trade_seas <- trade_long %>% 
  group_by(variable) %>% 
  map_seas(value, Mánuður) 

trade_seas%>% 
  filter(seasonal_component %in% c("trend", "final", "value")) %>% 
  ggplot(aes(Mánuður, seasonal_value, color = seasonal_component)) +
  geom_line() +
  facet_wrap(~variable, scales = "free_y")
trade_seas%>% 
  filter(seasonal_component %in% c("trend", "final", "value")) %>% 
  ggplot(aes(Mánuður, seasonal_value, color = seasonal_component)) +
  geom_line() +
  facet_wrap(~variable, scales = "free_y")


karsfri/HMSr documentation built on Feb. 26, 2020, 10:36 a.m.