Learn R Programming

tidyfinance

This repository contains an R package that collects helper functions for developers and researchers familiar with Tidy Finance with R. The functions provide shortcuts to selected issues that the book discusses in detail.

Installation

You can install the released version of tidyfinance from CRAN via:

install.packages("tidyfinance")

You can install the development version of tidyfinance from GitHub via:

# install.packages("pak")
pak::pak("tidy-finance/r-tidyfinance")

Usage

Download Data

The main functionality of the tidyfinance package centers around data download. You can download most of the data that we used in Tidy Finance with R using the download_data() function or its children. For instance, both functions give the same result:

download_data(
  type = "factors_ff_3_monthly", 
  start_date = "2000-01-01", 
  end_date = "2020-12-31"
)

download_data_factors_ff(
  type = "factors_ff_3_monthly", 
  start_date = "2000-01-01", 
  end_date = "2020-12-31"
)

You can also download data directly from WRDS (if you have set your credentials via Sys.setenv(WRDS_USER = "your_username", WRDS_PASSWORD = "your_password")), e.g.,

download_data(
  type = "wrds_compustat_annual", 
  start_date = "2000-01-01", 
  end_date = "2020-12-31"
)

If you want to fetch additional columns that are not included in our default selection, then pass them as additional arguments in the corresponding child function:

download_data_wrds_compustat(
  type = "wrds_compustat_annual",  
  start_date = "2000-01-01", 
  end_date = "2020-12-31",
  additional_columns = c("acoxar", "amc", "aldo")
)

You can get a list of all currently supported types via list_supported_types(). Please open an issue on GitHub to request additional supported types.

Other Helpers

We include functions to check out content from tidy-finance.org:

list_tidy_finance_chapters()
open_tidy_finance_website()

There are also some simple helpers for regression analyses:

winsorize()
trim()
create_summary_statistics()

We also include (experimental) functions that can be used for different applications, but note that they might heavily change in future package versions as we try to make them more general:

# For portfolio sorts
?assign_portfolio()

# For model estimation
?estimate_model()

# For beta estimation
?estimate_betas()

# For beta estimation
?estimate_fama_macbeth()

Copy Link

Version

Install

install.packages('tidyfinance')

Monthly Downloads

517

Version

0.4.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Christoph Scheuch

Last Published

December 17th, 2024

Functions in tidyfinance (0.4.3)

download_data_osap

Download and Process Open Source Asset Pricing Data
assign_portfolio

Assign Portfolios Based on Sorting Variable
list_tidy_finance_chapters

List Chapters of Tidy Finance
list_supported_types

List All Supported Dataset Types
list_supported_indexes

List Supported Indexes
download_data_constituents

Download Constituent Data
open_tidy_finance_website

Open Tidy Finance Website or Specific Chapter in Browser
list_supported_types_ff

List Supported Fama-French Dataset Types
list_supported_types_ff_legacy

List Supported Legacy Fama-French Dataset Types
download_data_factors

Download and Process Factor Data
get_wrds_connection

Establish a Connection to the WRDS Database
download_data_stock_prices

Download Stock Data
download_data_wrds_trace_enhanced

Download Enhanced TRACE Data from WRDS
download_data_wrds_fisd

Download Filtered FISD Data from WRDS
compute_portfolio_returns

Compute Portfolio Returns
lag_column

Lag a Column Based on Date and Time Range
download_data_wrds

Download Data from WRDS
download_data_wrds_ccm_links

Download CCM Links from WRDS
download_data

Download and Process Data Based on Type
disconnection_connection

Disconnect Database Connection
download_data_factors_q

Download and Process Global Q Factor Data
list_supported_types_other

List Supported Other Data Types
list_supported_types_macro_predictors

List Supported Macro Predictor Dataset Types
estimate_fama_macbeth

Estimate Fama-MacBeth Regressions
download_data_factors_ff

Download and Process Fama-French Factor Data
create_summary_statistics

Create Summary Statistics for Specified Variables
list_supported_types_q

List Supported Global Q Dataset Types
estimate_betas

Estimate Rolling Betas
list_supported_types_wrds

List Supported WRDS Dataset Types
winsorize

Winsorize a Numeric Vector
trim

Trim a Numeric Vector
download_data_wrds_compustat

Download Data from WRDS Compustat
download_data_wrds_crsp

Download Data from WRDS CRSP
tidyfinance-package

tidyfinance: Tidy Finance Helper Functions
set_wrds_credentials

Set WRDS Credentials
compute_breakpoints

Compute Breakpoints Based on Sorting Variable
compute_long_short_returns

Compute Long-Short Returns
add_lag_columns

Add Lagged Versions of Columns to a Data Frame
breakpoint_options

Create Breakpoint Options for Portfolio Sorting
check_supported_type

Check if a Dataset Type is Supported
data_options

Create Data Options
create_wrds_dummy_database

Create WRDS Dummy Database
download_data_macro_predictors

Download and Process Macro Predictor Data
download_data_fred

Download and Process Data from FRED
get_random_user_agent

Get a Random User Agent
estimate_model

Estimate Model Coefficients