import_predictors.R (5363B)
1 #!/usr/bin/env Rscript 2 # 3 # import_predictors.R 4 # 5 # This code creates or imports the predictors of aggregate equity returns 6 # There are three parts for T-bill, D/P ratio and cay 7 # 8 # 1. T-bill comes from the H15 release of the FRB 9 # we downloaded it directly from FRED at https://fred.stlouisfed.org/series/TB3MS 10 # 11 # 2. cay comes from Martin Lettau's website at http://faculty.haas.berkeley.edu/lettau/data_cay.html 12 # 13 # 3. D-P ratio is estimated from the MSI CRSP Files 14 # We use a method of continuously reinvested dividends 15 # See attached LaTeX file for explanations of the procedure 16 # Data is from CRSP and available at /wrds/crsp/sasdata/a_stock/msi.sas7bdat 17 # 18 # 4. Estimate future excess returns: we use a horizon of three years in the paper 19 # 20 # (c) Valentin Haddad, Erik Loualiche & Matthew Plosser 21 # 22 # Last updated on June 4th 2019 23 # 24 ################################################################################## 25 26 27 ################################################################################## 28 message("Log file for code executed at\n") 29 message(format(Sys.time(), "%a %b %d %X %Y")) 30 ################################################################################## 31 32 33 ################################################################################## 34 # APPEND REQUIRED PACKAGES 35 36 # See this https://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them 37 using<-function(...) { 38 libs<-unlist(list(...)) 39 req<-unlist(lapply(libs,require,character.only=TRUE)) 40 need<-libs[req==FALSE] 41 if(length(need)>0){ 42 install.packages(need) 43 lapply(need,require,character.only=TRUE) 44 } 45 } 46 47 package_to_load <- c("crayon", "devtools", "alfred", "haven", "dplyr", 48 "stringr", "lubridate", "RcppRoll", "statar", "data.table") 49 using(package_to_load) 50 51 check_file = file.exists("log/R-session-info.log.R") 52 sink("log/R-session-info.log.R", append=check_file) 53 cat(bold("\n\n# -----\n# Session info for import_predictors\n\n")) 54 session_info() 55 sink() 56 ################################################################################## 57 58 59 ################################################################################## 60 # 1. TREASURIES 61 dt_tbill <- get_fred_series("TB3MS", "rf", observation_start = "1950-01-01", observation_end = "2020-12-31") %>% data.table 62 dt_tbill <- dt_tbill[, .(dateym=year(date)*100+month(date), rf=rf/100)] 63 dt_tbill[] 64 # fwrite(dt_tbill, "./input/tbill.csv") 65 ################################################################################## 66 67 68 ################################################################################## 69 # 2. CAY 70 dt_cay <- fread("./input/cay_current.csv", skip=0, header=T) 71 setnames(dt_cay, c("date", "c", "w", "y", "cay")) 72 dt_cay <- dt_cay[, .(date_y=year(date), month = month(date), cay) ] 73 dt_cay <- dt_cay[, .(dateym=date_y*100+month, cay) ] 74 dt_cay[] 75 ################################################################################## 76 77 78 ################################################################################## 79 # 3. Dividend-Price RATIO 80 dt_msi <- read_sas("./input/msi.sas7bdat") %>% data.table 81 dt_msi <- dt_msi[, .(date=DATE, vwretd, vwretx) ] 82 fwrite(dt_msi, "./output/msi.csv") # SAVED HERE IF YOU NEED IT 83 84 # ESTIMATE THE DP RATIO 85 dt_dp <- dt_msi[, .(date, vwretx, vwretd, vwrx=1+vwretx, vwrd=1+vwretd) ] 86 dt_dp[, `:=`(vwrx=1+vwretx, vwrd=1+vwretd) ] 87 dt_dp[, `:=`(dpvw = 100 * (vwretd-vwretx) / (1+vwretx) ) ] 88 dt_dp[, `:=`(retd_retx = (1+vwretd) / (1+vwretx) ) ] 89 dt_dp[, `:=`(datem = as.monthly(date)) ] 90 dt_dp[, dp := 0 ] 91 for (i in seq(11,0)){ 92 dt_dp[, dp := (dp*tlag(retd_retx, i, time=datem) + tlag(dpvw, i, time=datem)) ] 93 } 94 dt_dp <- dt_dp[, .(dateym=year(datem)*100+month(datem), dp=dp/100)] 95 dt_dp[] 96 ################################################################################## 97 98 99 ################################################################################## 100 # ESTIMATE FUTURE EXCESS RETURNS 101 dt_rmrf <- fread("./output/msi.csv") %>% data.table 102 dt_rmrf <- dt_rmrf[, .(dateym=year(date)*100+month(date), retm=vwretd) ] 103 dt_rmrf <- merge(dt_rmrf, dt_tbill, by = "dateym") 104 105 dt_rmrf[, lead1_retm := shift(retm, 1, type="lead") ] 106 dt_rmrf[, retm_y := exp( roll_sum(log(1+lead1_retm), n=12, align="left", fill=NA) ) - 1 ] 107 dt_rmrf[, rf_y := (1+rf)^(1/4) * (1+shift(rf, 3, type="lead"))^(1/4) * 108 (1+shift(rf, 6, type="lead"))^(1/4) * (1+shift(rf, 9, type="lead"))^(1/4) - 1 ] 109 dt_rmrf[, rmrf_y3 := 1 * ( 110 ( (1+retm_y) * (1 + shift(retm_y, 12, type="lead")) * (1 + shift(retm_y, 24, type="lead")) )^(1/3) - 111 ( ((1+rf_y) * (1 + shift(rf_y, 12, type="lead")) * (1 + shift(rf_y, 24, type="lead")) )^(1/3) - 1) -1) ] 112 113 dt_rmrf <- dt_rmrf[, .(dateym, rmrf_y3) ] 114 dt_rmrf[ !is.na(rmrf_y3) ] 115 ################################################################################## 116 117 118 ################################################################################## 119 # MERGE THE PREDICTORS 120 dt_predict <- merge(dt_dp, dt_tbill, by = c("dateym")) 121 dt_predict <- merge(dt_predict, dt_rmrf, by = c("dateym"), all.x = T) 122 dt_predict <- merge(dt_predict, dt_cay, by = c("dateym"), all.x = T) 123 dt_predict <- dt_predict[ !is.na(rmrf_y3) ] 124 dt_predict <- dt_predict[ !is.na(cay) ] 125 dt_predict[] 126 127 fwrite(dt_predict, "./tmp/predict.csv") 128 ################################################################################## 129 130 131 132 133 134 135 136 137 138 139 140 141 142