Continuing from Part 3…

# Generating a time series data of daily mean of Atm Prs

library(tseries)
library(forecast)
library(TTR)

Atm.Prs.TS <- ts(new.weather.df$mean, frequency=1)
plot.ts(Atm.Prs.TS)

atmprsplot1

# This plot looks better than earlier one

#save this data in a csv format in current working directory for later use

write.table(Atm.Prs.TS, file=”atmprsts.csv”, sep=”,”)

#storing values as a daily time series

Atm.Prs.TS1 <- ts(new.weather.df$mean, frequency=365)

#decomposing the above time series

Atm.Prs.TS1decomp<- decompose(Atm.Prs.TS1)

#looking at the seasonal values
Atm.Prs.TS1decomp$seasonal

#ploting decomposed series
plot(Atm.Prs.TS1decomp)

plot2

#The decomposition plot above shows the original time series in the top plot,
# the estimated trend component in the second plot from top,
# the estimated seasonal component in the third plot from top,
# and the estimated irregular component in the bottom plot.

atmprsseasonaladj<- Atm.Prs.TS1 – Atm.Prs.TS1decomp$seasonal
plot(atmprsseasonaladj)

Rplotadj

#forecasting using HoltWinters’ Method

atmprsforecast1<- HoltWinters(Atm.Prs.TS1, beta=FALSE, gamma=FALSE)

#we can view the results:

atmprsforecast1

#The forecasts made by the function HoltWinters() are stored in a list variable called “fitted”.
# we can view these values:

atmprsforecast1$fitted

#Similarly, we can develop forecasting using HoltWinters() function for seasonally adjusted series as well…

atmprsforecast2 <- HoltWinters(atmprsseasonaladj, beta=FALSE, gamma=FALSE)
atmprsforecast2
atmprsforecast2$fitted

# We also can plot the original time series vs. the forecast series:

plot(atmprsforecast1)

hwplot1
#and seasonally adjusted series
plot(atmprsforecast2)

hwplot2

 

# the sum of squared errors can be viewed by typing:
atmprsforecast1$SSE
atmprsforecast2$SSE
#by default HoltWinters() function gives a forecast for the time period of the series, for forecasting in future,
#we can use forecast.HoltWinters of “forecast” package

library(forecast)

#forecasting for 100 periods in future… you can use any number you want

atmprsforecast3<- forecast.HoltWinters(atmprsforecast1, h=100)

#viewing the forecated values:

atmprsforecast3

#we can plot the foracastes by typing plot.forecast() function:

plot.forecast(atmprsforecast3)

# The forecast.HoltWinters() function forecasts with 80% and 95% prediction interval, and is shown in blue and gray shade

# for each data point in the series, the errors in forecast are calculated as the observed values minus predicted values

plot3

 

 

Advertisements