Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

library ( forecast ) # ARIMA library ( randomForest ) # Random Forest library ( kernlab ) # Support Vector Machines # dataset < -

library(forecast) # ARIMA
library(randomForest) # Random Forest
library(kernlab) # Support Vector Machines
# dataset<- read.csv(E:\New folder\EE623_W3_XREG3MonthsData_LOADFORECAST.csv)
dataset=read.csv(file.choose(),header=TRUE)
head(dataset) #displays first few rows of the dataset
names(dataset) #displays columns names of the dataset
pastset<- dataset[1:2184,]
Yts=ts(pastset$Actual,start=c(2015,1), frequency=8760) # The ts() function will convert a numeric vector into an R time series object
require(forecast) # can be used to attach and load add-on packages which are alredy installed
fit<- auto.arima(Yts)
fit
print(dim(pastset[1:2184, c(3,4)]))
print(dim(pastset[2185:2208, c(3,4)]))
# Initialize the Multivariate ARIMA model
# The relationship between the predictor(input) variables and forecasted variable(Load) is initialized through this model
InitializedModel = arima(Yts,order = c(4,1,5),xreg = pastset[1:2184,c(3,4)])
# Assuming InitializedModel is correctly fitted
# Predict the next day load-next 24 samples
# single day has been selected(July 31,2015)-forecast & validate the performance of the proposed model
future_xreg <- dataset[2185:2208, c(3,4)
# Prediction=forecast(InitializedModel,h=24,xreg=dataset[2185:2208,c(3,4,5,6,7)]) #add xreg for multivariate
str(predictions)
# plot(predictions$pred, type="1")
# Basic plot of the predictions
plot(predictions$pred, type="1",main ="Forecasted Values",xlab = "Time",ylab = "Predicted Value")
# Adding a ribbon for standard error (assuming a normal distribution for the error)
# Typically, you would use pred +/-1.96*se for 95% confidence interval
# Adjust the multiplier as needed for different confidence levels
lines(predictions$pred +1.96*predictions$se, col = "blue",Ity = "dashed")
lines(predictions$pred -1.96*predictions$se, col = "blue",Ity = "dashed")
f <- predictions$pred
plot(f, main ="ARIMA Forecasted values",xlab = "Time",ylab = "Value",col ="blue" )
# Initialize the multivariate ARIMA model
# The relationship between the predictor(input) variables and forecasted variable(Load) is initialized through this model.
# InitializedModel = arima(Yts, order = c(4,1,5),xreg=pastset[1:2184,c(3,4,5,6,7)]) #add command xreg for multivariate
# Predict the next day load-next 24 samples
# Single day has been selected (July 31,2015)-forecast & validate the performance of the proposed model
# Prediction=forecast(InitializedModel, h=24, xreg=dataset[2185:2208,c(3,4,5,6,7)]) #add xreg for multivariate
# plot(Prediction)
# Prediction$mean
# plot(Prediction$mean)
# f=Prediction$mean
# plot(f)
# f
# Mape - ARIMA
mape = mean(abs(dataset$Actual[2185:2208]-f)/dataset$Actual[2185:2208])
mape
## Random forest(RF)
# Ensemble based Method - use multiple learning algorithms-
# the ensembles use the small portion of the larger dataset, it is extremely effective in handling the large dataset
# achieve good accuracy as well as overcoming the over-fitting problem.
# Training Dataset-Load
pastset <-dataset[1:2184,]
# Training the RF Model
rf <-randomForest(Actual~P.DD.1+P.DD.6+Temp+irradiance+windspeed,data=pastset,importance=TRUE,ntree=1000,mtry=2)
# mtry: Number of variables randomly sampled as candidates at each split
# ntree: Number of trees to grow
# Larger number of trees produce more stable models and covariate importance estimates, but require more memory and a longer run time.
# For larger datasets, 500 or more may be required.
# For regression models, mtry is the number of predictor variables divided by 3.
# Test dataset - Forecast day (July 30,2015)
forecastset <-dataset[2185:2208,]
forecastset
attach(forecastset)
# The database is attached to the R search path.
# This means that the database is searched by R when evaluating a variable,so objects in the database can be accessed by simply giving their names.
# Predict the next day load- next 24 samples
RF.pred <-predict(rf, newdata=data.frame(P.DD.1,P.DD.6,data=forecastset))
RF.pred
# Plot the Rf predictions
plot(1:24,RF.pred, type ="1", main="Random Forest Forecasted Values", xlab="Time",ylab="Predicted Value",col="darkgreen")
# MAPE RF
mape=mean(abs(dataset$Actual[2185:2208]-RF.pred)/dataset$Actual[2185:2208])
mape
### Support Vector Machines- supervised learning model that analyzes data for regression in ths work
# Training Dataset-Load
pastset <-dataset[1:2184,]
# Training the SVM Model
svm.model <-ksvm(Actual ~ P.DD.1+ P.DD.6,data = pastset,kernel="vanilladot")
# vanilladot helps in fitting a linear SVM
# Fit non-linear SVM with Gaussian(rbf= radial basis function), then kernel = "rbfdot"
# Test dataset -Forecast day(July 30,2015)
forecastset <-dataset[2185:2208,]
attach(forecastset)
# Predict the next day load-next 24 samples
SVM.pred <-predict(svm.model,newdata=data.frame(P.DD.1,P.DD.6,Temp,irradiance,windspeed,data=forecastset))
SVM.pred
plot(1:24,SVM.pred, type="1",main="SVM Forecasted Values",xlab="Time",ylab="Predicted Value",col="darkred")
# MAPE-SVM
mape=mean(abs(dataset$Actual[2185:2208]-SVM.pred)/dataset$Actual[2185:2208])
mape
I run into this error message anytime I run the arima model. Any help will be appreciated.
>InitializedModel = arima(Yts,order= c(4,1,5),xreg = pastset[1:2184,c(3,4,5,6,7)])
Error in solve.default(res$hessian*n.used, A):
system is computationally singular: reciprocal condition number =8.18369e-32

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design And Implementation

Authors: KROENKE DAVID M.

1st Edition

8120322258, 978-8120322257

More Books

Students also viewed these Databases questions