title: “Reproducible Finance with R” subtitle: “Ch1: Introduction” author: “Ethan D’Onofrio” date: “2026-09-29” output: html_document: toc: yes pdf_document: default word_document: default editor_options: chunk_output_type: console —

Major Takeaways from Chapter 1

Chapter 1 introduces reproducible finance and explains how R makes financial analysis easier to repeat and understand. It covers three approaches: xts, tidyverse, and tidyquant. The chapter also emphasizes data visualization and Shiny applications, which help turn complicated financial information into useful graphs and interactive tools for analyzing investment portfolios.

# Load packages
library(tidyverse)
library(tidyquant)

Stock index

tq_index_options()
## [1] "DOW"       "DOWGLOBAL" "SP400"     "SP500"     "SP600"
data <- tq_index("SP400")

Stock exchanges

tq_exchange_options()
## [1] "AMEX"   "NASDAQ" "NYSE"
data2 <- tq_exchange("NYSE")

tq_get

stock prices from yahoo finance

stock <- tq_get("TSLA")

economic data from FRED

unemployment_uh <- tq_get("NHUR", get = "economic.data")
## Warning: x = 'NHUR', get = 'economic.data': Error in getSymbols.FRED(Symbols = "NHUR", env = <environment>, verbose = FALSE, : Unable to import "NHUR".
## Stream error in the HTTP/2 framing layer [fred.stlouisfed.org]:
## HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2)
## getSymbols.FRED: requests without an API key are not guaranteed to succeed.
## Register for a free key at https://fredaccount.stlouisfed.org/apikeys and set it with
## setDefaults(getSymbols.FRED, api.key = "your key").
unemployment_uh
## [1] NA
library(tidyverse) # core functions 
library(tidyquant) # for financial analysis

# Import data
AAPL <- tq_get("AAPL")

line Chart

AAPL %>%
  ggplot(aes(x = date, y = close)) +
  geom_line() +
  labs(title = "APPl Line Chart", y = "closing Price", x = "") +
  theme_tq()

Bar Chart

AAPL %>%
  ggplot(aes(x = date, y = close)) +
  geom_barchart(aes(open = open, high = high,
  low = low, close = close)) +
  labs(title = "AAPL Bar Chart", y = "Closing Price", x = "") +
 theme_tq()

Candlestick Chart

AAPL %>%
  tail(30) %>%

  ggplot(aes(x = date, y = close)) +

  geom_candlestick(aes(open = open, high = high,

                       low = low, close = close)) +

  labs(title = "AAPL Candlestick Chart",

       y = "Closing Price",

       x = "") +

  theme_tq()

Performance Analysis

Get Stock prices and convert to return

Ra <- c("AAPL", "GOOG", "NFLX") %>%
  tq_get(
    get = "stock.prices",
    from = "2010-01-01",
    to = "2015-12-31"
  ) %>%
  group_by(symbol) %>%
  tq_transmute(
    select = adjusted,
    mutate_fun = periodReturn,
    period = "monthly",
    col_rename = "Ra"
  )

Ra
## # A tibble: 216 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AAPL   2010-01-29 -0.103 
##  2 AAPL   2010-02-26  0.0654
##  3 AAPL   2010-03-31  0.148 
##  4 AAPL   2010-04-30  0.111 
##  5 AAPL   2010-05-28 -0.0161
##  6 AAPL   2010-06-30 -0.0208
##  7 AAPL   2010-07-30  0.0227
##  8 AAPL   2010-08-31 -0.0550
##  9 AAPL   2010-09-30  0.167 
## 10 AAPL   2010-10-29  0.0607
## # ℹ 206 more rows

Get baseline and convert to returns

Rb <- "XLK" %>%
  tq_get(
    get = "stock.prices",
    from = "2010-01-01",
    to = "2015-12-31"
  ) %>%
  tq_transmute(
    select = adjusted,
    mutate_fun = periodReturn,
    period = "monthly",
    col_rename = "Rb"
  )

Rb
## # A tibble: 72 × 2
##    date            Rb
##    <date>       <dbl>
##  1 2010-01-29 -0.0993
##  2 2010-02-26  0.0348
##  3 2010-03-31  0.0684
##  4 2010-04-30  0.0126
##  5 2010-05-28 -0.0748
##  6 2010-06-30 -0.0540
##  7 2010-07-30  0.0745
##  8 2010-08-31 -0.0561
##  9 2010-09-30  0.117 
## 10 2010-10-29  0.0578
## # ℹ 62 more rows

Join the two tables

RaRb <- left_join(
  Ra,
  Rb,
  by = c("date" = "date")
)

RaRb
## # A tibble: 216 × 4
## # Groups:   symbol [3]
##    symbol date            Ra      Rb
##    <chr>  <date>       <dbl>   <dbl>
##  1 AAPL   2010-01-29 -0.103  -0.0993
##  2 AAPL   2010-02-26  0.0654  0.0348
##  3 AAPL   2010-03-31  0.148   0.0684
##  4 AAPL   2010-04-30  0.111   0.0126
##  5 AAPL   2010-05-28 -0.0161 -0.0748
##  6 AAPL   2010-06-30 -0.0208 -0.0540
##  7 AAPL   2010-07-30  0.0227  0.0745
##  8 AAPL   2010-08-31 -0.0550 -0.0561
##  9 AAPL   2010-09-30  0.167   0.117 
## 10 AAPL   2010-10-29  0.0607  0.0578
## # ℹ 206 more rows

Calculate CAPM

RaRb_capm <- RaRb %>%
  tq_performance(
    Ra = Ra,
    Rb = Rb,
    performance_fun = table.CAPM
  )

RaRb_capm
## # A tibble: 3 × 18
## # Groups:   symbol [3]
##   symbol ActivePremium  Alpha AlphaRobust AnnualizedAlpha  Beta `Beta-`
##   <chr>          <dbl>  <dbl>       <dbl>           <dbl> <dbl>   <dbl>
## 1 AAPL           0.119 0.0089      0.0095           0.112 1.11    0.578
## 2 GOOG           0.034 0.0028     -0.0005           0.034 1.14    1.39 
## 3 NFLX           0.447 0.053       0.0439           0.859 0.384  -1.52 
## # ℹ 11 more variables: `Beta-Robust` <dbl>, `Beta+` <dbl>, `Beta+Robust` <dbl>,
## #   BetaRobust <dbl>, Correlation <dbl>, `Correlationp-value` <dbl>,
## #   InformationRatio <dbl>, `R-squared` <dbl>, `R-squaredRobust` <dbl>,
## #   TrackingError <dbl>, TreynorRatio <dbl>