-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.Rmd
145 lines (109 loc) · 6.42 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
# FFdownload <a href='https://github.com/sstoeckl/FFdownload'><img src='man/figures/logo.png' align="right" height="139" /></a>
<!-- badges: start -->
[![Project Status](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R-CMD-check](https://github.com/sstoeckl/ffdownload/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/sstoeckl/ffdownload/actions/workflows/R-CMD-check.yaml)
[![CRAN_latest_release_date](https://www.r-pkg.org/badges/last-release/FFdownload)](https://cran.r-project.org/package=FFdownload)
[![CRAN status](https://www.r-pkg.org/badges/version/FFdownload)](https://CRAN.R-project.org/package=FFdownload)
[![CRAN downloads](https://cranlogs.r-pkg.org/badges/grand-total/FFdownload)](https://cranlogs.r-pkg.org/badges/grand-total/FFdownload)
[![CRAN downloads last month](http://cranlogs.r-pkg.org/badges/FFdownload)](https://cran.r-project.org/package=FFdownload)
[![CRAN downloads last week](http://cranlogs.r-pkg.org/badges/last-week/FFdownload)](https://cran.r-project.org/package=FFdownload)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![Website - pkgdown](https://img.shields.io/website-up-down-green-red/https/sstoeckl.github.io/ffdownload.svg)](https://sstoeckl.github.io/ffdownload/)
<!-- badges: end -->
# `R` Code to download Datasets from [Kenneth French's famous website](http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html).
# Update
Version 1.1.1 corrects a small error for publication on CRAN.
## Motivation
One often needs those datasets for further empirical work and it is a tedious effort to download the (zipped) csv, open and then manually separate the contained datasets. This package downloads them automatically, and converts them to a list of xts-objects that contain all the information from the csv-files.
## Contributors
Original code from MasimovR <https://github.com/MasimovR/>. Was then heavily redacted by me.
## Installation
You can install FFdownload from CRAN with
```{r cran-installation, eval = FALSE}
install.packages("FFdownload")
```
or directly from github with:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("sstoeckl/FFdownload")
```
## Examples
### Example 0: Easy Access
This is the quick-starter example. It just retrieves the data and provides it for easy usage!
```{r example_0, eval=TRUE, message=FALSE}
library(FFdownload)
library(tidyverse)
FFdownload(inputlist = c("F-F_Research_Data_5_Factors_2x3"), output_file = "FFdata.RData", format = "tbl")
load("FFdata.RData")
FFdata$`x_F-F_Research_Data_5_Factors_2x3`$monthly$Temp2 |>
tidyr::pivot_longer(cols = -date, names_to = "FFFactors", values_to = "Value") |>
group_by(FFFactors) |> mutate(Price=cumprod(1+Value/100)) |>
ggplot2::ggplot(aes(x = date, col = FFFactors, y = Price)) + geom_line(lwd=1.2) +
theme_bw() + theme(legend.position="bottom")
```
### Example 1: Monthly files
In this example, we use `FFDwonload` to
1. get a list of all available monthly zip-files and save that files as *temp.txt*.
```{r example_1a, eval=TRUE, message=FALSE}
temptxt <- tempfile(fileext = ".txt")
# example_1: Use FFdownload to get a list of all monthly zip-files. Save that list as temptxt.
FFdownload(exclude_daily=TRUE,download=FALSE,download_only=TRUE,listsave=temptxt)
```
```{r example_1b, message=FALSE, warning=FALSE}
FFlist <- readr::read_csv(temptxt) %>% dplyr::select(2) %>% dplyr::rename(Files=x)
FFlist %>% dplyr::slice(1:3,(dplyr::n()-2):dplyr::n())
```
2. Next, after inspecting the list we specify a vector `inputlist` to only download the datasets we actually need.
```{r example_2, message=FALSE, warning=FALSE}
tempd <- tempdir()
inputlist <- c("F-F_Research_Data_Factors","F-F_Momentum_Factor","F-F_ST_Reversal_Factor","F-F_LT_Reversal_Factor")
FFdownload(exclude_daily=TRUE,tempd=tempd,download=TRUE,download_only=TRUE,inputlist=inputlist)
```
3. In the final step we process the downloaded files (formatting the output data.frames as tibbles for direct proceeding):
```{r example_3a, message=FALSE}
tempf <- paste0(tempd,"\\FFdata.RData")
getwd()
FFdownload(output_file = tempf, exclude_daily=TRUE,tempd=tempd,download=FALSE,
download_only=FALSE,inputlist = inputlist, format="tbl")
```
4. Then we check that everything worked and output a combined file of monthly factors (only show first 5 rows).
```{r example_3b, message=FALSE}
library(timetk)
load(file = tempf)
FFdata$`x_F-F_Research_Data_Factors`$monthly$Temp2 %>%
left_join(FFdata$`x_F-F_Momentum_Factor`$monthly$Temp2, by="date") %>%
left_join(FFdata$`x_F-F_LT_Reversal_Factor`$monthly$Temp2,by="date") %>%
left_join(FFdata$`x_F-F_ST_Reversal_Factor`$monthly$Temp2,by="date") %>% head()
```
5. No we do the same with annual data:
```{r example_3c, message=FALSE}
FFfive <- FFdata$`x_F-F_Research_Data_Factors`$annual$`annual_factors:_january-december` %>%
left_join(FFdata$`x_F-F_Momentum_Factor`$annual$`january-december` ,by="date") %>%
left_join(FFdata$`x_F-F_LT_Reversal_Factor`$annual$`january-december`,by="date") %>%
left_join(FFdata$`x_F-F_ST_Reversal_Factor`$annual$`january-december` ,by="date")
FFfive %>% head()
```
6. Finally we plot wealth indices for 6 of these factors:
```{r FFpic}
FFfive %>%
pivot_longer(Mkt.RF:ST_Rev,names_to="FFVar",values_to="FFret") %>% mutate(FFret=FFret/100,date=as.Date(date)) %>%
filter(date>="1960-01-01",!FFVar=="RF") %>% group_by(FFVar) %>% arrange(FFVar,date) %>%
mutate(FFret=ifelse(date=="1960-01-01",1,FFret),FFretv=cumprod(1+FFret)-1) %>%
ggplot(aes(x=date,y=FFretv,col=FFVar,type=FFVar)) + geom_line(lwd=1.2) + scale_y_log10() +
labs(title="FF5 Factors plus Momentum", subtitle="Cumulative wealth plots",ylab="cum. returns") +
scale_colour_viridis_d("FFvar") +
theme_bw() + theme(legend.position="bottom")
```
# Acknowledgment
I am grateful to **Kenneth French** for providing all this great research data on his website! Our lives would be so much harder without this *boost* for productivity. I am also grateful for the kind conversation with Kenneth with regard to this package: He appreciates my work on this package giving others easier access to his data sets!