Last active
August 7, 2019 22:08
-
-
Save aaronschiff/3419bea99a6d525ff62aab26dd9d10f2 to your computer and use it in GitHub Desktop.
GDP vs business confidence correlation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup | |
library(conflicted) | |
library(tidyverse) | |
library(lubridate) | |
library(readxl) | |
conflict_prefer("filter", "dplyr") | |
conflict_prefer("lag", "dplyr") | |
conflict_prefer("lead", "dplyr") | |
# Read GDP data and calculate growth rates | |
# Seasonally adjusted quarterly real gdp | |
gdp_seas_adj <- read_csv("SNE446901_20190808_074504_68.csv", | |
skip = 2, | |
col_types = "ci", | |
col_names = c("quarter", "gdp_seas_adj")) %>% | |
filter(!is.na(gdp_seas_adj)) %>% | |
separate(quarter, into = c("year", "quarter"), sep = "Q", convert = TRUE) %>% | |
mutate(qtr_gdp_seas_adj_growth = 100 * (gdp_seas_adj / lag(gdp_seas_adj) - 1), | |
yoy_gdp_seas_adj_growth = 100 * (gdp_seas_adj / lag(gdp_seas_adj, 4) - 1)) | |
# Un-adjsuted quarterly real gdp | |
gdp <- read_csv("SNE445001_20190808_094414_16.csv", | |
skip = 2, | |
col_types = "ci", | |
col_names = c("quarter", "gdp")) %>% | |
filter(!is.na(gdp)) %>% | |
separate(quarter, into = c("year", "quarter"), sep = "Q", convert = TRUE) %>% | |
mutate(yoy_gdp_growth = 100 * (gdp / lag(gdp, 4) - 1)) | |
# Read business confidence data | |
bus_conf <- read_excel("ANZ_Business_Outlook_Data.xlsx", | |
sheet = "BusConf", | |
skip = 5, | |
col_types = c("date", rep("numeric", 6)), | |
col_names = c("date_month", "conf_all", "conf_retail", "conf_mfg", "conf_ag", "conf_con", "conf_serv"), | |
na = "...") %>% | |
filter(!is.na(conf_all)) %>% | |
mutate(date_month = as_date(date_month)) %>% | |
mutate(quarter = quarter(date_month), | |
year = year(date_month)) | |
# Calculate quarterly average business confidence | |
qtr_avg_bus_conf <- bus_conf %>% | |
group_by(quarter, year) %>% | |
summarise_at(vars(starts_with("conf")), mean, na.rm = TRUE) %>% | |
arrange(year, quarter) | |
# Combine GDP and confidence data | |
combined_dat <- left_join(gdp_seas_adj, | |
gdp, | |
by = c("quarter", "year")) %>% | |
left_join(qtr_avg_bus_conf, by = c("quarter", "year")) | |
# Calculate correlation of seasonally adjusted GDP growth with conf_all | |
gdp_qtr_seas_adj_gr_conf_cor <- function(l) { | |
y <- cor(combined_dat$qtr_gdp_seas_adj_growth, | |
lag(combined_dat$conf_all, l), | |
use = "complete.obs") | |
return(y) | |
} | |
gdp_yoy_seas_adj_gr_conf_cor <- function(l) { | |
y <- cor(combined_dat$yoy_gdp_seas_adj_growth, | |
lag(combined_dat$conf_all, l), | |
use = "complete.obs") | |
return(y) | |
} | |
gdp_yoy_gr_conf_cor <- function(l) { | |
y <- cor(combined_dat$yoy_gdp_growth, | |
lag(combined_dat$conf_all, l), | |
use = "complete.obs") | |
return(y) | |
} | |
gdp_conf_cor_vs_lag <- | |
tibble(bus_conf_lag = 0:8) %>% | |
rowwise() %>% | |
mutate(cor_qtr_seas_adj_gr = gdp_qtr_seas_adj_gr_conf_cor(bus_conf_lag), | |
cor_yoy_seas_adj_gr = gdp_yoy_seas_adj_gr_conf_cor(bus_conf_lag), | |
cor_yoy_gr = gdp_yoy_gr_conf_cor(bus_conf_lag)) | |
# Regression of year-on-year GDP growth vs confidence | |
summary(lm(yoy_gdp_growth ~ lag(conf_all, 4), data = combined_dat)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment