Codes
Codes
tsales <- ts(sales, start = c(2003,1),frequency = 12 ) #numerical values have default plot of scatter
plot
plot(tsales)
plot(tsales, xlab= "time", ylab="monthly sales", main= "sales(2003-2004)")
start(tsales)
end(tsales)
frequency(tsales)
tsales.subset <- window(tsales, start = c(2003,5),end = c(2004,6))#subset the series
plot(tsales.subset)
library(forecast)
plot(Nile, main = "raw time series")
plot(ma(Nile, 3), main ="Simple Moving Average (k = 3)")
plot(ma(Nile, 7), main ="Simple Moving Average (k = 7)")
plot(ma(Nile, 15), main ="Simple Moving Average (k = 15)")
lim <- c(min(Nile), max(Nile))
par(mfrow = c(2,2))
plot(Nile, main = "raw time series")
plot(ma(Nile, 3), main ="Simple Moving Average (k = 3)", ylim = lim)
plot(ma(Nile, 7), main ="Simple Moving Average (k = 7)", ylim = lim)
plot(ma(Nile, 15), main ="Simple Moving Average (k = 15)", ylim = lim)
par(mfrow = c(1,1))
#seasonal decomposition
par(mfrow = c(2,1))
plot(AirPassengers)
lair <- log(AirPassengers) #to convert from multiplicative to additive
plot(lair,ylab = "logAirPassengers")
fit <- stl(lair, s.window = "period") #used for decomposition into seasonality, trend, irregularity or
error
plot(fit)
fit$time.series
exp(fit$time.series)#anti log
monthplot(AirPassengers, xlab="",ylab="")
seasonplot(AirPassengers, year.labels = "true", main = "")
#simple exponential
plot(nhtemp)
fit <- ets(nhtemp, model = "ANN")
fit
pred <- forecast(fit,1)
pred
plot(pred,xlab = "year", ylab = "temp", main = " ")
accuracy(fit)
#level 3 exponentail sm
fit1 <- ets(lair , model = "AAA")
fit1
accuracy(fit1)
pred1 <- forecast(fit1,5)
plot(pred1, xlab = "year", ylab = "temp", main = "")
fit2 <- ets(lair)
fit2
accuracy(fit2)
accuracy(fit1)
##########
par(mfrow = c(2,1))
plot(sales_ts)
lines(fit_lin_trend$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_lin_trend$residuals)
par(mfrow = c(1,1))
##########
par(mfrow = c(2,1))
plot(sales_ts)
lines(fit_exp_trend$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_exp_trend$residuals)
par(mfrow = c(1,1))
__________________________________________________________________________________
############# Quadratic Trend (Amtrak Data) #############
#setwd("D:/Shivani 2017/LBS classes/Data Science 1/Data
Science_2017/Forecasting/Regression_Forecasting")
#when data is polynomial we sqaure or cube the independent variable
ride <- read.csv("Amtrak data.csv", header = TRUE, stringsAsFactors = TRUE)
head(ride)
ride_ts <- ts(ride$Ridership, start = c(1991,1), end = c(2004, 3), freq = 12)
ride_ts
plot(ride_ts, xlab = "Time", ylab = "Ridership", ylim = c(1300, 2300))
fit_quad_trend <- tslm(ride_ts ~ trend + I(trend^2))
accuracy(fit_quad_trend)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values")
##########
par(mfrow = c(2,1))
plot(ride_ts)
lines(fit_quad_trend$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_quad_trend$residuals)
par(mfrow = c(1,1))
##################################
rm(list = ls())
##########################
##Model with seasonality ### seasonality was not included till, now we include seasonality
fit_season <- tslm(ride_ts ~ season)
accuracy(fit_season)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values")
######################################
par(mfrow = c(2,1))
plot(ride_ts)
lines(fit_season$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_season$residuals)
par(mfrow = c(1,1))
##################################
## Model with trend and seasonality ####
fit_season_trend <- tslm(ride_ts ~ trend + I(trend^2) + season)
accuracy(fit_season_trend)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values")
#######################################
par(mfrow = c(2,1))
plot(ride_ts)
lines(fit_season_trend$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_season_trend$residuals)
par(mfrow = c(1,1))
rm(list = ls())
______________________________________________________________________________
#setwd("D:/Shivani 2017/LBS classes/Data Science/Forecasting/Regression_Forecasting")
ride <- read.csv("Amtrak data.csv", header = TRUE, stringsAsFactors = TRUE)
head(ride)
ride_ts <- ts(ride$Ridership, start = c(1991,1), end = c(2004, 3), freq = 12)
ride_ts
plot(ride_ts, xlab = "Time", ylab = "Ridership", ylim = c(1300, 2300))
library(forecast)
accuracy(fit_tren_sea)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values (Linear Trend with
Season)")
##########
par(mfrow = c(2,1))
plot(ride_ts, xlim = c(1990, 2006))
lines(fit_tren_sea$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_tren_sea$residuals)
par(mfrow = c(1,1))
##########
par(mfrow = c(2,1))
plot(ride_ts, xlim = c(1990, 2006))
lines(fit_quad_sea$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_quad_sea$residuals)
par(mfrow = c(1,1))
rm(list = ls())
__________________________________________________________________________________
Revised Partition
#setwd("D:/Shivani 2017/LBS classes/Data Science/Forecasting/Regression_Forecasting")
head(ride)
ride_ts <- ts(ride$Ridership, start = c(1991,1), end = c(2004, 3), freq = 12)
ride_ts
library(forecast)
accuracy(fit_tren_sea)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values (Linear Trend with
Season)")
##########
par(mfrow = c(2,1))
plot(ride_ts, xlim = c(1990, 2006))
lines(fit_tren_sea$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_tren_sea$residuals)
par(mfrow = c(1,1))
summary(fit_quad_sea)
accuracy(fit_quad_sea)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values (Quadratic Trend
with Season)")
##########
par(mfrow = c(2,1))
plot(ride_ts, xlim = c(1990, 2006))
lines(fit_quad_sea$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_quad_sea$residuals)
par(mfrow = c(1,1))
rm(list = ls())
--------------------------------------------------------------------------------------------------------------------------------------
ARIMA
install.packages("forecast")
library(forecast)
install.packages("tseries")
library(tseries)
plot(dNile)
adf.test(dNile)
# as now we now we need to difference one timeso we will apply adf to dnile not nile
# ADF test proves that time series is stationary, therefore, proceed further.
par(mfrow = c(2,1))
Acf(dNile)
Pacf(dNile)
par(mfrow = c(1,1))
accuracy(fit)
qqnorm(fit$residuals)
qqline(fit$residuals)
Box.test(fit$residuals, type = "Ljung-Box") # box.test value should be >.05 to satisy the test
forecast(fit, 3)
forecast(fit1, 3)
accuracy(fit1)
plot(forecast(fit1, 3))
rm(list = ls())