Error in Forecast

Hello All,

Can you please help me with this error?
Error in forecast(fit_decomposelog, method = "rwdrift", h = 36)[, 1] :
incorrect number of dimensions

The entire code is as follows:

install packages, if needed, and then load the packages.

install.packages("pacman")
install.packages("ggplot2")
install.packages("data.table")
install.packages("fpp2")
install.packages("forecast")
install.packages("stats")
install.packages("tseries")
install.packages("zoo")
install.packages("lubridate")
install.packages("rio")
install.packages("dplyr")
install.packages("crate")
library(ggplot2)
library(data.table)
library(fpp2)
library(forecast)
library(stats)
library(tseries)
library(zoo)
library(lubridate)
library(rio)
library(dplyr)

1. Reading Time Series Data

FCL_forecast <- read.csv(file="D:\Configuraciones\sachin.nataraj\Escritorio\phd\Forecasting of freight rates\Laura\FCL-EVRG.csv", header=TRUE,stringsAsFactors = TRUE, sep=";")

Check the data

head(FCL_forecast)
print(FCL_forecast)
View(FCL_forecast)
str(FCL_forecast)

########################################### Descreptive analysis #################################################
##########Check missing period and add missind data with duplicate of previous period value#########

#Check class of date column in data / confirming "factor" class
class(FCL_forecast[,7])
class(FCL_forecast[,8])

#Convert "factor" to "Date" according to cell formatting in CSV file
fmt <- "%d/%m/%Y"
FCL_forecast[,7] <- as.Date(FCL_forecast[,7], format = fmt)
FCL_forecast[,8] <- as.Date(FCL_forecast[,8], format = fmt)

######to read file and check for missing values#########
initialDate <- FCL_forecast[1,7]
n <- length(FCL_forecast[,7])
finalDate <- FCL_forecast[n,7]

auxDate <- initialDate
missingRows <- NULL

while((finalDate-auxDate)!=0){
if(format(auxDate, format = "%d")=="01"){
auxDate <- auxDate+15
}else{
auxDate <- auxDate-15
month(auxDate) <- month(auxDate) + 1}
if(!(auxDate %in% FCL_forecast[,7])){
missingRows <- c(missingRows, auxDate)}
}
as.Date(missingRows)

auxDate <- initialDate
newDB <- rbind(FCL_forecast,FCL_forecast[(1:length(missingRows)),])
n2 <- dim(newDB)[1]
indexReading <- 2
indexWriting <- 2

while((finalDate-auxDate)!=0){
if(format(auxDate, format = "%d")=="01"){
auxDate <- auxDate+15
}else{
auxDate <- auxDate-15
month(auxDate) <- month(auxDate) + 1}

if(auxDate %in% FCL_forecast[,7]){
	newDB[indexWriting,] <- FCL_forecast[indexReading,]
	indexReading <- indexReading + 1
}else{
	newDB[indexWriting,-c(7,8)] <- FCL_forecast[(indexReading-1),-c(7,8)]
	newDB[indexWriting,7] <- auxDate
	auxDate2 <- auxDate
	if(format(auxDate2, format = "%d")=="01"){
		auxDate2 <- auxDate2 + 14
	}else{
		day(auxDate2) <- days_in_month(auxDate2)}
	newDB[indexWriting,8] <- auxDate2}	
indexWriting <- indexWriting + 1

}

newDB[n2,] <- FCL_forecast[n,]

write.table(newDB, file = "newDB.csv",row.names=F, sep = ",")
getwd()

sort(newDB$periods, decreasing = FALSE)
View(newDB)
str(newDB)
summary(newDB)
#########New data set created with no missing period##########

Define pairs origin-destination and core carriers

origin <- c("BEANR")
destination <- c("CNSHA")

listcarriers <- c("YMLU", "EVRG", "MSCU", "COSU","CMDU", "HLCU", "MAEU")

##2. store the data as time series

FCL_forecasttimeseries <- ts(newDB[,6], start = c(2012,07), frequency = 24)
FCL_forecasttimeseries
plot(newDB[,6])

3. Plotting Time Series

par(mar = c(5, 5, 5, 5))
plot.ts(ylab = "FCL_forecast (USD)", xlab = "Years", col= "red", FCL_forecasttimeseries)

###########seasonal plot year wise#########
ggseasonplot(FCL_forecasttimeseries, year.labels=TRUE, year.labels.left=TRUE)+ylab("cost")+xlab("Period") +ggtitle("seasonal plot: FCL_Baserates")

##########Polar Seasonal PLot year wise#########
ggseasonplot(FCL_forecasttimeseries, polar = TRUE)+ylab("degree")+ggtitle("Polar Seasonal plot: FCL_Baserates")

monthplot(FCL_forecasttimeseries)

#Decompose data into seasonal, trend and irregular components using classical decomposition

fit_decompose <- decompose(FCL_forecasttimeseries, type = "multiplicative")
fit_decompose
plot(fit_decompose)

###########decompose & Plot the unpredictable part of time series######
fit_decompose$random
decompose(fit_decompose$random)
plot(decompose(fit_decompose$random))

########Decompose with ERROR bar (if Trend & seasonality is significant or not)#############
fit_decomposelog <-stl(log10(FCL_forecasttimeseries),s.window = "periodic" )
plot(fit_decomposelog)
fit_decomposelog

########################################### Predictive analysis #################################################

#Install and load forecast package if not installed
if(!require(forecast)) {
install.packages("forecast", dependencies = T)
library(forecast) }

###########Splitting data into training and test data sets############
TS_Train <- window(FCL_forecasttimeseries, start=c(2012,07), end=c(2017,12), freq=24)

TS_Test <- window(FCL_forecasttimeseries, start=c(2018,1), freq=24)

TS_Train
TS_Test
summary(TS_Test)
View(TS_Test)
autoplot(TS_Train, series = "Train")+ autolayer(TS_Test, series = "Test") + ggtitle("FCL rates Training & Test data") + xlab("periods")+ylab("Rates")+ guides(colour=guide_legend(title = "Forecast"))

#############FORECASTING MAEHODS ############
###########R1.Random walk with drift forecasting (using Log value) for Test & Training data#########

fit_decomposelog <- stl(log10(TS_Train), s.window = "p")
fit_decomposelog
TS_Train_stl <- forecast(fit_decomposelog, method = "rwdrift", h=36) ##### (h=36)No. of periods that you want to forecast######
plot(TS_Train_stl)
TS_Train_stl
View(TS_Train_stl)
########Accuracy measures: RMSE and MAPE using RWD#########
vec2 <- 10^(cbind(log10(TS_Test), as.data.frame(forecast(fit_decomposelog, method = "rwdrift", h=36)[,1])))
ts.plot(vec2, col=c("blue","red"), main="FCL forecast vs actual")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.