# Load packages

# Core
library(tidyverse)
library(tidyquant)

Functions

When should you write a function?

# For reproducible work
set.seed (1234)

# Create a data frame
df <- tibble:: tibble(
    a = rnorm(10),
    b = rnorm(10),
    c = rnorm(10),
    d=rnorm(10)
)
# Rescale each column
df$a <- (df$a - min(df$a, na.rm = TRUE)) / 
    (max(df$a, na.rm = TRUE) - min(df$a, na.rm = TRUE))
df$b <- (df$b - min(df$b, na.rm = TRUE)) / 
    (max(df$b, na.rm = TRUE) - min(df$b, na.rm = TRUE))
df$c <- (df$c - min(df$c, na.rm = TRUE)) / 
    (max(df$c, na.rm = TRUE) - min(df$c, na.rm = TRUE))
df$d <- (df$d - min(df$d, na.rm = TRUE)) / 
    (max(df$d, na.rm = TRUE) - min(df$d, na.rm = TRUE))
df
## # A tibble: 10 × 4
##        a      b     c     d
##    <dbl>  <dbl> <dbl> <dbl>
##  1 0.332 0.153  0.782 1    
##  2 0.765 0      0.473 0.519
##  3 1     0.0651 0.498 0.448
##  4 0     0.311  0.943 0.511
##  5 0.809 0.573  0.373 0.168
##  6 0.831 0.260  0     0.308
##  7 0.516 0.143  1     0    
##  8 0.524 0.0255 0.210 0.256
##  9 0.519 0.0472 0.708 0.575
## 10 0.424 1      0.253 0.522
rescale <- function(x) {
    
    #   body
   x <- (x - min(x, na.rm = TRUE)) / 
    (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
    
    # return value
    return(x)
}
df$a <- rescale (df$a)
df$b <-rescale (df$b)
df$c <- rescale (df$c)
df$d <- rescale (df$d)

Functions are for humans and computers

Conditional execution

detect_sign <- function (x) {
    if (x > 0) {
        message ("value is positive")
        print(x)
    } else if (x == 0) {
        warning( "Value is not positive, but it can be accepted")
        print(x)
    } else{
        stop( "Value is negative, but the function must stop")
              print(x)
    }
}

3 %>% detect_sign()
## [1] 3
0 %>% detect_sign()
## [1] 0
 # -1 %>% detect_sign()

Function arguments

?mean

x<- c(1:10, 100, NA)
x
##  [1]   1   2   3   4   5   6   7   8   9  10 100  NA
x %>% mean()
## [1] NA
x %>% mean (na.rm = TRUE)
## [1] 14.09091
x %>% mean(na.rm = TRUE, trim = 0.1)
## [1] 6
mean_remove_na <- function(x, na.rm = TRUE, ...) {
    avg <- mean(x, na.rm = na.rm, ...)
    return(avg)
}
x %>% mean_remove_na()
## [1] 14.09091
x %>% mean_remove_na(na.rm = FALSE)
## [1] NA
x %>% mean_remove_na(trim = 0.1)
## [1] 6

Return values

1 Import stock prices

symbols <- c("SPY", "EFA", "IJS", "EEM","AGG") 
prices <- tq_get(x    = symbols, 
                 get = "stock.prices",
                 from = "2012-12-31",
                 to   = "2017-12-31")

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted,
                 mutate_fun = periodReturn, 
                 period     = "monthly",
                 type       = "log") %>%
    slice(-1) %>%
    ungroup() %>%
set_names(c("asset", "date", "returns"))

3 Assign weight

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AGG" "EEM" "EFA" "IJS" "SPY"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)
weights
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weights)
w_tbl 
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AGG        0.25
## 2 EEM        0.25
## 3 EFA        0.2 
## 4 IJS        0.2 
## 5 SPY        0.1

4 Build a portfolio

# ?tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
    
    tq_portfolio(assets_col = asset,
                 returns_col = returns,
                 weights = w_tbl, 
                 rebalance_on = "months")
portfolio_returns_tbl
## # A tibble: 60 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-01-31           0.0204 
##  2 2013-02-28          -0.00239
##  3 2013-03-28           0.0121 
##  4 2013-04-30           0.0174 
##  5 2013-05-31          -0.0128 
##  6 2013-06-28          -0.0247 
##  7 2013-07-31           0.0321 
##  8 2013-08-30          -0.0224 
##  9 2013-09-30           0.0511 
## 10 2013-10-31           0.0301 
## # ℹ 50 more rows

5.1 Get market returns

market_returns_tbl <-tq_get(x = "SPY",
       get = "stock.prices",
       from = "2012-12-31",
       to = "2017-12-31") %>%
    # Convert prices to returns
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "monthly",
                 type = "log",
                 col_rename ="returns") %>%
    slice(-1)  

5.2 Join returns

portfolio_market_returns_tbl<-left_join(market_returns_tbl, portfolio_returns_tbl, by = "date") %>%
    set_names("date", "market_returns", "portfolio_returns") 

5.3 CAPM Beta

portfolio_market_returns_tbl %>%
    tq_performance(Ra = portfolio_returns,
                   Rb = market_returns,
                   performance_fun = CAPM.beta)
## # A tibble: 1 × 1
##   CAPM.beta.1
##         <dbl>
## 1       0.738

6 Plot

Scatterplot of returns with regression line

portfolio_market_returns_tbl %>%
    ggplot(aes(x=market_returns,
               y = portfolio_returns)) +
    geom_point(color = "cornflowerblue") +
    geom_smooth(method = "lm", se = FALSE, size = 1.5, color = tidyquant:: palette_light()[3]) +
    labs(y="Portfolio Returns",
         x = "Market Returns")

Line plot of fitted vs actual returns

actual_fitted_long_tbl <-portfolio_market_returns_tbl %>%
    #Linear Regression Model
    lm(portfolio_returns ~ market_returns, data = .) %>%
    # Get
    broom::augment() %>%
    # Add date
    mutate(date = portfolio_market_returns_tbl$date) %>%
    select(date, portfolio_returns, .fitted) %>%
    #Transform data to long 
    pivot_longer(cols = c(portfolio_returns, .fitted),
                 names_to = "type", values_to = "returns")
actual_fitted_long_tbl %>%
    ggplot(aes(x=date, y =returns, color = type)) + 
    geom_line()