Skip to content

Commit

Permalink
🚀 RELEASE: update cay to 2019q3
Browse files Browse the repository at this point in the history
Also add new index msi.csv
Predictor now goes to 2016q4
  • Loading branch information
eloualiche committed Mar 23, 2020
1 parent cafb1aa commit 48a7572
Show file tree
Hide file tree
Showing 13 changed files with 1,047 additions and 897 deletions.
21 changes: 14 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,43 @@
# Makefile for PREDICTING THE EQUITY RISK PREMIUM
#
# Created on June 04th 2019
# Last modified on June 14th 2019
# Last modified on March 23rd 2020
## --------------------------------------------------------------------------------------------------------



## --------------------------------------------------------------------------------------------------------
## LOAD A FEW OPTIONS
-include ./rules.mk
## --------------------------------------------------------------------------------------------------------

# --------------------------------------------------------------------------------------------------------
## ALL
all: output/predict.csv readme.md

##
## DOWNLOAD THE DATA FROM WRDS
## Edit rules.mk to change your user name under WRDS_USERNAME
input/msi.sas7bdat:
rsync -aPvzh --stats --human-readable XXXX@wrds-cloud.wharton.upenn.edu:/wrds/crsp/sasdata/a_stock/msi.sas7bdat ./input/
$(call colorecho,"Download stock market index from crsp ...")
rsync -aPvzh --stats --human-readable ${WRDS_USERNAME}@wrds-cloud.wharton.upenn.edu:/wrds/crsp/sasdata/a_stock/msi.sas7bdat ./input/
@echo

## GENERATE PREDICTORS
tmp/predict.csv: src/import_predictors.R input/cay_current.csv input/msi.sas7bdat
$(call colorecho,"Import and merge all return predictors ...")
R CMD BATCH $(R_OPTS) src/import_predictors.R log/import_predictors.log.R
@echo

## RUN REGRESSIONS
output/predict.csv: src/rp_measure.R tmp/predict.csv
$(call colorecho,"Estimate predictive regression ...")
R CMD BATCH $(R_OPTS) src/rp_measure.R log/rp_measure.log.R
@echo

## OUTPUT RESULTS
readme.md: src/readme_in.md
readme.md: src/readme_in.md output/predict.png tmp/reg_update.txt
$(call colorecho,"Update readme file ...")
cat src/readme_in.md tmp/reg_update.txt > readme.md
@echo

Expand All @@ -47,10 +58,6 @@ clean:
rm -rf readme.md

##
## --------------------------------------------------------------------------------------------------------
SHELL := /bin/bash
R_OPTS := --vanilla
# DATE := `date '+%Y-%m-%d %H:%M:%S'`
# --------------------------------------------------------------------------------------------------------


Expand Down
537 changes: 272 additions & 265 deletions input/cay_current.csv

Large diffs are not rendered by default.

238 changes: 130 additions & 108 deletions log/R-session-info.log.R

Large diffs are not rendered by default.

146 changes: 86 additions & 60 deletions log/import_predictors.log.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
Expand Down Expand Up @@ -48,18 +48,33 @@ Type 'q()' to quit R.
Log file for code executed at

> message(format(Sys.time(), "%a %b %d %X %Y"))
Tue Aug 27 21:49:24 2019
Mon Mar 23 08:59:59 2020
> ##################################################################################
>
>
> ##################################################################################
> # APPEND REQUIRED PACKAGES
> library(crayon)
> library(devtools)
>
> library(alfred)
> library(haven)
> library(dplyr)
> # See this https://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them
> using<-function(...) {
+ libs<-unlist(list(...))
+ req<-unlist(lapply(libs,require,character.only=TRUE))
+ need<-libs[req==FALSE]
+ if(length(need)>0){
+ install.packages(need)
+ lapply(need,require,character.only=TRUE)
+ }
+ }
>
> package_to_load <- c("crayon", "devtools", "alfred", "haven", "dplyr",
+ "stringr", "lubridate", "RcppRoll", "statar", "data.table")
> using(package_to_load)
Loading required package: crayon
Loading required package: devtools
Loading required package: usethis
Loading required package: alfred
Loading required package: haven
Loading required package: dplyr

Attaching package: ‘dplyr’

Expand All @@ -71,18 +86,18 @@ The following objects are masked from ‘package:base’:

intersect, setdiff, setequal, union

> library(stringr)
> library(lubridate)
Loading required package: stringr
Loading required package: lubridate

Attaching package: ‘lubridate’

The following object is masked from ‘package:base’:

date

> library(RcppRoll)
> library(statar)
> library(data.table)
Loading required package: RcppRoll
Loading required package: statar
Loading required package: data.table

Attaching package: ‘data.table’

Expand All @@ -93,46 +108,57 @@ The following objects are masked from ‘package:lubridate’:

The following objects are masked from ‘package:dplyr’:

between, coalesce, first, last
between, first, last

>
> check_file = file.exists("log/R-session-info.log.R")
> sink("log/R-session-info.log.R", append=check_file)
> cat(bold("\n\n# -----\n# Session info for import_predictors\n\n"))
> session_info()
Session info ------------------------------------------------------------------
Packages ----------------------------------------------------------------------
> sink()
> ##################################################################################
>
>
> ##################################################################################
> # 1. TREASURIES
> dt_tbill <- get_fred_series("TB3MS", "rf", observation_start = "1950-01-01", observation_end = "2019-05-31") %>% data.table
> dt_tbill <- get_fred_series("TB3MS", "rf", observation_start = "1950-01-01", observation_end = "2020-12-31") %>% data.table
> dt_tbill <- dt_tbill[, .(dateym=year(date)*100+month(date), rf=rf/100)]
> dt_tbill[]
dateym rf
1: 195001 0.0107
2: 195002 0.0112
3: 195003 0.0112
4: 195004 0.0115
5: 195005 0.0116
---
838: 201910 0.0165
839: 201911 0.0154
840: 201912 0.0154
841: 202001 0.0152
842: 202002 0.0152
> # fwrite(dt_tbill, "./input/tbill.csv")
> ##################################################################################
>
>
> ##################################################################################
> # 2. CAY
> dt_cay <- fread("./input/cay_current.csv", skip=1, header=T)
> dt_cay <- dt_cay[, .(date_y=as.integer(str_sub(date, 1, 4)),
+ quarter=as.integer(str_sub(date, 5, 6)), cay=cay) ]
> dt_cay <- dt_cay[, .(dateym=date_y*100+quarter*3, cay) ]
> dt_cay
> dt_cay <- fread("./input/cay_current.csv", skip=0, header=T)
> setnames(dt_cay, c("date", "c", "w", "y", "cay"))
> dt_cay <- dt_cay[, .(date_y=year(date), month = month(date), cay) ]
> dt_cay <- dt_cay[, .(dateym=date_y*100+month, cay) ]
> dt_cay[]
dateym cay
1: 195203 0.01646544
2: 195206 0.02551783
3: 195209 0.01633620
4: 195212 0.02542006
5: 195303 0.02543387
1: 195203 0.01510493
2: 195206 0.02483727
3: 195209 0.01484008
4: 195212 0.02216598
5: 195303 0.02152118
---
259: 201609 -0.02916950
260: 201612 -0.02042846
261: 201703 -0.02529660
262: 201706 -0.02655350
263: 201709 -0.02760623
267: 201809 -0.02934508
268: 201812 -0.02020734
269: 201903 -0.04435449
270: 201906 -0.03764155
271: 201909 -0.03665922
> ##################################################################################
>
>
Expand Down Expand Up @@ -161,11 +187,11 @@ Packages ----------------------------------------------------------------------
4: 192603 NA
5: 192604 NA
---
1113: 201808 0.02033382
1114: 201809 0.02040150
1115: 201810 0.02047582
1116: 201811 0.02050859
1117: 201812 0.02114445
1125: 201908 0.02129861
1126: 201909 0.02150184
1127: 201910 0.02149657
1128: 201911 0.02113526
1129: 201912 0.02085788
> ##################################################################################
>
>
Expand All @@ -185,18 +211,18 @@ Packages ----------------------------------------------------------------------
>
> dt_rmrf <- dt_rmrf[, .(dateym, rmrf_y3) ]
> dt_rmrf[ !is.na(rmrf_y3) ]
dateym rmrf_y3
1: 195001 0.19164596
2: 195002 0.18441326
3: 195003 0.17389623
4: 195004 0.14728401
5: 195005 0.13288460
---
788: 201508 0.13987039
789: 201509 0.15268617
790: 201510 0.09634409
791: 201511 0.10151984
792: 201512 0.07465740
dateym rmrf_y3
1: 195001 0.1916460
2: 195002 0.1844133
3: 195003 0.1738962
4: 195004 0.1472840
5: 195005 0.1328846
---
800: 201608 0.0992516
801: 201609 0.1035328
802: 201610 0.1184933
803: 201611 0.1160087
804: 201612 0.1191020
> ##################################################################################
>
>
Expand All @@ -209,17 +235,17 @@ Packages ----------------------------------------------------------------------
> dt_predict <- dt_predict[ !is.na(cay) ]
> dt_predict[]
dateym dp rf rmrf_y3 cay
1: 195203 0.05817138 0.0159 0.18092953 0.01646544
2: 195206 0.05739649 0.0170 0.21642173 0.02551783
3: 195209 0.05709103 0.0171 0.23193277 0.01633620
4: 195212 0.05522191 0.0209 0.22202729 0.02542006
5: 195303 0.05455042 0.0201 0.26058722 0.02543387
1: 195203 0.05817138 0.0159 0.18092953 0.01510493
2: 195206 0.05739649 0.0170 0.21642173 0.02483727
3: 195209 0.05709103 0.0171 0.23193277 0.01484008
4: 195212 0.05522191 0.0209 0.22202729 0.02216598
5: 195303 0.05455042 0.0201 0.26058722 0.02152118
---
252: 201412 0.02094913 0.0003 0.09787760 -0.02747550
253: 201503 0.02111608 0.0003 0.08734601 -0.03462231
254: 201506 0.02111542 0.0002 0.09928718 -0.03462943
255: 201509 0.02153820 0.0002 0.15268617 -0.02656083
256: 201512 0.02191271 0.0023 0.07465740 -0.03519129
256: 201512 0.02191273 0.0023 0.07466284 -0.02350179
257: 201603 0.02265601 0.0029 0.11699368 -0.02321807
258: 201606 0.02307582 0.0027 0.11884748 -0.01823511
259: 201609 0.02297658 0.0029 0.10353283 -0.01942223
260: 201612 0.02284611 0.0051 0.11910203 -0.01902802
>
> fwrite(dt_predict, "./tmp/predict.csv")
> ##################################################################################
Expand All @@ -240,4 +266,4 @@ Packages ----------------------------------------------------------------------
>
> proc.time()
user system elapsed
1.400 0.153 2.169
2.462 0.335 4.291
Loading

0 comments on commit 48a7572

Please sign in to comment.