You are on page 1of 7

PlotTimeSerieForecast <- function (DataSet, TrainingSet.start, TestSet.

start,
TrainingSet.forecast, GraphTitle)
{
#
# TrainingSet.start =
# 1 =>
# |--------------------------- DataSet ----------------------------|
# |--------------- Training Set ---------------|----- TestSet -----|
#
# >1 =>
# |--------------------------- DataSet ----------------------------|
# |----- Not Used -----|----- Training Set ----|----- TestSet -----|
#
# DataSet must be a Time Serie with Frequency greater than 1
#

DataSet.length = length (DataSet)

DataSet.frequency = frequency (DataSet)

DataSet.X <- time(DataSet)

DataSet.Y <- getDataPart(DataSet)

X.min = DataSet.X [1]


X.max = DataSet.X [DataSet.length]

mean.NonNA_index <- which(!is.na(TrainingSet.forecast$mean))


lower.NonNA_index <- which(!is.na(TrainingSet.forecast$lower[,2]))
upper.NonNA_index <- which(!is.na(TrainingSet.forecast$upper[,2]))

if ((length(lower.NonNA_index) == 0) || (length(upper.NonNA_index) == 0)) {


print("No available prediction")
}
else {
Y <- TrainingSet.forecast$lower[,2]
Y.NonNA_index <- which(!is.na(Y))
Y.NonNA<-Y[min(Y.NonNA_index):max(Y.NonNA_index)]
Y.min <- min(Y.NonNA, DataSet)

Y <- TrainingSet.forecast$upper[,2]
Y.NonNA_index <- which(!is.na(Y))
Y.NonNA<-Y[min(Y.NonNA_index):max(Y.NonNA_index)]
Y.max <- max(Y.NonNA, DataSet)

X.Start.forecast = DataSet.X [TestSet.start]


X.Start.training = DataSet.X [TrainingSet.start]

TestSet <- ts(DataSet.Y [TestSet.start:DataSet.length], frequency =


DataSet.frequency)

TrainingSet <- ts(DataSet.Y [TrainingSet.start:(TestSet.start-1)],


frequency = DataSet.frequency)

plot(DataSet,
main = GraphTitle,
xlim = c(X.min, X.max),
ylim = c(Y.min, Y.max),
col="grey")

lines (ts(TrainingSet,
frequency = frequency (DataSet),
start = c(X.Start.training , 1)),

xlim = c(X.min, X.max),


ylim = c(Y.min, Y.max),
col="blue")

lines (ts(TrainingSet.forecast$mean,
frequency = frequency (DataSet),
start = c(X.Start.forecast, 1)),

xlim = c(X.min, X.max),


ylim = c(Y.min, Y.max),
col="red")

lines (ts(TrainingSet.forecast$upper [,2],


frequency = frequency (DataSet),
start = c(X.Start.forecast, 1)),

xlim = c(X.min, X.max),


ylim = c(Y.min, Y.max),
col="orange")

lines (ts(TrainingSet.forecast$lower [,2],


frequency = frequency (DataSet),
start = c(X.Start.forecast, 1)),

xlim = c(X.min, X.max),


ylim = c(Y.min, Y.max),
col="orange")

lines (ts(TestSet,
frequency = frequency (DataSet),
start = c(X.Start.forecast, 1)),

xlim = c(X.min, X.max),


ylim = c(Y.min, Y.max),
col="black")

abline(v = DataSet.X [TrainingSet.start], col = "blue")


abline(v = DataSet.X [TestSet.start - 1], col = "blue")
}

DoForecast <- function (ForecastingMethod, DataSet, DataSetName, TestSet.length,


TrainingSet.start)
{
#
# ForecastingMethods are
# - "auto.arima"
# - "drift"
# - "ets"
# - "holt"
# - "HoltWinters Exponential Smoothing"
# - "HoltWinters Non-Seasonal"
# - "HoltWinters Seasonal"
# - "hw additive"
# - "hw multiplicative"
# - "meanf"
# - "naive"
# - "rwf" for Randow Walk
# - "ses"
# - "snaive"
# - "splinef mle"
# - "stlf"
# - "stlm arima"
# - "stlm ets"
# - "thetaf"
# - "ucm"
#

#
# TrainingSet.start =
# 1 =>
# |--------------------------- DataSet ----------------------------|
# |--------------- Training Set ---------------|----- TestSet -----|
#
# >1 =>
# |--------------------------- DataSet ----------------------------|
# |----- Not Used -----|----- Training Set ----|----- TestSet -----|
#
# DataSet must be a Time Serie with Frequency greater than 1
#

#
# Forecasting Accuracy measurements
#
# ME: Mean Error
# RMSE: Root Mean Squared Error
# MAE: Mean Absolute Error
# MPE: Mean Percentage Error
# MAPE: Mean Absolute Percentage Error
# MASE: Mean Absolute Scaled Error
# ACF1: Autocorrelation of errors at lag 1
#

if (!require("forecast")) install.packages("forecast")

library("forecast")

DataSet.frequency = frequency (DataSet)

DataSet.length = length (DataSet)

TestSet.start = DataSet.length - TestSet.length + 1

TestSet <- ts(DataSet [TestSet.start:DataSet.length], DataSet.frequency)

TrainingSet <- ts(DataSet [TrainingSet.start:(TestSet.start-1)], frequency =


DataSet.frequency)

if (DataSet.frequency >= 2) {
Continue = TRUE

print("Please wait. Time execution depends on Forecasting Method and on


Training Set Length and Frequency")

BeforeForecast <- proc.time()

if (ForecastingMethod == "auto.arima") {

TrainingSet.fit <- auto.arima(TrainingSet)

TrainingSet.fit.forecast <- forecast(TrainingSet.fit, h =


TestSet.length)
}
else if (ForecastingMethod == "drift") {

TrainingSet.fit.forecast <- rwf(TrainingSet, drift=TRUE, h =


TestSet.length)
}
# else if (ForecastingMethod == "croston") {
#
# TrainingSet.fit.forecast <- croston(TrainingSet, h =
TestSet.length)
# }
else if (ForecastingMethod == "ets") {

TrainingSet.fit <- ets(TrainingSet)

TrainingSet.fit.forecast <- forecast(TrainingSet.fit, h =


TestSet.length)
}
else if (ForecastingMethod == "holt") {

TrainingSet.fit.forecast <- holt(TrainingSet, h = TestSet.length)


}
else if (ForecastingMethod == "HoltWinters Exponential Smoothing") {

TrainingSet.fit <- HoltWinters(TrainingSet, beta=FALSE,


gamma=FALSE)

TrainingSet.fit.forecast <- forecast(TrainingSet.fit, h =


TestSet.length)
}
else if (ForecastingMethod == "HoltWinters Non-Seasonal") {

TrainingSet.fit <- HoltWinters(TrainingSet, gamma=FALSE)

TrainingSet.fit.forecast <- forecast(TrainingSet.fit, h =


TestSet.length)
}
else if (ForecastingMethod == "HoltWinters Seasonal") {

TrainingSet.fit <- HoltWinters(TrainingSet, , seasonal = "mult")

TrainingSet.fit.forecast <- forecast(TrainingSet.fit, h =


TestSet.length)
}
else if (ForecastingMethod == "hw additive") {
TrainingSet.fit.forecast <- hw(TrainingSet, h = TestSet.length,
seasonal = "additive")
}
else if (ForecastingMethod == "hw multiplicative") {

TrainingSet.fit.forecast <- hw(TrainingSet, h = TestSet.length,


seasonal = "multiplicative")
}
else if (ForecastingMethod == "meanf") {

TrainingSet.fit.forecast <- meanf(TrainingSet, h =


TestSet.length)
}
else if (ForecastingMethod == "naive") {

TrainingSet.fit.forecast <- naive(TrainingSet, h =


TestSet.length)
}
else if (ForecastingMethod == "rwf") {

# rwf = Random Walk

TrainingSet.fit.forecast <- rwf(TrainingSet, h = TestSet.length)


}
else if (ForecastingMethod == "ses") {

TrainingSet.fit.forecast <- snaive(TrainingSet, h =


TestSet.length)
}
else if (ForecastingMethod == "snaive") {

TrainingSet.fit.forecast <- snaive(TrainingSet, h =


TestSet.length)
}
else if (ForecastingMethod == "splinef mle") {

TrainingSet.fit.forecast <- splinef(TrainingSet, h =


TestSet.length, method = "mle")

}
else if (ForecastingMethod == "stlf") {

TrainingSet.fit.forecast <- stlf(TrainingSet, h = TestSet.length)

}
else if (ForecastingMethod == "stlm arima") {

TrainingSet.fit <- stlm(TrainingSet, method = "arima")

TrainingSet.fit.forecast <- forecast(TrainingSet.fit, h =


TestSet.length)

}
else if (ForecastingMethod == "stlm ets") {

TrainingSet.fit <- stlm(TrainingSet, method = "ets")

TrainingSet.fit.forecast <- forecast(TrainingSet.fit, h =


TestSet.length)
}
else if (ForecastingMethod == "thetaf") {

TrainingSet.fit.forecast <- thetaf(TrainingSet, h =


TestSet.length)

}
else if (ForecastingMethod == "ucm") {

Continue = TRUE

if (!require("rucm")) install.packages("rucm")

library(rucm)

TrainingSet.fit <- ucm(formula = TrainingSet~0, data =


TrainingSet, level = TRUE)

TrainingSet.fit.forecast <- forecast(TrainingSet.fit$s.level, h =


TestSet.length)

}
else {

print(paste(ForecastingMethod, "NOT available !"))


Continue = FALSE
}

AfterForecast <- proc.time()

print(paste(ForecastingMethod, "execution time", AfterForecast [1] -


BeforeForecast [1]))
}

if (Continue == TRUE) {

if (TrainingSet.start == 1) {

TrainingSet.forecast.name = paste ("FULL", DataSetName,


ForecastingMethod)
}
else {

TrainingSet.forecast.name = paste ("RESTRICTED", DataSetName,


ForecastingMethod)
}

TrainingSet.fit.forecast.upper <-
getDataPart(TrainingSet.fit.forecast$upper [, 2])

TrainingSet.fit.forecast.mean <-
getDataPart(TrainingSet.fit.forecast$mean)

TrainingSet.fit.forecast.lower <-
getDataPart(TrainingSet.fit.forecast$lower [, 2])
Prediction.Interval.average <- mean(TrainingSet.fit.forecast.upper -
TrainingSet.fit.forecast.lower)

Prediction.Interval.median <- median(TrainingSet.fit.forecast.upper -


TrainingSet.fit.forecast.lower)

print (paste(TrainingSet.forecast.name, "Prediction Interval Average",


Prediction.Interval.average))

print (paste(TrainingSet.forecast.name, "Prediction Interval Median ",


Prediction.Interval.median))

TrainingSet.forecast.accuracy <-
accuracy(TrainingSet.fit.forecast.mean, TestSet)

rownames(TrainingSet.forecast.accuracy) <- c(TrainingSet.forecast.name)

print(TrainingSet.forecast.accuracy)

if (TrainingSet.start == 1) {

GraphTitle = paste (DataSetName, "\n", ForecastingMethod, " FULL


DataSet")
}
else {

GraphTitle = paste (DataSetName, "\n", ForecastingMethod, "


RESTRICTED DataSet")
}

PlotTimeSerieForecast(DataSet, TrainingSet.start, TestSet.start,


TrainingSet.fit.forecast, GraphTitle)
}

You might also like