Beta is a measure of systematic risk. I have prior tutorials better explaining Beta, so this post will primarily be covering how we can examine Beta with R and the PerformanceAnalytics package.

**Step One: Obtain the Data Via quantmod**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
library(quantmod) library(Quandl) library(PerformanceAnalytics) library(dplyr) #-----------Benchmark-----------# ticker = '^GSPC' #Get Data stock <- getSymbols.yahoo(ticker, from="2016-01-01", periodicity = "daily", auto.assign=FALSE)[,4] #Remove Dates With No Prices stock <- stock[apply(stock,1,function(x) all(!is.na(x))),] #Calculate Returns for Assets: Daily RoC bench_ret <- na.omit(ROC(stock, type="discrete")) #Rename Columns colnames(bench_ret) <- "SP500" #-----------Asset-----------# asset = 'MMM' #Get Data stock <- getSymbols.yahoo(asset, from="2016-01-01", periodicity = "daily", auto.assign=FALSE)[,4] #Remove Dates With No Prices stock <- stock[apply(stock,1,function(x) all(!is.na(x))),] #Rename Columns colnames(stock) <- asset #Calculate Returns for Assets: Daily RoC asset_ret <- na.omit(ROC(stock, type="discrete")) #-----------Portfolio-----------# #Create Vector of Tickers tickers <- c("AAPL", "INTC", "ADSK", "NVDA", "ADBE","ATVI", "BIDU", "CELG", "CMCSA", "QCOM") #Get Prices (can be monthly or weekly) portfolioPrices <- NULL for (Ticker in tickers) portfolioPrices <- cbind(portfolioPrices, getSymbols.yahoo(Ticker, from="2016-01-01", periodicity = "daily", auto.assign=FALSE)[,4]) #Delete all dates with no prices portfolioPrices <- portfolioPrices[apply(portfolioPrices,1,function(x) all(!is.na(x))),] #Rename Columns colnames(portfolioPrices) <- tickers #Calculate Returns for Assets: Daily RoC portfolioReturns <- na.omit(ROC(portfolioPrices, type="discrete")) #Calculate Aggregate Returns for Portfolio weights <-c(".1",".1", ".1", ".1", ".1", ".1", ".1", ".1", ".1", ".1") port_rets <- Return.portfolio(portfolioReturns, weights = NULL, geometric = TRUE) names(port_rets) <- "Portfolio" #-----------Risk Free Data-----------# rf_data = Quandl("USTREASURY/YIELD",start_date="2016-11-13") rf <- data.frame(rf_data[4]) #calculate mean rf <- colMeans(rf[sapply(rf, is.numeric)]) |

**Step Two: Calculate Beta**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#----------Beta-------------# CAPM.beta(port_rets, bench_ret, Rf = rf) CAPM.beta(asset_ret, bench_ret, Rf = rf) #----------Beta in Up Market-------------# #Get > 0 returns for benchmark pos_rets <- bench_ret[bench_ret$SP500 > 0, ] #Get Date Values dates <- index(pos_rets) #Filter By Date List asset_pos <- asset_ret[index(asset_ret) %in% as.Date(dates),] beta_pos <- CAPM.beta(asset_pos, pos_rets, Rf = rf) #----------Beta in Down Market-------------# neg_rets <- bench_ret[bench_ret$SP500 < 0, ] neg_dates <- index(neg_rets) asset_neg <- asset_ret[index(asset_ret) %in% as.Date(neg_dates),] beta_neg <- CAPM.beta(asset_neg, neg_rets, Rf = rf) #----------Timing Ratio-------------# #Timing Raio (>1 in rising market, <1 in falling market) time_ratio = beta_pos / beta_neg |

*Elaborating on Beta in up and down markets as well as the timing ratio….*

This my own personal extension of the beta.bull, beta.bear, and timing ratio functions within the PerformanceAnalytics package which I had issues with. I’m literally just calculating beta when the market returns were positive and negative. The timing ratio is applicable to the portfolio as it is a measure of properly allocating during up and down markets. During a down market, the value should be less than one as you would theoretically incur fewer losses. During a bull market, the value should be greater than one as you would capture more gains due to the timing of your allocations.

**Step Three: Output Regression Chart**

1 2 3 4 |
#----------Regression Chart-------------# chart.Regression(asset_ret, bench_ret, Rf = rf, excess.returns = FALSE, fit = c("loess", "linear"), legend.loc = "topleft", element.color = "blue", main = paste(asset, "to", "S&P 500")) |

I get an error in the beta up market

<>

“”Error in xts(rep(NA, length(index(x))), index(x)) : order.by requires an appropriate time-based object””