0% found this document useful (0 votes)
19 views2 pages

R Codes - QMF

The document contains R code for financial analysis of Apple Inc. stock using various models including CAPM, Fama-French 3-factor model, GARCH, and ARIMA. It downloads historical stock price data, calculates returns, and fits models to analyze the stock's performance and forecast future prices. The code also includes steps for visualizing the results of the forecasts.

Uploaded by

SI P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

R Codes - QMF

The document contains R code for financial analysis of Apple Inc. stock using various models including CAPM, Fama-French 3-factor model, GARCH, and ARIMA. It downloads historical stock price data, calculates returns, and fits models to analyze the stock's performance and forecast future prices. The code also includes steps for visualizing the results of the forecasts.

Uploaded by

SI P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

R codes:

# Install libraries if not already installed


install.packages(c("quantmod", "PerformanceAnalytics", "tseries", "fBasics",
"rugarch", "car", "forecast", "fama", "dplyr"))

# Load libraries
library(quantmod)
library(PerformanceAnalytics)
library(tseries)
library(fBasics)
library(rugarch)
library(forecast)
library(dplyr)
library(car)
# Download Apple stock price data from Yahoo Finance
getSymbols("AAPL", src = "yahoo", from = "2010-01-01", to = "2023-01-01")
stock_data <- Cl(AAPL) # Get adjusted closing prices

# Convert to returns
returns <- dailyReturn(stock_data)
# Download S&P 500 data as a proxy for the market
getSymbols("^GSPC", src = "yahoo", from = "2010-01-01", to = "2023-01-01")
market_data <- dailyReturn(Cl(GSPC))

# CAPM regression: regressing Apple's returns on market returns


capm_model <- lm(returns ~ market_data)
summary(capm_model)
# Load Fama-French factors (downloaded from Kenneth French's website)
fama_french_factors <- read.csv("F-F_Research_Data_Factors.csv")
factors <- fama_french_factors[ ,c("Mkt_RF", "SMB", "HML")]
# Regress Apple's returns on Fama-French 3 factors
ff_model <- lm(returns ~ factors$Mkt_RF + factors$SMB + factors$HML)
summary(ff_model)
# Regress Apple's returns on the three Fama-French factors
ols_model <- lm(returns ~ factors$Mkt_RF + factors$SMB + factors$HML)
summary(ols_model)
# Specify GARCH(1,1) model
garch_spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder =
c(1, 1)),
mean.model = list(armaOrder = c(1, 1), include.mean = TRUE))

# Fit GARCH model to Apple's return data


garch_fit <- ugarchfit(spec = garch_spec, data = returns)
summary(garch_fit)

# Forecast volatility
garch_forecast <- ugarchforecast(garch_fit, n.ahead = 10)
plot(garch_forecast)
# Fit ARIMA model to Apple's stock prices
arima_model <- auto.arima(stock_data)
summary(arima_model)

# Forecast the next 10 days of stock prices


forecast_arima <- forecast(arima_model, h = 10)
plot(forecast_arima)

You might also like