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.
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("remotes")
remotes::install_github("tidy-finance/r-tidyfinance")
Load the package:
library(tidyfinance)
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.
The function always requires a domain argument and depending on the
domain typically also a dataset. For instance, to download monthly
Fama-French factors, you have to provide the dataset name according to
Ken French’s Data
Library:
download_data(
domain = "Fama-French",
dataset = "Fama/French 5 Factors (2x3) [Daily]",
start_date = "2000-01-01",
end_date = "2020-12-31"
)
#> # A tibble: 5,284 × 7
#> date mkt_excess smb hml rmw cma risk_free
#> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2000-01-03 -0.0071 -0.0009 -0.0131 -0.0146 -0.0071 0.0002
#> 2 2000-01-04 -0.0406 0.0034 0.0207 0.0053 0.0136 0.0002
#> 3 2000-01-05 -0.0009 0.0035 -0.0005 0.0044 0.0114 0.0002
#> 4 2000-01-06 -0.0074 -0.0003 0.0124 0.0064 0.0122 0.0002
#> 5 2000-01-07 0.0321 -0.0089 -0.0157 -0.0083 -0.0099 0.0002
#> 6 2000-01-10 0.0175 0.0043 -0.0135 -0.022 -0.0024 0.0002
#> 7 2000-01-11 -0.0171 0.0034 0.0092 0.009 0.012 0.0002
#> 8 2000-01-12 -0.0069 -0.0022 0.0074 0.0041 0.0085 0.0002
#> 9 2000-01-13 0.0159 0.0048 -0.0084 -0.0172 -0.0102 0.0002
#> 10 2000-01-14 0.0114 0.0022 -0.0048 -0.0035 -0.0059 0.0002
#> # ℹ 5,274 more rows
For q factors, you provide the relevant file name:
download_data(
domain = "Global Q",
dataset = "q5_factors_monthly_2024",
start_date = "2000-01-01",
end_date = "2020-12-31"
)
#> # A tibble: 252 × 7
#> date risk_free mkt_excess me ia roe eg
#> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2000-01-01 0.00434 -0.0476 0.0562 0.0294 -0.0500 0.00638
#> 2 2000-02-01 0.00430 0.0246 0.221 -0.0539 -0.0755 -0.0311
#> 3 2000-03-01 0.00438 0.0523 -0.144 0.0183 0.0767 0.0356
#> 4 2000-04-01 0.00460 -0.0638 -0.0486 0.0379 0.0871 0.0510
#> 5 2000-05-01 0.00478 -0.0437 -0.0460 0.00508 0.000196 0.0370
#> 6 2000-06-01 0.00374 0.0466 0.103 -0.0338 -0.0479 -0.0172
#> 7 2000-07-01 0.00477 -0.0242 0.00995 0.0225 0.0343 0.0329
#> 8 2000-08-01 0.00500 0.0703 0.00449 -0.00800 -0.0208 -0.0101
#> 9 2000-09-01 0.00509 -0.0543 0.0158 0.0522 0.0209 0.00734
#> 10 2000-10-01 0.00522 -0.0274 -0.0222 0.0292 0.0734 0.0656
#> # ℹ 242 more rows
To download the Welch and Goyal (2008) macroeconomic predictors for monthly, quarterly, or annual frequency:
download_data(
domain = "Goyal-Welch",
dataset = "monthly",
start_date = "2000-01-01",
end_date = "2020-12-31"
)
#> # A tibble: 252 × 15
#> date rp_div dp dy ep de svar bm ntis tbl
#> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2000-01-01 -0.0244 -4.42 -4.48 -3.35 -1.08 0.00521 0.155 0.0254 0.0532
#> 2 2000-02-01 0.0866 -4.40 -4.42 -3.31 -1.09 0.00300 0.167 0.0274 0.0555
#> 3 2000-03-01 -0.0355 -4.49 -4.40 -3.38 -1.11 0.00668 0.150 0.0183 0.0569
#> 4 2000-04-01 -0.0269 -4.46 -4.49 -3.34 -1.12 0.00794 0.153 0.0119 0.0566
#> 5 2000-05-01 0.0194 -4.44 -4.46 -3.32 -1.13 0.00519 0.156 0.00918 0.0579
#> 6 2000-06-01 -0.0212 -4.47 -4.44 -3.33 -1.13 0.00236 0.157 0.00712 0.0569
#> 7 2000-07-01 0.0532 -4.46 -4.47 -3.30 -1.15 0.00207 0.156 0.00471 0.0596
#> 8 2000-08-01 -0.0595 -4.52 -4.46 -3.35 -1.17 0.00104 0.146 0.00467 0.0609
#> 9 2000-09-01 -0.0105 -4.48 -4.53 -3.29 -1.19 0.00160 0.154 0.00447 0.06
#> 10 2000-10-01 -0.0876 -4.47 -4.48 -3.30 -1.17 0.00557 0.149 0.00374 0.0611
#> # ℹ 242 more rows
#> # ℹ 5 more variables: lty <dbl>, ltr <dbl>, tms <dbl>, dfy <dbl>, infl <dbl>
To download data from Open Source Asset Pricing (OSAP):
download_data(
domain = "Open Source Asset Pricing",
start_date = "2020-01-01",
end_date = "2020-12-31"
)
#> # A tibble: 12 × 213
#> date am aop abnormal_accruals accruals accruals_bm activism1
#> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2020-01-01 -0.0790 -0.0255 0.0156 -1.40e-2 0.0567 NA
#> 2 2020-02-01 -0.0444 -0.0145 -0.0139 -1.11e-2 -0.0323 NA
#> 3 2020-03-01 -0.136 0.00547 -0.0361 1.74e-2 -0.0118 NA
#> 4 2020-04-01 -0.0333 -0.0113 0.0778 4.53e-2 -0.0238 NA
#> 5 2020-05-01 -0.117 0.0185 0.0207 -3.61e-3 -0.0268 NA
#> 6 2020-06-01 0.0165 -0.00604 0.0330 8.51e-5 0.0400 NA
#> 7 2020-07-01 -0.0366 -0.0913 -0.0209 3.00e-2 -0.0533 NA
#> 8 2020-08-01 0.0265 0.0191 -0.0249 9.04e-4 0.0223 NA
#> 9 2020-09-01 -0.0366 -0.0189 -0.0235 -1.53e-2 -0.0460 NA
#> 10 2020-10-01 0.0711 0.0304 -0.00682 -4.40e-3 0.0332 NA
#> 11 2020-11-01 0.0739 0.0202 0.0261 2.42e-2 0.193 NA
#> 12 2020-12-01 0.0148 0.000180 -0.00594 2.04e-2 -0.0292 NA
#> # ℹ 206 more variables: activism2 <dbl>, ad_exp <dbl>, age_ipo <dbl>,
#> # analyst_revision <dbl>, analyst_value <dbl>, announcement_return <dbl>,
#> # asset_growth <dbl>, bm <dbl>, bmdec <dbl>, bpebm <dbl>, beta <dbl>,
#> # beta_fp <dbl>, beta_liquidity_ps <dbl>, beta_tail_risk <dbl>,
#> # bid_ask_spread <dbl>, book_leverage <dbl>, brand_invest <dbl>,
#> # cboper_prof <dbl>, cf <dbl>, cpvol_spread <dbl>, cash <dbl>,
#> # cash_prod <dbl>, ch_asset_turnover <dbl>, ch_eq <dbl>, …
To download characteristic-managed portfolio (factor) returns from
Global Factor Data, select a region, the
factor content (a single factor, a theme, "all_themes", or
"all_factors"), a frequency, and a weighting scheme:
download_data(
domain = "Global Factor Data",
region = "usa",
factors = "all_factors",
frequency = "monthly",
weighting = "vw_cap",
start_date = "2020-01-01",
end_date = "2020-12-31"
)
#> # A tibble: 1,836 × 9
#> location name freq weighting direction n_stocks n_stocks_min date
#> <chr> <chr> <chr> <chr> <int> <int> <int> <date>
#> 1 usa age monthly vw_cap -1 2858 1082 2020-01-01
#> 2 usa age monthly vw_cap -1 2863 1080 2020-02-01
#> 3 usa age monthly vw_cap -1 2864 1078 2020-03-01
#> 4 usa age monthly vw_cap -1 2800 1077 2020-04-01
#> 5 usa age monthly vw_cap -1 2800 1072 2020-05-01
#> 6 usa age monthly vw_cap -1 2846 1116 2020-06-01
#> 7 usa age monthly vw_cap -1 2801 1112 2020-07-01
#> 8 usa age monthly vw_cap -1 2820 1109 2020-08-01
#> 9 usa age monthly vw_cap -1 2851 1107 2020-09-01
#> 10 usa age monthly vw_cap -1 2806 1106 2020-10-01
#> # ℹ 1,826 more rows
#> # ℹ 1 more variable: ret <dbl>
The dataset argument also gives access to the underlying long-short
portfolios ("portfolios"), industry returns ("industry"), and the
reference files "nyse_cutoffs" and "return_cutoffs":
download_data(
domain = "Global Factor Data",
dataset = "industry",
region = "usa",
classification = "gics",
start_date = "2020-01-01",
end_date = "2020-12-31"
)
#> # A tibble: 132 × 7
#> gics date n location ret freq weighting
#> <int> <date> <int> <chr> <dbl> <chr> <chr>
#> 1 30 2020-01-01 151 usa -0.0190 monthly vw_cap
#> 2 45 2020-01-01 473 usa 0.0277 monthly vw_cap
#> 3 10 2020-01-01 227 usa -0.143 monthly vw_cap
#> 4 35 2020-01-01 836 usa -0.0156 monthly vw_cap
#> 5 60 2020-01-01 40 usa -0.0148 monthly vw_cap
#> 6 25 2020-01-01 444 usa -0.0228 monthly vw_cap
#> 7 15 2020-01-01 217 usa -0.0631 monthly vw_cap
#> 8 50 2020-01-01 135 usa -0.0178 monthly vw_cap
#> 9 20 2020-01-01 458 usa -0.00683 monthly vw_cap
#> 10 55 2020-01-01 65 usa 0.0443 monthly vw_cap
#> # ℹ 122 more rows
Use list_supported_jkp_factors() to see the available regions, or
list_supported_jkp_factors("usa") to see the factors available for a
region.
To download the liquidity factors of Pastor and Stambaugh (2003) from Robert Stambaugh’s data library:
download_data(
domain = "Pastor-Stambaugh",
start_date = "2020-01-01",
end_date = "2020-12-31"
)
#> # A tibble: 12 × 4
#> date agg_liq innov_liq traded_liq
#> <date> <dbl> <dbl> <dbl>
#> 1 2020-01-01 -0.0408 -0.0138 0.0191
#> 2 2020-02-01 0.00627 0.0236 0.0411
#> 3 2020-03-01 -0.260 -0.164 -0.125
#> 4 2020-04-01 0.0115 -0.0266 0.123
#> 5 2020-05-01 -0.0559 -0.0665 0.0691
#> 6 2020-06-01 -0.0151 0.0156 0.0661
#> 7 2020-07-01 0.0345 0.0742 0.000783
#> 8 2020-08-01 0.00611 -0.0116 0.0426
#> 9 2020-09-01 -0.0996 -0.123 -0.000901
#> 10 2020-10-01 -0.0431 0.0285 0.0392
#> 11 2020-11-01 0.0539 0.0689 0.0276
#> 12 2020-12-01 0.0249 0.0131 0.0545
To download the mispricing factors of Stambaugh and Yuan (2017) from
Robert Stambaugh’s data
library,
optionally selecting "monthly" (the default) or "daily" data. Note
that the source files currently end in December 2016:
download_data(
domain = "Stambaugh-Yuan",
dataset = "monthly",
start_date = "2015-01-01",
end_date = "2016-12-31"
)
#> # A tibble: 24 × 6
#> date mkt_excess smb mgmt perf risk_free
#> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2015-01-01 -0.0311 -0.0271 -0.0227 0.0506 0
#> 2 2015-02-01 0.0613 0.0245 -0.0124 -0.0300 0
#> 3 2015-03-01 -0.0112 0.0214 -0.0192 0.00596 0
#> 4 2015-04-01 0.0059 -0.0260 0.00818 -0.0418 0
#> 5 2015-05-01 0.0136 0.0141 -0.00498 0.0108 0
#> 6 2015-06-01 -0.0153 0.0301 -0.00718 0.00387 0
#> 7 2015-07-01 0.0154 -0.0340 0.00419 0.0706 0
#> 8 2015-08-01 -0.0604 -0.00684 0.00725 0.0133 0
#> 9 2015-09-01 -0.0308 -0.0228 0.0373 0.0790 0
#> 10 2015-10-01 0.0775 -0.0227 0.0240 -0.00612 0
#> # ℹ 14 more rows
To download the cost of capital (WACC) by industry from Aswath
Damodaran’s data
library,
optionally selecting a region (e.g., "us", the default, or
"europe") and archived years via year. Reading the Excel files
requires the readxl package:
download_data(
domain = "Damodaran",
dataset = "wacc",
region = "us",
year = 2023:2024
)
#> # A tibble: 192 × 14
#> region year industry number_of_firms beta cost_of_equity equity_to_capital
#> <chr> <int> <chr> <int> <dbl> <dbl> <dbl>
#> 1 us 2023 Advertis… 57 1.37 0.102 0.748
#> 2 us 2023 Aerospac… 70 1.08 0.0883 0.797
#> 3 us 2023 Air Tran… 25 1.27 0.0971 0.381
#> 4 us 2023 Apparel 38 1.19 0.0938 0.672
#> 5 us 2023 Auto & T… 34 1.52 0.109 0.763
#> 6 us 2023 Auto Par… 39 1.34 0.101 0.722
#> 7 us 2023 Bank (Mo… 15 1.06 0.0876 0.316
#> 8 us 2023 Banks (R… 625 0.461 0.0600 0.495
#> 9 us 2023 Beverage… 19 1.13 0.0909 0.803
#> 10 us 2023 Beverage… 29 0.764 0.0740 0.854
#> # ℹ 182 more rows
#> # ℹ 7 more variables: sd_stock <dbl>, cost_of_debt <dbl>, tax_rate <dbl>,
#> # after_tax_cost_of_debt <dbl>, debt_to_capital <dbl>, wacc <dbl>,
#> # wacc_local <dbl>
To download multiple series from the Federal Reserve Economic Data (FRED):
download_data(
domain = "FRED",
series = c("GDP", "CPIAUCNS"),
start_date = "2020-01-01",
end_date = "2020-12-31"
)
#> # A tibble: 16 × 3
#> date value series
#> <date> <dbl> <chr>
#> 1 2020-01-01 21751. GDP
#> 2 2020-04-01 19958. GDP
#> 3 2020-07-01 21704. GDP
#> 4 2020-10-01 22087. GDP
#> 5 2020-01-01 258. CPIAUCNS
#> 6 2020-02-01 259. CPIAUCNS
#> 7 2020-03-01 258. CPIAUCNS
#> 8 2020-04-01 256. CPIAUCNS
#> 9 2020-05-01 256. CPIAUCNS
#> 10 2020-06-01 258. CPIAUCNS
#> 11 2020-07-01 259. CPIAUCNS
#> 12 2020-08-01 260. CPIAUCNS
#> 13 2020-09-01 260. CPIAUCNS
#> 14 2020-10-01 260. CPIAUCNS
#> 15 2020-11-01 260. CPIAUCNS
#> 16 2020-12-01 260. CPIAUCNS
To download the FRED-MD / FRED-QD (McCracken-Ng) macroeconomic
databases, a curated, balanced panel of monthly or quarterly macro
series with McCracken-Ng stationarity transform codes. Set
transform = TRUE to apply the transforms, and vintage to a
"YYYY-MM" label or "all" to access historical (real-time) releases:
download_data(
domain = "FRED",
dataset = "FRED-MD",
transform = TRUE
)
#> # A tibble: 801 × 127
#> date RPI W875RX1 DPCERA3M086SBEA CMRMTSPLx RETAILx INDPRO
#> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1959-01-01 NA NA NA NA NA NA
#> 2 1959-02-01 0.00388 0.00362 0.0103 0.00734 0.00731 0.0194
#> 3 1959-03-01 0.00646 0.00732 0.00940 -0.00337 0.00832 0.0143
#> 4 1959-04-01 0.00651 0.00703 -0.00362 0.0199 0.000616 0.0211
#> 5 1959-05-01 0.00580 0.00662 0.0120 0.00684 0.00780 0.0150
#> 6 1959-06-01 0.00307 0.00301 0.00364 -0.0000968 0.00906 0.00114
#> 7 1959-07-01 -0.000580 -0.000762 -0.00339 0.0122 -0.000330 -0.0242
#> 8 1959-08-01 -0.00565 -0.00575 0.00600 -0.0525 0.00636 -0.0345
#> 9 1959-09-01 0.000763 0 0.0100 0.0147 -0.0132 -0.00121
#> 10 1959-10-01 0.00127 0.00117 -0.00683 0.00162 0.00729 -0.00729
#> # ℹ 791 more rows
#> # ℹ 120 more variables: IPFPNSS <dbl>, IPFINAL <dbl>, IPCONGD <dbl>,
#> # IPDCONGD <dbl>, IPNCONGD <dbl>, IPBUSEQ <dbl>, IPMAT <dbl>, IPDMAT <dbl>,
#> # IPNMAT <dbl>, IPMANSICS <dbl>, IPB51222S <dbl>, IPFUELS <dbl>,
#> # CUMFNS <dbl>, HWI <dbl>, HWIURATIO <dbl>, CLF16OV <dbl>, CE16OV <dbl>,
#> # UNRATE <dbl>, UEMPMEAN <dbl>, UEMPLT5 <dbl>, UEMP5TO14 <dbl>,
#> # UEMP15OV <dbl>, UEMP15T26 <dbl>, UEMP27OV <dbl>, CLAIMSx <dbl>, …
To download stock prices from Yahoo Finance:
download_data(
domain = "Stock Prices",
symbols = c("AAPL", "MSFT"),
start_date = "2020-01-01",
end_date = "2020-12-31"
)
#> # A tibble: 506 × 8
#> symbol date volume open low high close adjusted_close
#> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 AAPL 2020-01-02 135480400 74.1 73.8 75.2 75.1 72.3
#> 2 AAPL 2020-01-03 146322800 74.3 74.1 75.1 74.4 71.6
#> 3 AAPL 2020-01-06 118387200 73.4 73.2 75.0 74.9 72.1
#> 4 AAPL 2020-01-07 108872000 75.0 74.4 75.2 74.6 71.8
#> 5 AAPL 2020-01-08 132079200 74.3 74.3 76.1 75.8 73.0
#> 6 AAPL 2020-01-09 170108400 76.8 76.6 77.6 77.4 74.5
#> 7 AAPL 2020-01-10 140644800 77.7 77.1 78.2 77.6 74.7
#> 8 AAPL 2020-01-13 121532000 77.9 77.8 79.3 79.2 76.3
#> 9 AAPL 2020-01-14 161954400 79.2 78.0 79.4 78.2 75.2
#> 10 AAPL 2020-01-15 121923600 78.0 77.4 78.9 77.8 74.9
#> # ℹ 496 more rows
You can also download high-frequency data for the S&P 500 that we host on Hugging Face:
download_data(
domain = "Tidy Finance",
dataset = "high_frequency_sp500",
start_date = "2007-07-26",
end_date = "2007-07-27"
)
#> # A tibble: 9,360 × 9
#> ts midquote signed_volume trading_volume depth0_ask
#> <dttm> <dbl> <dbl> <dbl> <dbl>
#> 1 2007-07-26 09:30:05 150. 0 11565577. 13841.
#> 2 2007-07-26 09:30:10 150. 0 3017263. 10795.
#> 3 2007-07-26 09:30:15 150. 0 6108290. 12371.
#> 4 2007-07-26 09:30:20 150. 98630 21820258. 14069.
#> 5 2007-07-26 09:30:25 150. 0 931698 15283.
#> 6 2007-07-26 09:30:30 150. -4400 661077 5307.
#> 7 2007-07-26 09:30:35 150. -12000 2102820 3192.
#> 8 2007-07-26 09:30:40 150. 39927 12384621. 6234.
#> 9 2007-07-26 09:30:45 150. -14671 3105370. 9255.
#> 10 2007-07-26 09:30:50 150. 5347 5518370. 5708.
#> # ℹ 9,350 more rows
#> # ℹ 4 more variables: depth0_bid <dbl>, depth5_ask <dbl>, depth5_bid <dbl>,
#> # spread <dbl>
You can also download data directly from WRDS if you have access to the
underlying data and set your credentials via
Sys.setenv(WRDS_USER = "your_username", WRDS_PASSWORD = "your_password")
or the set_wrds_credentials() helper function.
A WRDS account alone is not enough to connect: you also need to enroll
in two-factor authentication (Duo) and accept the WRDS terms of use on
the WRDS website. When you connect, approve the Duo push notification on
your device. See ?get_wrds_connection for the full setup checklist.
The WRDS downloads tell you when they connect and how long the download
took. Pass quiet = TRUE to suppress these messages.
To download monthly CRSP data:
download_data(
domain = "WRDS",
dataset = "crsp_monthly",
start_date = "2020-01-01",
end_date = "2020-12-31"
)
#> ℹ Connecting to WRDS. Approve the Duo request if prompted.
#> ✔ Connected to WRDS. [2.7s]
#>
#> ℹ Downloading CRSP monthly data from WRDS. This can take a few minutes.
#> ✔ Downloaded CRSP monthly data from WRDS. [2.8s]
#>
#> # A tibble: 43,478 × 15
#> permno date calculation_date ret shrout prc primaryexch siccd
#> <int> <date> <date> <dbl> <dbl> <dbl> <chr> <int>
#> 1 14313 2020-01-01 2020-01-31 0.00609 2.10e6 2.31 Q 3629
#> 2 14316 2020-01-01 2020-01-31 -0.158 6.54e7 16.2 Q 2834
#> 3 14317 2020-01-01 2020-01-31 0.129 2.54e7 79.7 Q 1542
#> 4 14318 2020-01-01 2020-01-31 -0.0380 6.84e7 31.9 Q 6351
#> 5 14325 2020-01-01 2020-01-31 0.276 5.94e7 76.0 Q 3841
#> 6 14328 2020-01-01 2020-01-31 -0.0994 5.21e7 6.52 N 7832
#> 7 14329 2020-01-01 2020-01-31 0.0171 2.49e8 44.1 N 5812
#> 8 14338 2020-01-01 2020-01-31 -0.0280 2.82e8 108. N 7011
#> 9 14339 2020-01-01 2020-01-31 0.111 2.05e7 6.5 Q 6799
#> 10 14352 2020-01-01 2020-01-31 0.0295 3.92e7 8.73 Q 2834
#> # ℹ 43,468 more rows
#> # ℹ 7 more variables: listing_age <int>, mktcap <dbl>, mktcap_lag <dbl>,
#> # prc_lag <dbl>, exchange <chr>, industry <chr>, ret_excess <dbl>
To download annual (or quaterly) Compustat data:
download_data(
domain = "WRDS",
dataset = "compustat_annual",
start_date = "2020-01-01",
end_date = "2020-12-31"
)
#> ℹ Connecting to WRDS. Approve the Duo request if prompted.
#> ✔ Connected to WRDS. [2.6s]
#>
#> ℹ Downloading Compustat annual data from WRDS. This can take a few minutes.
#> ✔ Downloaded Compustat annual data from WRDS. [1.2s]
#>
#> # A tibble: 11,993 × 25
#> gvkey date datadate seq ceq at lt txditc txdb
#> <chr> <date> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 001004 2020-05-01 2020-05-31 903. 9.03e2 2.08e+3 1.18e+3 0 0
#> 2 001019 2020-12-01 2020-12-31 13.5 1.35e1 4.06e+1 2.71e+1 3.61e-1 3.61e-1
#> 3 001045 2020-12-01 2020-12-31 -6867 -6.87e3 6.20e+4 6.89e+4 9 e+0 9 e+0
#> 4 001050 2020-12-01 2020-12-31 203. 2.03e2 4.19e+2 2.16e+2 6.97e+0 6.97e+0
#> 5 001062 2020-11-01 2020-11-30 464. 4.64e2 4.65e+2 8.04e-1 0 0
#> 6 001075 2020-12-01 2020-12-31 5634. 5.63e3 2.00e+4 1.43e+4 2.33e+3 2.14e+3
#> 7 001076 2020-12-01 2020-12-31 986. 9.86e2 1.32e+3 3.31e+2 1.27e+2 1.27e+2
#> 8 001078 2020-12-01 2020-12-31 32784 3.28e4 7.25e+4 3.95e+4 1.41e+3 1.41e+3
#> 9 001084 2020-12-01 2020-12-31 -2.67 -2.67e0 6.92e-1 3.36e+0 0 0
#> 10 001096 2020-12-01 2020-12-31 3372. 3.37e3 1.11e+4 6.69e+3 6.48e+2 6.48e+2
#> # ℹ 11,983 more rows
#> # ℹ 16 more variables: itcb <dbl>, pstkrv <dbl>, pstkl <dbl>, pstk <dbl>,
#> # capx <dbl>, oancf <dbl>, sale <dbl>, cogs <dbl>, xint <dbl>, xsga <dbl>,
#> # ib <dbl>, curcd <chr>, be <dbl>, op <dbl>, at_lag <dbl>, inv <dbl>
To download the CRSP-Compustat linking table:
download_data(
domain = "WRDS",
dataset = "ccm_links"
)
#> ℹ Connecting to WRDS. Approve the Duo request if prompted.
#> ✔ Connected to WRDS. [2.8s]
#>
#> ℹ Downloading CRSP-Compustat links from WRDS. This can take a few minutes.
#> ✔ Downloaded CRSP-Compustat links from WRDS. [716ms]
#>
#> # A tibble: 33,324 × 4
#> permno gvkey linkdt linkenddt
#> <dbl> <chr> <date> <date>
#> 1 25881 001000 1970-11-13 1978-06-30
#> 2 10015 001001 1983-09-20 1986-07-31
#> 3 10023 001002 1972-12-14 1973-06-05
#> 4 10031 001003 1983-12-07 1989-08-16
#> 5 54594 001004 1972-04-24 2026-10-18
#> 6 61903 001005 1973-01-31 1983-01-31
#> 7 10058 001007 1973-10-01 1979-01-30
#> 8 10058 001007 1979-01-31 1984-09-28
#> 9 10066 001008 1983-08-25 1987-02-26
#> 10 10074 001009 1982-01-18 1996-03-13
#> # ℹ 33,314 more rows
To download Enhanced TRACE data for selected bonds:
download_data(
domain = "WRDS",
dataset = "trace_enhanced",
cusips = c("00101JAH9"),
start_date = "2019-01-01",
end_date = "2021-12-31"
)
#> ℹ Connecting to WRDS. Approve the Duo request if prompted.
#> ✔ Connected to WRDS. [2.7s]
#>
#> ℹ Downloading enhanced TRACE data from WRDS. This can take a few minutes.
#> ✔ Downloaded enhanced TRACE data from WRDS. [753ms]
#>
#> # A tibble: 7,694 × 8
#> cusip_id trd_exctn_dt trd_exctn_tm rptd_pr entrd_vol_qt yld_pt rpt_side_cd
#> <chr> <date> <time> <dbl> <dbl> <dbl> <chr>
#> 1 00101JAH9 2019-01-02 09:22:14 92.3 5000 6.12 S
#> 2 00101JAH9 2019-01-02 09:22:14 92.3 5000 6.12 S
#> 3 00101JAH9 2019-01-02 12:14:09 91 3314000 6.49 B
#> 4 00101JAH9 2019-01-02 14:01:00 91.2 5000 6.43 S
#> 5 00101JAH9 2019-01-02 14:01:00 91.3 24000 6.41 S
#> 6 00101JAH9 2019-01-02 14:01:00 91.3 5000 6.41 S
#> 7 00101JAH9 2019-01-02 14:01:03 91.2 24000 6.43 S
#> 8 00101JAH9 2019-01-02 15:50:07 91.8 315000 6.28 S
#> 9 00101JAH9 2019-01-03 09:17:16 91.5 50000 6.34 S
#> 10 00101JAH9 2019-01-03 09:17:16 91.5 50000 6.34 S
#> # ℹ 7,684 more rows
#> # ℹ 1 more variable: cntra_mp_id <chr>
You can download pre-computed portfolio returns from the Tidy Finance Factor Library hosted on Hugging Face:
download_data(
domain = "Tidy Finance",
dataset = "factor_library",
sorting_variable = "assetgrowth"
)
#> No `start_date` or `end_date` provided. Returning the full data set.
#> # A data frame: 779 × 19
#> id date ret sorting_variable min_size_quantile min_stock_price
#> <int> <date> <dbl> <chr> <dbl> <dbl>
#> 1 2.12e6 1960-02-01 -0.0272 assetgrowth 0.2 NA
#> 2 2.12e6 1960-03-01 -0.0590 assetgrowth 0.2 NA
#> 3 2.12e6 1960-04-01 -0.0156 assetgrowth 0.2 NA
#> 4 2.12e6 1960-05-01 -0.0751 assetgrowth 0.2 NA
#> 5 2.12e6 1960-06-01 0.00232 assetgrowth 0.2 NA
#> 6 2.12e6 1960-07-01 0.0306 assetgrowth 0.2 NA
#> 7 2.12e6 1960-08-01 -0.0193 assetgrowth 0.2 NA
#> 8 2.12e6 1960-09-01 0.0347 assetgrowth 0.2 NA
#> 9 2.12e6 1960-10-01 0.0294 assetgrowth 0.2 NA
#> 10 2.12e6 1960-11-01 -0.0370 assetgrowth 0.2 NA
#> # ℹ 769 more rows
#> # ℹ 13 more variables: min_listing_age <int>, exclude_financials <lgl>,
#> # exclude_utilities <lgl>, exclude_negative_book_equity <lgl>,
#> # exclude_negative_earnings <lgl>, sorting_variable_lag <chr>,
#> # rebalancing <chr>, n_portfolios_main <dbl>, sorting_method <chr>,
#> # breakpoints_min_size_threshold <dbl>, n_portfolios_secondary <dbl>,
#> # breakpoints_exchanges <chr>, weighting_scheme <chr>
The package also provides functions to construct your own factor portfolios:
# Portfolio sorts and long-short returns
compute_portfolio_returns()
compute_long_short_returns()
# Configuration helpers
breakpoint_options()
data_options()
# Rolling and lagging
add_lagged_columns()
join_lagged_values()
compute_rolling_value()
# Regression estimation
estimate_model()
estimate_betas()
estimate_fama_macbeth()
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()