Housing Markets Down: Hierarchical Time Series

[This article was first published on DataGeeek, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

For a long time, everybody in Turkey complains about how far the house and rent prices are up. It seems the same situation is true all over the world. This is called the pandemic housing boom in the USA. But this might’ve been coming to the end, according to some authorities.

To check this argument, we will first examine the trend of quarterly nominal housing price indices with the base year 2015 since the 1970s.

library(fpp3)
library(tidyverse)
library(countrycode)
library(janitor)
library(zoo)

df <- read_csv("https://raw.githubusercontent.com/mesdi/blog/main/housing_all.csv")

df_ts <- 
  df %>% 
  clean_names() %>%
  #converting the string time variable to quarterly date format for tsibble
  mutate(time = yearquarter(as.yearqtr(time, format = "%Y-Q%q")),
         #converting the country codes to their corresponding names
         location = countrycode(location, "iso3c", "country.name")) %>% 
  na.omit() %>% 
  as_tsibble(key=c(location, subject)) %>%
  #subcontinent names of United Nations
  mutate(region = countrycode(location, "country.name","un.regionsub.name")) %>%
  mutate(region = case_when(
            region == "Australia and New Zealand" ~ "Ocenia",
            region == "Latin America and the Caribbean" ~ "Southern America",
            TRUE ~ region)) %>% 
  relocate(time,region)

Now that we’ve created our tsibble data frame, we can create a function that compares the nominal house and rent indices of the corresponding countries within the subcontinent.

plot_line <- 
  function(subcontinent = "Northern America"){
  
    df_ts %>% 
      filter(region == subcontinent, year(time) >= 2000) %>% 
      ggplot(aes(x = time, y=value))+
      geom_line(aes(color = subject), size = 1.2)+
      facet_wrap(vars(location), scales = "free_y")+
      labs(x="", y="")+
      theme_minimal()+
      theme(strip.background = element_rect(fill = "lightblue", color=NA,),
            strip.text = element_text(face = "bold.italic"),
            legend.title = element_blank())
  
}

plot_line()

When we examine the above plots for Northern America countries, we can clearly see that there is an upward breaking of nominal house price indices since 2020 when Covid-19 started to affect significantly.

We can look at the countries of other regions with the function we’ve just made before, as well.

plot_line("Western Europe")

It looks like the same trendline can be valid for Western Europe.

Now, we will model the nominal house prices to see how the trend would be if the pandemic doesn’t exist. Hence, we can understand whether the upward trends were due to Covid-19.

Because our data is based on geographic divisions, we are going to use the hierarchical time series method to model. We will produce coherent forecasts using bottom-up and MintT approaches with the mint_shrink method .

#creating hierarchical time series with nominal house prices
df_full <- df_ts %>%
  filter(subject=="NOMINAL") %>% 
  aggregate_key(region/location, value = sum(value))

#generating coherent forecasts using bottom-up and MinT methods
fit <- df_full %>%
  filter(year(time) <= 2019) %>%
  model(base = ARIMA(value)) %>%
  reconcile(
    bu = bottom_up(base),
    mint = min_trace(base, method = "mint_shrink")
  )

Now, we can create a function that predicts the nominal house indices from 2020 and plots them disaggregated to the countries we want. The region and forecast horizon arguments are optional.

plot_agg <- 
  function(subcontinent = "Northern America", ahead = "5 years"){
  
  fc <- fit %>% forecast(h = ahead) 
  
  fc %>%
    #disaggregated by locations(countries)
    filter(!is_aggregated(location)) %>%
    filter(region == subcontinent) %>% 
    autoplot(
      df_full %>% filter(year(time) >= 2000),
      level = NULL, #omits prediction interval
      size=1.2
    ) +
    labs(y = "", x="") +
    facet_wrap(vars(location), scales = "free_y")+
    theme_minimal()+
    theme(strip.background = element_rect(fill = "#e2e14c", color=NA,),
          strip.text = element_text(face = "bold.italic"),
          legend.title = element_blank())
  
}

plot_agg()

When we look at the above graph, we can only see the MinT method is drawn. The reason is that the predictions of all methods are almost the same. Also, we can say for Northern America that this uptrend from 2020 has not been natural and was affected by Covid-19.

To leave a comment for the author, please follow the link and comment on their blog: DataGeeek.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)