You are on page 1of 11

library("forecast")

library("zoo")
Amtrak.data <- read.csv("Amtrak data.csv")
ridership.ts <- ts(Amtrak.data$Ridership, start = c(1991, 1), end = c(2004, 3), freq = 12)
stepsAhead <- 12
nTrain <- length(ridership.ts) - stepsAhead
train.ts <- window(ridership.ts, start = c(1991, 1), end = c(1991, nTrain))
valid.ts <- window(ridership.ts, start = c(1991, nTrain + 1), end = c(1991, nTrain + stepsAhead))

#Model 1: Linear trend model


ridership.lm.train <- tslm(train.ts ~ trend) #Creating Linear trend model
coefficients(ridership.lm.train) #Display Beta ceoefficient values
summary(ridership.lm.train) #Summary of train data
ridership.lm.pred <- forecast(ridership.lm.train, h=stepsAhead) #Forecasting
plot(ridership.lm.pred, ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n", main = "Linear trend
model", flty = 2) #Plot Creation
accuracy(ridership.lm.pred, h=stepsAhead) #Accuracy check of the model

#Model 2: Exponential trend model


ridership.lm.exp <- tslm(train.ts ~ trend,lambda = 0) #Creating Exponential trend model
coefficients(ridership.lm.exp) #Display Beta ceoefficient values
summary(ridership.lm.exp) #Summary of train data
ridership.lm.exp.pred <- forecast(ridership.lm.exp, h=stepsAhead) #Forecasting
plot(ridership.lm.exp.pred, ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n", main =
"Exponential trend model", flty = 2) #Plot Creation
accuracy(ridership.lm.exp.pred, h=stepsAhead) #Accuracy check of the model

#Model 3: Polynomial trend model


ridership.lm.poly <- tslm(train.ts ~ poly(trend,2)) #Creating Polynomial trend model
coefficients(ridership.lm.train.poly) #Display Beta ceoefficient values
summary(ridership.lm.train.poly) #Summary of train data
ridership.lm.poly.pred <- forecast(ridership.lm.train.poly, h=stepsAhead) #Forecasting
plot(ridership.lm.poly.pred, ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n", main =
"Polynomial trend model", flty = 2) #Plot Creation
accuracy(ridership.lm.poly.pred , h=stepsAhead)#Accuracy check of the model

#Model 4:Seasonal additive model


ridership.lm.season <- tslm(train.ts ~ season)
coefficients(ridership.lm.season)#Display Beta ceoefficient values
summary(ridership.lm.season)#Summary of train data
ridership.lm.season.pred <- forecast(ridership.lm.season, h=stepsAhead)#Forecasting
plot(ridership.lm.season.pred, ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n", main =
"Seasonal additive model", flty = 2) #Plot Creation
accuracy(ridership.lm.season.pred, h=stepsAhead) #Accuracy check of the model

#Model 5:Seasonal multiplicative model


ridership.lm.season.mul <- tslm(train.ts ~ season,lambda=0)
coefficients(ridership.lm.season.mul)#Display Beta ceoefficient values
summary(ridership.lm.season.mul)#Summary of train data
ridership.lm.season.mul.pred <- forecast(ridership.lm.season.mul, h=stepsAhead)#Forecasting
plot(ridership.lm.season.mul.pred, ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n", main =
"Seasonal multiplicative model", flty = 2) #Plot Creation
accuracy(ridership.lm.season.mul.pred, h=stepsAhead)#Accuracy check of the model

#Model 6:Seasonal + trend 6


ridership.lm.seatrend<- tslm(train.ts ~ poly(trend,2)+season)
coefficients(ridership.lm.seatrend)#Display Beta ceoefficient values
summary(ridership.lm.seatrend)#Summary of train data
ridership.lm.seatrend.pred <- forecast(ridership.lm.seatrend, h=stepsAhead)#Forecasting
plot(ridership.lm.seatrend.pred, ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n", main =
"Seasonal multiplicative model", flty = 2) #Plot Creation
accuracy(ridership.lm.seatrend.pred, h=stepsAhead)#Accuracy check of the model

library("forecast")

library("zoo")

American.data <- read.csv("American Airlines.csv")

American.ts <- ts(American.data$NEW.YORK...ATLANTA, start = c(2012, 1), end = c(2017, 5), freq =
12)

stepsAhead <- 13

nTrain <- length(American.ts) - stepsAhead

attach(UdupiRainfall17)

American_train.ts <- window(American.ts, start = c(2012, 1), end = c(2012, nTrain))

American_valid.ts <- window(American.ts, start = c(2012, nTrain + 1), end = c(2012, nTrain +
stepsAhead))

ma.trailing <- rollmean(American.ts,k = stepsAhead, align = "right")

ma.centered <- ma(American.ts, order = stepsAhead)

plot(American.ts, ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n", main = "LAX - IAD")

lines(ma.centered, lwd = 2)

lines(ma.trailing, lwd = 2, lty = 2)

legend(2012,2017, c("Ridership","Centered Moving Average", "Trailing Moving Average"),


lty=c(1,1,2),

lwd=c(1,2,2), bty = "n")


#simple exponential smoothening

ses <- ets(American.ts, model = "ANN", alpha = 0.2)

ses.pred <- forecast(ses, h = stepsAhead, level = 0.3)

ses.opt <- ets(American.ts, model = "ANN")

ses.opt.pred <- forecast(ses.opt, h = stepsAhead, level = 0)

ses.opt

accuracy(ses.pred$mean, stepsAhead)

accuracy(ses.opt.pred$mean, stepsAhead)

#holt's

diff.twice.ts <- diff(diff(American.ts, lag = 12), lag = 1)

hw <- ets(American.ts, model = "AAM", restrict = FALSE)

hw.pred <- forecast(hw, h = stepsAhead, level = 0)

plot(hw.pred, ylab = "Holt's", xlab = "Time", bty = "l", xaxt = "n"

, main = "Holt's", flty = 2)

# triple smoothening

hw <- ets(American.ts, model = "MAA", restrict = FALSE)

hw.pred <- forecast(hw, h = stepsAhead, level = 0)

plot(hw.pred, ylab = "Winter", xlab = "Time", bty = "l", xaxt = "n"

, main = " Winter", flty = 2)


Linear Trend:
R Codes-
library("forecast")
#read amtrak data csv file
Amtrak.data <- read.csv("Amtrak data.csv")
ridership.ts <- ts(Amtrak.data$Ridership, start = c(1991, 1), end = c(2004, 3), freq = 12)
plot(ridership.ts)

#data partitioning
stepsAhead <- 12
nTrain <- length(ridership.ts) - stepsAhead
train.ts <- window(ridership.ts, start = c(1991, 1), end = c(1991, nTrain))
valid.ts <- window(ridership.ts, start = c(1991, nTrain + 1), end = c(1991, nTrain + stepsAhead))

# linear trend model


ridership.lm <- tslm(train.ts ~ trend)
summary(ridership.lm)
ridership.lm.pred <- forecast(ridership.lm, h = stepsAhead, level = 0.4)
plot(ridership.lm.pred)
plot(ridership.lm.pred, ylim = c(1300, 2600), ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n",
xlim = c(1991,2006.25), main = "", flty = 2)

#axis resizing
axis(1, at = seq(1991, 2006, 1), labels = format(seq(1991, 2006, 1)))
lines(ridership.lm$fitted, lwd = 2)
lines(valid.ts)
lines(c(2004.25 - 3, 2004.25 - 3), c(0, 3500))
lines(c(2004.25, 2004.25), c(0, 3500))
text(1996.25, 2500, "Training")
text(2002.75, 2500, "Validation")
arrows(2004 - 3, 2450, 1991.25, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)
arrows(2004.5 - 3, 2450, 2004, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)

#error calculation section


accuracy(ridership.lm.pred$mean, valid.ts)

Output-
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1713.0288 27.0855 63.245 < 2e-16 ***
trend 1.2053 0.3175 3.796 0.000215 ***
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Errors:
ME RMSE MAE MPE MAPE ACF1 Theil's U
Test set 168.8525 210.0252 191.5552 7.816868 9.054327 0.1975228 1.250612

Exponential Trend:
library("forecast")
#read amtrak data csv file
Amtrak.data <- read.csv("Amtrak data.csv")
ridership.ts <- ts(Amtrak.data$Ridership, start = c(1991, 1), end = c(2004, 3), freq = 12)
plot(ridership.ts)

#data partitioning 12 validation data


stepsAhead <- 12
nTrain <- length(ridership.ts) - stepsAhead
train.ts <- window(ridership.ts, start = c(1991, 1), end = c(1991, nTrain))
valid.ts <- window(ridership.ts, start = c(1991, nTrain + 1), end = c(1991, nTrain + stepsAhead))

# exponential trend model


ridership.lm <- tslm(train.ts ~ trend,lambda = 0)
summary(ridership.lm)
ridership.lm.pred <- forecast(ridership.lm, h = stepsAhead, level = 0.4)
plot(ridership.lm.pred)
plot(ridership.lm.pred, ylim = c(1300, 2600), ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n",
xlim = c(1991,2006.25), main = "", flty = 2)

#axis resizing
axis(1, at = seq(1991, 2006, 1), labels = format(seq(1991, 2006, 1)))
lines(ridership.lm$fitted, lwd = 2)
lines(valid.ts)
lines(c(2004.25 - 3, 2004.25 - 3), c(0, 3500))
lines(c(2004.25, 2004.25), c(0, 3500))
text(1996.25, 2500, "Training")
text(2002.75, 2500, "Validation")
arrows(2004 - 3, 2450, 1991.25, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)
arrows(2004.5 - 3, 2450, 2004, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)

#error calculation section


accuracy(ridership.lm.pred$mean, valid.ts)
Output-
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.4439865 0.0154745 481.05 < 2e-16 ***
trend 0.0006512 0.0001814 3.59 0.000452 ***
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Errors:
ME RMSE MAE MPE MAPE ACF1 Theil's U
Test set 177.6078 217.1507 197.4261 8.241939 9.322188 0.1980872 1.291944

Quadratic Trend:
library("forecast")
#read amtrak data csv file
Amtrak.data <- read.csv("Amtrak data.csv")
ridership.ts <- ts(Amtrak.data$Ridership, start = c(1991, 1), end = c(2004, 3), freq = 12)
plot(ridership.ts)

#data partitioning
stepsAhead <- 12
nTrain <- length(ridership.ts) - stepsAhead
train.ts <- window(ridership.ts, start = c(1991, 1), end = c(1991, nTrain))
valid.ts <- window(ridership.ts, start = c(1991, nTrain + 1), end = c(1991, nTrain + stepsAhead))

# Quadratic trend model


ridership.lm <- tslm(train.ts ~ trend +I(trend^2))
summary(ridership.lm)
ridership.lm.pred <- forecast(ridership.lm, h = stepsAhead, level = 0.4)
plot(ridership.lm.pred)
plot(ridership.lm.pred, ylim = c(1300, 2600), ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n",
xlim = c(1991,2006.25), main = "", flty = 2)

#axis resizing
axis(1, at = seq(1991, 2006, 1), labels = format(seq(1991, 2006, 1)))
lines(ridership.lm$fitted, lwd = 2)
lines(valid.ts)
lines(c(2004.25 - 3, 2004.25 - 3), c(0, 3500))
lines(c(2004.25, 2004.25), c(0, 3500))
text(1996.25, 2500, "Training")
text(2002.75, 2500, "Validation")
arrows(2004 - 3, 2450, 1991.25, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)
arrows(2004.5 - 3, 2450, 2004, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)

#error calculation section


accuracy(ridership.lm.pred$mean, valid.ts)
Output-

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.859e+03 3.778e+01 49.207 < 2e-16 ***
trend -4.679e+00 1.179e+00 -3.970 0.000113 ***
I(trend^2) 3.976e-02 7.714e-03 5.154 8.28e-07 ***

Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1


Errors:
ME RMSE MAE MPE MAPE ACF1 Theil's U
Test set -11.30135 137.2493 105.874 -0.9633482 5.344486 0.328795 0.8655024
Seasonal additive Trend:
library("forecast")
#read amtrak data csv file
Amtrak.data <- read.csv("Amtrak data.csv")
ridership.ts <- ts(Amtrak.data$Ridership, start = c(1991, 1), end = c(2004, 3), freq = 12)
plot(ridership.ts)

#data partitioning
stepsAhead <- 12
nTrain <- length(ridership.ts) - stepsAhead
train.ts <- window(ridership.ts, start = c(1991, 1), end = c(1991, nTrain))
valid.ts <- window(ridership.ts, start = c(1991, nTrain + 1), end = c(1991, nTrain + stepsAhead))

# Seasonal additive trend model


ridership.lm <- tslm(train.ts ~ season)
summary(ridership.lm)
ridership.lm.pred <- forecast(ridership.lm, h = stepsAhead, level = 0.4)
plot(ridership.lm.pred)
plot(ridership.lm.pred, ylim = c(1300, 2600), ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n",
xlim = c(1991,2006.25), main = "", flty = 2)

#axis resizing
axis(1, at = seq(1991, 2006, 1), labels = format(seq(1991, 2006, 1)))
lines(ridership.lm$fitted, lwd = 2)
lines(valid.ts)
lines(c(2004.25 - 3, 2004.25 - 3), c(0, 3500))
lines(c(2004.25, 2004.25), c(0, 3500))
text(1996.25, 2500, "Training")
text(2002.75, 2500, "Validation")
arrows(2004 - 3, 2450, 1991.25, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)
arrows(2004.5 - 3, 2450, 2004, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)

#error calculation section


accuracy(ridership.lm.pred$mean, valid.ts)

Output-

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1603.95 32.62 49.172 < 2e-16 ***
season2 -37.68 46.13 -0.817 0.415498
season3 262.86 46.13 5.698 7.27e-08 ***
season4 251.29 47.08 5.337 3.88e-07 ***
season5 282.53 47.08 6.001 1.70e-08 ***
season6 241.17 47.08 5.123 1.02e-06 ***
season7 345.63 47.08 7.341 1.79e-11 ***
season8 390.68 47.08 8.298 9.64e-14 ***
season9 57.65 47.08 1.224 0.222918
season10 197.16 47.08 4.188 5.06e-05 ***
season11 187.32 47.08 3.979 0.000113 ***
season12 231.46 47.08 4.916 2.52e-06 ***

Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1


Errors:
ME RMSE MAE MPE MAPE ACF1 Theil's U
Test set 262.1078 264.7651 262.1078 12.72007 12.72007 -0.03267624 1.612042

Seasonal multiplicative Trend:


library("forecast")
#read amtrak data csv file
Amtrak.data <- read.csv("Amtrak data.csv")
ridership.ts <- ts(Amtrak.data$Ridership, start = c(1991, 1), end = c(2004, 3), freq = 12)
plot(ridership.ts)

#data partitioning
stepsAhead <- 12
nTrain <- length(ridership.ts) - stepsAhead
train.ts <- window(ridership.ts, start = c(1991, 1), end = c(1991, nTrain))
valid.ts <- window(ridership.ts, start = c(1991, nTrain + 1), end = c(1991, nTrain + stepsAhead))

# Seasonal multiplicative trend model


ridership.lm <- tslm(train.ts ~ season,lambda = 0)
summary(ridership.lm)
ridership.lm.pred <- forecast(ridership.lm, h = stepsAhead, level = 0.4)
plot(ridership.lm.pred)
plot(ridership.lm.pred, ylim = c(1300, 2600), ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n",
xlim = c(1991,2006.25), main = "", flty = 2)

#axis resizing
axis(1, at = seq(1991, 2006, 1), labels = format(seq(1991, 2006, 1)))
lines(ridership.lm$fitted, lwd = 2)
lines(valid.ts)
lines(c(2004.25 - 3, 2004.25 - 3), c(0, 3500))
lines(c(2004.25, 2004.25), c(0, 3500))
text(1996.25, 2500, "Training")
text(2002.75, 2500, "Validation")
arrows(2004 - 3, 2450, 1991.25, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)
arrows(2004.5 - 3, 2450, 2004, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)

#error calculation section


accuracy(ridership.lm.pred$mean, valid.ts)
Output-

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.37740 0.01843 400.245 < 2e-16 ***
season2 -0.02416 0.02607 -0.927 0.356
season3 0.15186 0.02607 5.826 3.97e-08 ***
season4 0.14566 0.02660 5.475 2.06e-07 ***
season5 0.16332 0.02660 6.139 8.69e-09 ***
season6 0.14055 0.02660 5.283 4.97e-07 ***
season7 0.19682 0.02660 7.398 1.32e-11 ***
season8 0.21992 0.02660 8.266 1.15e-13 ***
season9 0.03716 0.02660 1.397 0.165
season10 0.11732 0.02660 4.410 2.09e-05 ***
season11 0.11113 0.02660 4.177 5.27e-05 ***
season12 0.13588 0.02660 5.107 1.09e-06 ***

Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1


Errors:
ME RMSE MAE MPE MAPE ACF1 Theil's U
Test set 265.6621 268.2991 265.6621 12.89389 12.89389 -0.03041337 1.63308
Seasonal trend Trend:
library("forecast")
#read amtrak data csv file
Amtrak.data <- read.csv("Amtrak data.csv")
ridership.ts <- ts(Amtrak.data$Ridership, start = c(1991, 1), end = c(2004, 3), freq = 12)
plot(ridership.ts)

#data partitioning
stepsAhead <- 12
nTrain <- length(ridership.ts) - stepsAhead
train.ts <- window(ridership.ts, start = c(1991, 1), end = c(1991, nTrain))
valid.ts <- window(ridership.ts, start = c(1991, nTrain + 1), end = c(1991, nTrain + stepsAhead))

# Seasonal plus trend trend model


ridership.lm <- tslm(train.ts ~ trend +I(trend^2)+season)
summary(ridership.lm)
ridership.lm.pred <- forecast(ridership.lm, h = stepsAhead, level = 0.4)
plot(ridership.lm.pred)
plot(ridership.lm.pred, ylim = c(1300, 2600), ylab = "Ridership", xlab = "Time", bty = "l", xaxt = "n",
xlim = c(1991,2006.25), main = "", flty = 2)

#axis resizing
axis(1, at = seq(1991, 2006, 1), labels = format(seq(1991, 2006, 1)))
lines(ridership.lm$fitted, lwd = 2)
lines(valid.ts)
lines(c(2004.25 - 3, 2004.25 - 3), c(0, 3500))
lines(c(2004.25, 2004.25), c(0, 3500))
text(1996.25, 2500, "Training")
text(2002.75, 2500, "Validation")
arrows(2004 - 3, 2450, 1991.25, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)
arrows(2004.5 - 3, 2450, 2004, 2450, code = 3, length = 0.1, lwd = 1,angle = 30)

#error calculation section


accuracy(ridership.lm.pred$mean, valid.ts)
Output-

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.666e+03 2.682e+01 62.111 < 2e-16 ***
trend -5.247e+00 5.867e-01 -8.942 2.83e-15 ***
I(trend^2) 4.376e-02 3.841e-03 11.393 < 2e-16 ***
season2 -3.886e+01 2.932e+01 -1.326 0.1873
season3 2.604e+02 2.932e+01 8.881 3.98e-15 ***
season4 2.674e+02 2.995e+01 8.931 3.01e-15 ***
season5 2.978e+02 2.995e+01 9.943 < 2e-16 ***
season6 2.554e+02 2.995e+01 8.529 2.88e-14 ***
season7 3.588e+02 2.995e+01 11.980 < 2e-16 ***
season8 4.026e+02 2.995e+01 13.445 < 2e-16 ***
season9 6.832e+01 2.995e+01 2.281 0.0241 *
season10 2.065e+02 2.995e+01 6.894 1.98e-10 ***
season11 1.952e+02 2.995e+01 6.517 1.36e-09 ***
season12 2.378e+02 2.995e+01 7.939 7.52e-13 ***
Errors:
ME RMSE MAE MPE MAPE ACF1 Theil's U
Test set -34.11391 50.59855 39.2566 -1.695392 1.937465 -0.04653612 0.3115289

You might also like