This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#Load require libraries
library('ggplot2')
## Warning: package 'ggplot2' was built under R version 3.4.2
library('forecast')
## Warning: package 'forecast' was built under R version 3.4.3
library('tseries')
## Warning: package 'tseries' was built under R version 3.4.3
library(xts)
## Warning: package 'xts' was built under R version 3.4.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.4.2
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(Metrics)
## Warning: package 'Metrics' was built under R version 3.4.4
##
## Attaching package: 'Metrics'
## The following object is masked from 'package:forecast':
##
## accuracy
#Load the data from file
my_daily_data <- read.csv("DataSet.csv", header = TRUE, sep=",", stringsAsFactors = FALSE)
#Create date object
dates <- as.Date(my_daily_data[,2], format = "%d-%m-%Y" )
#Create XTS Object
data_xts <- as.xts(x = my_daily_data$Close, order.by = dates)
#Stationary Test
adf.test(data_xts, alternative = "stationary")
##
## Augmented Dickey-Fuller Test
##
## data: data_xts
## Dickey-Fuller = -2.0098, Lag order = 9, p-value = 0.5742
## alternative hypothesis: stationary
#Create an Index to seperate Train and Test Data
#Train data - 1st Jan 2016 to 31st June 2017
#Test data - 1st July 2017 to 3rd March 2018
index <- c(1:517)
train <- data_xts[index,]
test <- data_xts[-index,]
#Determine ARIMA Model for Training data
auto.arima(train)
## Series: train
## ARIMA(0,2,1)
##
## Coefficients:
## ma1
## -0.9799
## s.e. 0.0106
##
## sigma^2 estimated as 987.9: log likelihood=-2507.48
## AIC=5018.96 AICc=5018.98 BIC=5027.45
#Create Fit Model for Test data using Above model
fit <- Arima(test, order = c(0,2,1))
#Create a vector using Fitted values on Test data
fit_values <- as.vector(fit$fitted)
#Create a date vector similar to Test data
fit_dates <- seq(as.Date("2017/06/01"), by = "day", length.out = 276)
#Create XTS object for Fitted values
fit_xts <- as.xts(x = fit_values, order.by = fit_dates)
#Forecast for next 30 days using Fit Model
fcast <- forecast(fit, h = 30)
#Calculate RMSE comparing Test data and fitted values on Test
rmse(fit$x,fit$fitted)
## Warning in se(actual, predicted): Incompatible methods ("Ops.xts",
## "Ops.ts") for "-"
## [1] 583.119
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.