knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-" )
R
Code to download Datasets from Kenneth French's famous website.Version 1.1.1 corrects a small error for publication on CRAN.
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.
Original code from MasimovR https://github.com/MasimovR/. Was then heavily redacted by me.
You can install FFdownload from CRAN with
install.packages("FFdownload")
or directly from github with:
# install.packages("devtools") devtools::install_github("sstoeckl/FFdownload")
This is the quick-starter example. It just retrieves the data and provides it for easy usage!
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")
In this example, we use FFDwonload
to
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)
FFlist <- readr::read_csv(temptxt) %>% dplyr::select(2) %>% dplyr::rename(Files=x) FFlist %>% dplyr::slice(1:3,(dplyr::n()-2):dplyr::n())
inputlist
to only download the datasets we actually need.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)
tempf <- paste0(tempd,"\\FFdata.RData") getwd() FFdownload(output_file = tempf, exclude_daily=TRUE,tempd=tempd,download=FALSE, download_only=FALSE,inputlist = inputlist, format="tbl")
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()
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()
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")
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!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.