R Markdown

=============================================================================

Library Installation and Loading

=============================================================================

required_packages <- c(“xts”, “quantmod”, “corrplot”, “devtools”)

Install missing packages from CRAN

for (pkg in required_packages) { if (!require(pkg, character.only = TRUE)) { install.packages(pkg, dependencies = TRUE) library(pkg, character.only = TRUE) } }

Install and load covFactorModel from GitHub

if (!require(“covFactorModel”, character.only = TRUE)) { devtools::install_github(“dppalomar/covFactorModel”) library(covFactorModel) }

=============================================================================

Factor Models with R - Complete Implementation

Based on Daniel P. Palomar’s tutorial

https://palomar.home.ece.ust.hk/MAFS6010R_lectures/Rsession_factor_models.html

=============================# =============================================================================

LIBRARY INSTALLATION (ensure packages are installed)

=============================================================================

required_packages <- c(“xts”, “quantmod”, “corrplot”, “devtools”)

for (pkg in required_packages) { if (!requireNamespace(pkg, quietly = TRUE)) { install.packages(pkg) } }

covFactorModel is only available on GitHub

if (!requireNamespace(“covFactorModel”, quietly = TRUE)) { devtools::install_github(“dppalomar/covFactorModel”) }

Load libraries

library(xts) library(quantmod) library(corrplot) library(covFactorModel)

=============================================================================

SINGLE FACTOR MODEL WITH covFactorModel (EM method)

=============================================================================

begin_date <- “2016-01-01” end_date <- “2017-12-31” stock_namelist <- c(“AAPL”, “AMD”, “ADI”, “ABBV”, “AEZS”, “A”, “APD”, “AA”, “CF”) sector_namelist <- c(rep(“Information Technology”, 3), rep(“Health Care”, 3), rep(“Materials”, 3))

Download stock data

data_set <- do.call(merge, lapply(stock_namelist, function(sym) Ad(getSymbols(sym, from = begin_date, to = end_date, auto.assign = FALSE)))) colnames(data_set) <- stock_namelist indexClass(data_set) <- “Date”

Market factor

SP500_index <- Ad(getSymbols(“^GSPC”, from = begin_date, to = end_date, auto.assign = FALSE)) f <- diff(log(SP500_index), na.pad = FALSE)

Log returns

X <- diff(log(data_set), na.pad = FALSE)

Fit market model using EM method

factor_model_em <- fitFactorModelEM(X = X, type = “M”, econ_fact = f)

View model estimates

cat(“model (EM) estimates:”) print(factor_model_em)

=============================================================================

FAMA-FRENCH 3-FACTOR MODEL USING EM METHOD

=============================================================================

begin_date_ff <- “2013-01-01” end_date_ff <- “2017-08-31” data_set_ff <- do.call(merge, lapply(stock_namelist, function(sym) Ad(getSymbols(sym, from = begin_date_ff, to = end_date_ff, auto.assign = FALSE)))) colnames(data_set_ff) <- stock_namelist indexClass(data_set_ff) <- “Date”

X_ff <- diff(log(data_set_ff), na.pad = FALSE) T_ff <- nrow(X_ff)

Simulated Fama-French factors

set.seed(123) fama_factors <- data.frame( Mkt.RF = rnorm(T_ff, mean = 0.0004, sd = 0.01), SMB = rnorm(T_ff, mean = 0, sd = 0.005), HML = rnorm(T_ff, mean = 0, sd = 0.005) ) F_ff <- xts(fama_factors, index(X_ff))

Fit 3-factor model using EM

factor_model_ff_em <- fitFactorModelEM(X = X_ff, type = “M”, econ_fact = F_ff)

cat(“-French EM model estimates:”) print(factor_model_ff_em)

=============================================================================

ETF PERFORMANCE ANALYSIS USING EM METHOD

=============================================================================

begin_date_etf <- “2016-10-01” end_date_etf <- “2017-06-30” etf_namelist <- c(“SPY”, “XIVH”, “SPHB”, “SPLV”, “USMV”, “JKD”)

data_set_etf <- do.call(merge, lapply(etf_namelist, function(sym) Ad(getSymbols(sym, from = begin_date_etf, to = end_date_etf, auto.assign = FALSE)))) colnames(data_set_etf) <- etf_namelist indexClass(data_set_etf) <- “Date”

SP500_index_etf <- Ad(getSymbols(“^GSPC”, from = begin_date_etf, to = end_date_etf, auto.assign = FALSE)) f_etf <- diff(log(SP500_index_etf), na.pad = FALSE)

X_etf <- diff(log(data_set_etf), na.pad = FALSE)

factor_model_etf_em <- fitFactorModelEM(X = X_etf, type = “M”, econ_fact = f_etf)

cat(“factor model estimates using EM:”) print(factor_model_etf_em)