library(magrittr)
library(xts)
library(vars)
# BM14 Replication Data. Constructing the database:
BM14 = merge(BM14_M, BM14_Q)
BM14[, BM14_Models$log_trans] %<>% log()
BM14[, BM14_Models$freq == "M"] %<>% diff()
BM14[, BM14_Models$freq == "Q"] %<>% diff(3)
### Small Model ---------------------------------------
# IC for number of factors
IC_small = ICr(BM14[, BM14_Models$small], max.r = 5)
plot(IC_small)
screeplot(IC_small)
# I take 2 factors. Now number of lags
VARselect(IC_small$F_pca[, 1:2])
# Estimating the model with 2 factors and 3 lags
dfm_small = DFM(BM14[, BM14_Models$small], 2, 3)
# Inspecting the model
summary(dfm_small)
plot(dfm_small) # Factors and data
plot(dfm_small, method = "all", type = "individual") # Factor estimates
plot(dfm_small, type = "residual") # Residuals from factor predictions
# 10 periods ahead forecast
plot(predict(dfm_small), xlim = c(300, 370))
### Medium-Sized Model ---------------------------------
# IC for number of factors
IC_medium = ICr(BM14[, BM14_Models$medium])
plot(IC_medium)
screeplot(IC_medium)
# I take 3 factors. Now number of lags
VARselect(IC_medium$F_pca[, 1:3])
# Estimating the model with 3 factors and 3 lags
dfm_medium = DFM(BM14[, BM14_Models$medium], 3, 3)
# Inspecting the model
summary(dfm_medium)
plot(dfm_medium) # Factors and data
plot(dfm_medium, method = "all", type = "individual") # Factor estimates
plot(dfm_medium, type = "residual") # Residuals from factor predictions
# 10 periods ahead forecast
plot(predict(dfm_medium), xlim = c(300, 370))
### Large Model ---------------------------------
# \donttest{
# IC for number of factors
IC_large = ICr(BM14)
plot(IC_large)
screeplot(IC_large)
# I take 6 factors. Now number of lags
VARselect(IC_large$F_pca[, 1:6])
# Estimating the model with 6 factors and 3 lags
dfm_large = DFM(BM14, 6, 3)
# Inspecting the model
summary(dfm_large)
plot(dfm_large) # Factors and data
# plot(dfm_large, method = "all", type = "individual") # Factor estimates
plot(dfm_large, type = "residual") # Residuals from factor predictions
# 10 periods ahead forecast
plot(predict(dfm_large), xlim = c(300, 370))
# }
Run the code above in your browser using DataLab