This is the assignments by using R-studio.

profileShelby1
ch20_R1.R

## ----setup, include=FALSE----------------------------------------------- knitr::opts_chunk$set(echo = TRUE) knitr::opts_chunk$set(fig.width=8, fig.height=4) ## ----------------------------------------------------------------------- HD <- read.table('Home_Depot_2012_GE19.txt', sep = '\t', header = TRUE) ## ----------------------------------------------------------------------- str(HD) ## ----------------------------------------------------------------------- plot(HD$year, HD$Sales, xlab = 'Year', ylab = 'Sales', main = 'Home Depot Quarterly Sales 1995 - 2012') lines(HD$year, HD$Sales) ## ----------------------------------------------------------------------- library(TTR) sma1 <- SMA(HD$Sales, n = 2) sma2 <- SMA(HD$Sales, n = 4) ## ----------------------------------------------------------------------- par(mfrow = c(1, 2)) plot(HD$year, HD$Sales, xlab = 'Year', ylab = 'Sales', main = 'SMA of Length 2') lines(HD$year, HD$Sales) lines(HD$year, sma1, col = 'red') plot(HD$year, HD$Sales, xlab = 'Year', ylab = 'Sales', main = 'SMA of Length 4') lines(HD$year, HD$Sales) lines(HD$year, sma2, col = 'red') ## ----------------------------------------------------------------------- ema1 <- EMA(HD$Sales, ratio = 0.5, n = 1) ema2 <- EMA(HD$Sales, ratio = 0.2, n = 1) par(mfrow = c(1, 2)) plot(HD$year, HD$Sales, xlab = 'Year', ylab = 'Sales', main = 'EMA (alpha = 0.5)') lines(HD$year, HD$Sales) lines(HD$year, ema1, col = 'red') plot(HD$year, HD$Sales, xlab = 'Year', ylab = 'Sales', main = 'EMA (alpha = 0.2)') lines(HD$year, HD$Sales) lines(HD$year, ema2, col = 'red') ## ----------------------------------------------------------------------- acf(HD$Sales, lag.max = 4, plot = FALSE) ## ----------------------------------------------------------------------- ar1 <- ar(HD$Sales, aic = FALSE, order.max = 4, demean = FALSE, intercept = TRUE, method = 'ols') ar1 ## ----------------------------------------------------------------------- fitted.ar1 <- HD$Sales - ar1$resid ## ----------------------------------------------------------------------- par(mfrow = c(1, 2)) plot(HD$year, HD$Sales, xlab = 'Year', ylab = 'Sales', main = 'AR(4)') lines(HD$year, HD$Sales) lines(HD$year, fitted.ar1, col = 'red') plot(HD$year, ar1$resid, xlab = 'Year', ylab = 'Residuals') abline(0, 0) ## ----------------------------------------------------------------------- HD.new <- HD[HD$year<2007,] imod <- lm(Sales ~ year + Q1 + Q2 + Q3, data = HD.new) summary(imod) ## ----------------------------------------------------------------------- (yhat.sma1 <- sma1[length(sma1)]) (yhat.sma2 <- sma2[length(sma2)]) (yhat.ema1 <- ema1[length(ema1)]) (yhat.ema2 <- ema2[length(ema2)]) ## ----------------------------------------------------------------------- (yhat.ar1 <- predict(ar1, n.ahead = 1, se.fit = FALSE)) ## ----------------------------------------------------------------------- data.new <- data.frame(year = 2013, Q1 = 1, Q2 = 0, Q3 = 0) yhat <- predict(imod, newdata = data.new) ## ----------------------------------------------------------------------- y.true <- 19.124 abs(y.true - yhat.sma1)/abs(y.true)*100 abs(y.true - yhat.sma2)/abs(y.true)*100 abs(y.true - yhat.ema1)/abs(y.true)*100 abs(y.true - yhat.ema2)/abs(y.true)*100 abs(y.true - yhat.ar1)/abs(y.true)*100 abs(y.true - yhat)/abs(y.true)*100