Time-series analysis — Part 1

Photo by Jake Hills on Unsplash
# reading the humidity time-series data from kaggle #library(ts_plot)
library(ggplot2)
library(ggfortify)
library(fpp2)
library(forecast)
library(GGally)
data <- read.csv('DailyDelhiClimateTrain.csv')
df <- ts(data, start=c(2013,1,1), frequency=365)
data1 <- read.csv('DailyDelhiClimateTest.csv')
df1 <- ts(data1, start=c(2017,1,1), frequency=365)
## plotting humidity ##autoplot(df[,"humidity"]) +
ggtitle("Humidity") + xlab("Year") +
ylab("Humidity")
Variation of humidity over time (Kaggle data)
Cyclic variation in time-series data
Equation for Average Method
#### naive, seasonal naive, mean method and drift method ##autoplot(df[,'humidity']) +
autolayer(meanf(df[,'humidity'], h=365), series="Mean", PI=FALSE) +
autolayer(naive(df[,'humidity'], h=365), series="Naïve", PI=FALSE) +
autolayer(rwf(df[,'humidity'], drift=TRUE, h=365), series="Drift", PI=FALSE) +
autolayer(snaive(df[,'humidity'], h=365), series="Seasonal naïve", PI=FALSE) +
autolayer(df1[,'humidity'], series="Actual", PI=FALSE) +
ggtitle("Comparison of actual humidity with humidity forecast") + xlab("Year") +
ylab("Humidity") + guides(colour=guide_legend(title="Forecast"))
## linear regression model ##ts.model <- tslm(df[,'humidity'] ~ trend + season)## plotting data forecast ##autoplot(df[,'humidity']) +
autolayer(forecast(ts.model, h = 100), series = "Linear Regression") +
autolayer(df1[,'humidity'], series="Actual", PI=FALSE) +
ggtitle("Comparison of actual humidity with humidity forecast") + xlab("Year") +
ylab("Megalitres") + guides(colour=guide_legend(title="Forecast"))
checkresiduals(ts.model)
Residual analysis of the linear regression model

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store