Load libraries

library(tidyquant)
library(tidyverse)
library(broom)
library(knitr)
library(kableExtra)
library(tsibble)
library(ggcorrplot)
library(ggfittext)
library(psych)
library(data.table)
library(ivreg)
library(janitor)
library(readxl)
library(fable)
library(mFilter)
library(ggrepel)
library(lmtest)
library(magrittr)
library(sandwich)
library(car)
library(slider)

Create dataframes

real_df <- 
  read_csv("real_assets.csv") %>% 
  mutate(date = floor_date(date(mdy_hm(date)), "month"))

real_long_df <- 
  read_csv("real_assets_long.csv")%>% 
  mutate(date = floor_date(date(mdy_hm(date)), "month"))
         
sp_real_df <- 
  read_csv("sp_real.csv") %>% 
mutate(date = mdy(date),
       date = floor_date(date, "month")) %>% 
  group_by(date) %>% 
  filter(row_number() == n()) %>% 
  ungroup %>% 
  mutate(sp_real = 100*((sp_real / lag(sp_real, 1)) - 1)) 

infl <- 
c("MEDCPIM158SFRBCLE", "TRMMEANCPIM158SFRBCLE", "CPIAUCSL", "CPILFESL") %>% 
  tq_get(get = "economic.data", from = "2014-12-01") %>% 
  pivot_wider(names_from = symbol, values_from = price) %>% 
  set_colnames(c("date", "median_cpi", "trimmed_mean_cpi", "cpi", "core_cpi")) %>% 
  mutate(cpi = 100*((cpi / lag(cpi, 1)) - 1),
         core_cpi = 100*((core_cpi / lag(core_cpi, 1)) - 1),
         median_cpi = ((((median_cpi / 100) + 1)^(1/12)) - 1)*100,
         trimmed_mean_cpi = ((((trimmed_mean_cpi / 100) + 1)^(1/12)) - 1)*100,
         headline_shock = cpi - median_cpi) %>% 
  drop_na

Join dataframes

real_df <- 
  real_df %>% 
  left_join(sp_real_df, by = "date") %>% 
  left_join(infl, by = "date")
  
real_long_df <- 
  real_long_df %>% 
  left_join(sp_real_df, by = "date") %>% 
  left_join(infl, by = "date")

Correlation matrices

real_df %>% 
  filter(between(year(date), 2021, 2023)) %>% 
  select(-date) %>% 
  cor(use = "complete.obs") %>% 
  round(2)
##                  NTRAA  SPGI  SNRU  TIPS  BCTR NTGRE SPXTR sp_real median_cpi
## NTRAA             1.00  0.95  0.90  0.72  0.53  0.91  0.89    0.97      -0.30
## SPGI              0.95  1.00  0.83  0.69  0.51  0.86  0.82    0.94      -0.17
## SNRU              0.90  0.83  1.00  0.49  0.71  0.72  0.69    0.85      -0.21
## TIPS              0.72  0.69  0.49  1.00  0.25  0.76  0.76    0.78      -0.35
## BCTR              0.53  0.51  0.71  0.25  1.00  0.36  0.30    0.53      -0.30
## NTGRE             0.91  0.86  0.72  0.76  0.36  1.00  0.90    0.95      -0.35
## SPXTR             0.89  0.82  0.69  0.76  0.30  0.90  1.00    0.89      -0.33
## sp_real           0.97  0.94  0.85  0.78  0.53  0.95  0.89    1.00      -0.34
## median_cpi       -0.30 -0.17 -0.21 -0.35 -0.30 -0.35 -0.33   -0.34       1.00
## trimmed_mean_cpi -0.20 -0.11 -0.11 -0.26 -0.11 -0.23 -0.28   -0.23       0.77
## cpi              -0.03 -0.01 -0.02 -0.17  0.03 -0.04 -0.09   -0.07       0.24
## core_cpi         -0.17 -0.14 -0.16 -0.08 -0.09 -0.14 -0.17   -0.15       0.41
## headline_shock    0.11  0.06  0.08 -0.01  0.17  0.12  0.06    0.09      -0.23
##                  trimmed_mean_cpi   cpi core_cpi headline_shock
## NTRAA                       -0.20 -0.03    -0.17           0.11
## SPGI                        -0.11 -0.01    -0.14           0.06
## SNRU                        -0.11 -0.02    -0.16           0.08
## TIPS                        -0.26 -0.17    -0.08          -0.01
## BCTR                        -0.11  0.03    -0.09           0.17
## NTGRE                       -0.23 -0.04    -0.14           0.12
## SPXTR                       -0.28 -0.09    -0.17           0.06
## sp_real                     -0.23 -0.07    -0.15           0.09
## median_cpi                   0.77  0.24     0.41          -0.23
## trimmed_mean_cpi             1.00  0.65     0.67           0.28
## cpi                          0.65  1.00     0.57           0.89
## core_cpi                     0.67  0.57     1.00           0.38
## headline_shock               0.28  0.89     0.38           1.00
real_long_df %>% 
  select(-date) %>% 
  cor(use = "complete.obs") %>% 
  round(2)
##                  NTRAA  SPGI  SNRU  TIPS  BCTR NTGRE SPXTR sp_real median_cpi
## NTRAA             1.00  0.94  0.84  0.61  0.55  0.93  0.86    0.96      -0.14
## SPGI              0.94  1.00  0.80  0.55  0.54  0.88  0.78    0.94      -0.09
## SNRU              0.84  0.80  1.00  0.36  0.69  0.71  0.73    0.84      -0.09
## TIPS              0.61  0.55  0.36  1.00  0.19  0.67  0.54    0.65      -0.27
## BCTR              0.55  0.54  0.69  0.19  1.00  0.42  0.43    0.59      -0.04
## NTGRE             0.93  0.88  0.71  0.67  0.42  1.00  0.83    0.95      -0.20
## SPXTR             0.86  0.78  0.73  0.54  0.43  0.83  1.00    0.84      -0.15
## sp_real           0.96  0.94  0.84  0.65  0.59  0.95  0.84    1.00      -0.17
## median_cpi       -0.14 -0.09 -0.09 -0.27 -0.04 -0.20 -0.15   -0.17       1.00
## trimmed_mean_cpi -0.10 -0.07 -0.03 -0.23  0.09 -0.14 -0.13   -0.12       0.88
## cpi               0.01  0.01  0.06 -0.20  0.25 -0.01 -0.03    0.02       0.48
## core_cpi         -0.04 -0.04 -0.04 -0.12  0.15 -0.05 -0.09   -0.04       0.65
## headline_shock    0.09  0.07  0.12 -0.08  0.31  0.10  0.05    0.12       0.00
##                  trimmed_mean_cpi   cpi core_cpi headline_shock
## NTRAA                       -0.10  0.01    -0.04           0.09
## SPGI                        -0.07  0.01    -0.04           0.07
## SNRU                        -0.03  0.06    -0.04           0.12
## TIPS                        -0.23 -0.20    -0.12          -0.08
## BCTR                         0.09  0.25     0.15           0.31
## NTGRE                       -0.14 -0.01    -0.05           0.10
## SPXTR                       -0.13 -0.03    -0.09           0.05
## sp_real                     -0.12  0.02    -0.04           0.12
## median_cpi                   0.88  0.48     0.65           0.00
## trimmed_mean_cpi             1.00  0.72     0.80           0.33
## cpi                          0.72  1.00     0.73           0.87
## core_cpi                     0.80  0.73     1.00           0.47
## headline_shock               0.33  0.87     0.47           1.00

Testing correlations

real_df %>% 
  cor.test(~ NTRAA + cpi, data = .)
## 
##  Pearson's product-moment correlation
## 
## data:  NTRAA and cpi
## t = 0.33064, df = 58, p-value = 0.7421
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.2128952  0.2940602
## sample estimates:
##        cor 
## 0.04337425
real_df %>% 
  cor.test(~ sp_real + cpi, data = .)
## 
##  Pearson's product-moment correlation
## 
## data:  sp_real and cpi
## t = 0.21483, df = 58, p-value = 0.8307
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.2273550  0.2801166
## sample estimates:
##        cor 
## 0.02819756

Scatterplot: NTRAA vs. CPI

real_df %>% 
  ggplot(aes(x = cpi, y = NTRAA)) +
  geom_point() +
  geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

real_df %>% 
  ggplot(aes(x = cpi, y = sp_real)) +
  geom_point() +
  geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

Regressions on CPI

real_long_df %>% 
  lm(NTRAA ~ cpi, data = .) %>% 
  confint()
##                  2.5 %   97.5 %
## (Intercept) -0.9228037 1.321700
## cpi         -2.7160342 3.104141
  real_long_df %>% 
  lm(sp_real ~ cpi, data = .) %>% 
  confint()
##                  2.5 %   97.5 %
## (Intercept) -0.4761304 1.272969
## cpi         -2.0090178 2.526535
  real_long_df %>% 
  lm(SPGI ~ cpi, data = .) %>% 
  confint()
##                  2.5 %   97.5 %
## (Intercept) -0.8514538 1.547949
## cpi         -2.8898350 3.332007
  real_long_df %>% 
  lm(SNRU ~ cpi, data = .) %>% 
  confint()
##                 2.5 %   97.5 %
## (Intercept) -1.337006 1.709129
## cpi         -2.651250 5.247619
  real_long_df %>% 
  lm(NTGRE ~ cpi, data = .) %>% 
  confint()
##                 2.5 %   97.5 %
## (Intercept) -1.120958 1.520157
## cpi         -3.595476 3.253143

Regressions on median_cpi

real_long_df %>% 
  lm(NTRAA ~ median_cpi, data = .) %>% 
  summary()
## 
## Call:
## lm(formula = NTRAA ~ median_cpi, data = .)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -17.9490  -2.3760   0.2459   2.2716  10.4962 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)    1.554      0.970   1.602    0.112
## median_cpi    -4.439      3.001  -1.479    0.142
## 
## Residual standard error: 4.242 on 107 degrees of freedom
## Multiple R-squared:  0.02003,    Adjusted R-squared:  0.01088 
## F-statistic: 2.187 on 1 and 107 DF,  p-value: 0.1421
  real_long_df %>% 
  lm(sp_real ~ median_cpi, data = .) %>% 
  summary()
## 
## Call:
## lm(formula = sp_real ~ median_cpi, data = .)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -16.6092  -1.5555  -0.0703   1.6713   7.2624 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)   1.6937     0.7525   2.251   0.0264 *
## median_cpi   -4.1796     2.3282  -1.795   0.0754 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.291 on 107 degrees of freedom
## Multiple R-squared:  0.02924,    Adjusted R-squared:  0.02017 
## F-statistic: 3.223 on 1 and 107 DF,  p-value: 0.07545
  real_long_df %>% 
  lm(SPGI ~ median_cpi, data = .) %>% 
  summary()
## 
## Call:
## lm(formula = SPGI ~ median_cpi, data = .)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -23.6447  -2.5862   0.5844   2.4062  11.5538 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)    1.327      1.043   1.273    0.206
## median_cpi    -3.137      3.227  -0.972    0.333
## 
## Residual standard error: 4.561 on 107 degrees of freedom
## Multiple R-squared:  0.008753,   Adjusted R-squared:  -0.0005114 
## F-statistic: 0.9448 on 1 and 107 DF,  p-value: 0.3332
  real_long_df %>% 
  lm(SNRU ~ median_cpi, data = .) %>% 
  summary()
## 
## Call:
## lm(formula = SNRU ~ median_cpi, data = .)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -19.3324  -3.6105   0.0702   3.9674  15.9592 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)    1.622      1.327   1.222    0.225
## median_cpi    -3.716      4.107  -0.905    0.368
## 
## Residual standard error: 5.805 on 107 degrees of freedom
## Multiple R-squared:  0.007595,   Adjusted R-squared:  -0.001679 
## F-statistic: 0.8189 on 1 and 107 DF,  p-value: 0.3675
  real_long_df %>% 
  lm(NTGRE ~ median_cpi, data = .) %>% 
  summary()
## 
## Call:
## lm(formula = NTGRE ~ median_cpi, data = .)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -22.3505  -3.0062   0.2156   2.2447  12.1389 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)    2.299      1.130   2.034   0.0444 *
## median_cpi    -7.307      3.497  -2.090   0.0390 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.942 on 107 degrees of freedom
## Multiple R-squared:  0.03921,    Adjusted R-squared:  0.03023 
## F-statistic: 4.366 on 1 and 107 DF,  p-value: 0.03902