R programming

bhihu45
Codefor.docx

Code for #2:

data_path <- "C:/Users/ Documents/m-ba3dx6113.txt"

install.packages("fpp")

install.packages("fBasics")

install.packages("quantmod")

install.packages("expsmooth")

install.packages("gridExtra")

install.packages("ggthemes")

install.packages("ggfortify")

install.packages("fGarch")

install.packages("fUnitRoots")

##packages

library(fBasics)

library(quantmod)

library(expsmooth)

library(fpp)

library(ggplot2)

library(knitr)

library(corrplot)

library(gridExtra)

library(ggthemes)

library(ggfortify)

library(fGarch)

library(fUnitRoots)

setwd("C:/Users/Documents")

source("Igarch.R")

source("garchM.R")

source("Tgarch11.R")

# Read data

da <- read.table("m-ba3dx6113.txt", header = T)

# Assign values

ba <- da$ba

# Transform to log returns

ba.log <- log(ba + 1)

# Explore summary stats

basicStats(ba.log)

# Convert to time series

ts.ba.log <- ts(ba.log, start = c(1961, 1), frequency = 12)

# Plot the time series

plot(ts.ba.log, xlab = "Year", ylab = "Returns",

main = "Monthly Returns of Boeing

Natual Log - Continuously Compounded")

# ACF & PACF

par(mfcol = c(2, 1))

acf(ba.log)

pacf(ba.log)

par(mfcol = c(1, 1))

# ACF & PACF - first differenced

par(mfcol = c(2, 1))

acf(diff(ba.log))

pacf(diff(ba.log))

par(mfcol = c(1, 1))

##########################################################################################################

#Part A)

#Serial correlation

# Is the expected log return zero?

t.test(ba.log)

# Are there serial correlations in the log return?

# ACF plot of series for checking serial correlations in series

# ACF plot of abs(series) for checking dependence in series

par(mfcol = c(2, 1))

acf(ba.log, 25, xlim = c(1, 25), ylim = c(-0.2, 0.4))

acf(abs(ba.log), 25, xlim = c(1, 25), ylim = c(-0.2, 0.4))

par(mfcol = c(1, 1))

# Ljung-Box test

Box.test(ba.log, lag = 12, type = "Ljung")

#--------------------------------------

# Testing for ARCH effects

#--------------------------------------

# Specify mean equation

# Since mean is significantly different from zero, subtract it

ba.log.res <- (ba.log - mean(ba.log))

#------------------

# Ljung-Box test

#------------------

# Use squared series

# H0: first m lags of ACF of squared series = 0

# If we reject H0:, series shows strong ARCH effects

# Test at lag 12 - reject H0:

Box.test(ba.log.res^2, lag = 12, type = "Ljung")

#------------------

# Examining ACF & PACF

#------------------

# Use squared series

# If autocorrelations > critical value lines, then:

# 1. Conclude serial correlations

# 2. Conclude ARCH effects

# ACF & PACF of squared residuals - first lag removed

par(mfcol = c(2, 1))

acf(ba.log.res^2, 25, xlim = c(1, 25), ylim = c(-0.2, 0.2))

pacf(ba.log.res^2, 25, ylim = c(-0.2, 0.2))

par(mfcol = c(1, 1))

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

# Q3B

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

# Build a GARCH model with Gaussian innovations for the log return series

# Perform model checking and write down the fitted model

ba.log.m1 <- garchFit(~garch(1, 1), data = ba.log, trace = F)

#--------------------------------------

# Model adequacy

#--------------------------------------

# Summary stats

# Includes Ljung-Box results for:

# Standardized residuals - adequacy of model mean equation

# Standardized residuals squared - adequacy of model variance equation

summary(ba.log.m1)

# Examine residuals for normality assumption

# Assign standardized residuals

ba.log.m1.res <- residuals(ba.log.m1, standardize = T)

# Q-Q Plot

qqnorm(ba.log.m1.res); qqline(ba.log.m1.res)

# Shapiro test of normality - H0: iid normal

shapiro.test(ba.log.m1.res)

# ACF & PACF

# Standardized residuals - adequacy of model mean equation

# Standardized residuals squared - adequacy of model variance equation

# First lag removed

par(mfcol = c(2, 2))

acf(ba.log.m1.res, 25, xlim = c(1, 25), ylim = c(-0.1, 0.1))

pacf(ba.log.m1.res, 25, ylim = c(-0.1, 0.1))

acf(ba.log.m1.res^2, 25, xlim = c(1, 25), ylim = c(-0.1, 0.1))

pacf(ba.log.m1.res^2, 25, ylim = c(-0.1, 0.1))

par(mfcol = c(1, 1))

# Note: residuals do not appear to satisfy normality assumption

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

# Q3C & Q3D

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

# Fit a GARCH model with skew-Student-t innovations to the monthly log returns

# Perform model checking and write down the fitted model

ba.log.m2 <- garchFit(~garch(1, 1), data = ba.log, trace = F,

cond.dist = "sstd")

#--------------------------------------

# Model adequacy

#--------------------------------------

# Summary stats

# Includes Ljung-Box results for:

# Standardized residuals - adequacy of model mean equation

# Standardized residuals squared - adequacy of model variance equation

summary(ba.log.m2)

# Examine residuals for normality assumption

# Assign standardized residuals

ba.log.m2.res <- residuals(ba.log.m2, standardize = T)

# Q-Q Plot

qqnorm(ba.log.m2.res);

qqline(ba.log.m2.res)

# Shapiro test of normality - H0: iid normal

shapiro.test(ba.log.m2.res)

# ACF & PACF

# Standardized residuals - adequacy of model mean equation

# Standardized residuals squared - adequacy of model variance equation

# First lag removed

par(mfcol = c(2, 2))

acf(ba.log.m2.res, 25, xlim = c(1, 25), ylim = c(-0.1, 0.1))

pacf(ba.log.m2.res, 25, ylim = c(-0.1, 0.1))

acf(ba.log.m2.res^2, 25, xlim = c(1, 25), ylim = c(-0.1, 0.1))

pacf(ba.log.m2.res^2, 25, ylim = c(-0.1, 0.1))

par(mfcol = c(1, 1))

# Note: residuals do not appear to satisfy normality assumption

# Based on the fitted model, is the monthly log returns of Boeing skewed?

ba.log.m2.tratio <- ((0.88820 - 1) / (0.05998));

ba.log.m2.tratio

ba.log.m2.pv <- 2*pnorm(ba.log.m2.tratio);

ba.log.m2.pv

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

# Q3E

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

# Fit a GARCH-M model to the monthly log returns

# Write down the fitted model

# Is the risk premium statistically significant?

ba.log.m3 <- garchM(ba.log)

ba.log.m3.pv <- 2*pnorm(0.65823, lower.tail = F);

ba.log.m3.pv

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

# Q3F

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

# Fit a TGARCH(1,1) model to the monthly log returns

# Write down the fitted model

# Is the leverage effect statistically significant?

ba.log.m4 <- Tgarch11(ba.log)

# Use 1-sided t-test based on H0:

# H0: gamma <= 0

# Ha: gamma > 0

ba.log.m4.tratio <- 2.54059

ba.log.m4.pv <- pnorm(ba.log.m4.tratio, lower.tail = F);

ba.log.m4.pv