0% found this document useful (0 votes)
71 views

Codes

The document discusses various time series analysis and forecasting techniques including: 1. Creating and plotting basic time series data 2. Applying smoothing techniques like simple moving averages 3. Seasonal decomposition using STL 4. Exponential smoothing state space models 5. Linear regression models with trends and seasonality for forecasting 6. Checking for stationarity and differencing for ARIMA models The key steps shown include preprocessing time series data, fitting various models, comparing forecasts, and diagnosing residuals to select the best fitting model.

Uploaded by

sonakshi garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Codes

The document discusses various time series analysis and forecasting techniques including: 1. Creating and plotting basic time series data 2. Applying smoothing techniques like simple moving averages 3. Seasonal decomposition using STL 4. Exponential smoothing state space models 5. Linear regression models with trends and seasonality for forecasting 6. Checking for stationarity and differencing for ARIMA models The key steps shown include preprocessing time series data, fitting various models, comparing forecasts, and diagnosing residuals to select the best fitting model.

Uploaded by

sonakshi garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

sales <- c(18,33,41,7,34,35,24,25,24,21,25,20,22,31,40,29,25,21,22,54,31,25,26,35)

tsales <- ts(sales, start = c(2003,1),frequency = 12 ) #numerical values have default plot of scatter
plot
plot(tsales)
plot(tsales, xlab= "time", ylab="monthly sales", main= "sales(2003-2004)")
start(tsales)
end(tsales)
frequency(tsales)
tsales.subset <- window(tsales, start = c(2003,5),end = c(2004,6))#subset the series
plot(tsales.subset)

rm(list = ls()) #cleaning of environment


#Ctrl+L to clear the console

library(forecast)
plot(Nile, main = "raw time series")
plot(ma(Nile, 3), main ="Simple Moving Average (k = 3)")
plot(ma(Nile, 7), main ="Simple Moving Average (k = 7)")
plot(ma(Nile, 15), main ="Simple Moving Average (k = 15)")
lim <- c(min(Nile), max(Nile))
par(mfrow = c(2,2))
plot(Nile, main = "raw time series")
plot(ma(Nile, 3), main ="Simple Moving Average (k = 3)", ylim = lim)
plot(ma(Nile, 7), main ="Simple Moving Average (k = 7)", ylim = lim)
plot(ma(Nile, 15), main ="Simple Moving Average (k = 15)", ylim = lim)
par(mfrow = c(1,1))

#seasonal decomposition
par(mfrow = c(2,1))
plot(AirPassengers)
lair <- log(AirPassengers) #to convert from multiplicative to additive
plot(lair,ylab = "logAirPassengers")
fit <- stl(lair, s.window = "period") #used for decomposition into seasonality, trend, irregularity or
error
plot(fit)
fit$time.series
exp(fit$time.series)#anti log
monthplot(AirPassengers, xlab="",ylab="")
seasonplot(AirPassengers, year.labels = "true", main = "")

#simple exponential
plot(nhtemp)
fit <- ets(nhtemp, model = "ANN")
fit
pred <- forecast(fit,1)
pred
plot(pred,xlab = "year", ylab = "temp", main = " ")
accuracy(fit)

#level 3 exponentail sm
fit1 <- ets(lair , model = "AAA")
fit1
accuracy(fit1)
pred1 <- forecast(fit1,5)
plot(pred1, xlab = "year", ylab = "temp", main = "")
fit2 <- ets(lair)
fit2
accuracy(fit2)
accuracy(fit1)

data_input <- read.csv('C:/Users/Shreya/Downloads/soft.csv')


tsales <- ts(data_input$Sales, start = c(1997,1),frequency = 4)
tsales
plot(tsales)
plot(tsales, xlab= "time", ylab="Quarterly sales", main= "sales(1997-2012)")
par(mfrow = c(1,1))
fit <- stl(tsales, s.window = "period")
fit
plot(fit)
lim <- c(min(tsales), max(tsales))
par(mfrow = c(2,2))
plot(tsales, main = "raw time series")
plot(ma(tsales, 3), main ="Simple Moving Average (k = 3)", ylim = lim)
plot(ma(tsales, 7), main ="Simple Moving Average (k = 7)", ylim = lim)
plot(ma(tsales, 15), main ="Simple Moving Average (k = 15)", ylim = lim)

fit <- ets(tsales, model = "ANN")


fit
pred <- forecast(fit,1)
pred
plot(pred,xlab = "year", ylab = "Sales", main = " ")
accuracy(fit)

fit <- ets(tsales, model = "AAN")


fit
pred <- forecast(fit,1)
pred
plot(pred,xlab = "year", ylab = "Sales", main = " ")
accuracy(fit)

fit <- ets(tsales, model = "AAA")


fit
pred <- forecast(fit,1)
pred
plot(pred,xlab = "year", ylab = "Sales", main = " ")
accuracy(fit)
sales <- read.csv("Soft Drink Sales.csv", header = TRUE, stringsAsFactors = TRUE)
head(sales) #first 6 observations
sales_ts <- ts(sales$Sales, start = c(1997, 1), frequency = 4)
sales_ts
plot(sales_ts)
library(forecast)

############ Linear Trend (Additive) ############


fit_lin_trend <- tslm(sales_ts ~ trend) #we use lm for simple regression but for forecasting we
use tslm-time series linear model
pred <- forecast(fit_lin_trend, h=4)
pred
summary(fit_lin_trend)
accuracy(fit_lin_trend) # we will look for RMSE and MAPE the least value in every model applied will
the best model
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values (Linear Trend)")

##########
par(mfrow = c(2,1))
plot(sales_ts)
lines(fit_lin_trend$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_lin_trend$residuals)
par(mfrow = c(1,1))

####### Exponential Trend (Multiplicative) ########


fit_exp_trend <- tslm(sales_ts ~ trend, lambda = 0) #lambda = 0 conversion of Y into log of Y
pred <- forecast(fit_exp_trend, h=4)
pred
accuracy(fit_exp_trend)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values")

##########
par(mfrow = c(2,1))
plot(sales_ts)
lines(fit_exp_trend$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)
plot(fit_exp_trend$residuals)
par(mfrow = c(1,1))
__________________________________________________________________________________
############# Quadratic Trend (Amtrak Data) #############
#setwd("D:/Shivani 2017/LBS classes/Data Science 1/Data
Science_2017/Forecasting/Regression_Forecasting")
#when data is polynomial we sqaure or cube the independent variable
ride <- read.csv("Amtrak data.csv", header = TRUE, stringsAsFactors = TRUE)
head(ride)
ride_ts <- ts(ride$Ridership, start = c(1991,1), end = c(2004, 3), freq = 12)
ride_ts
plot(ride_ts, xlab = "Time", ylab = "Ridership", ylim = c(1300, 2300))
fit_quad_trend <- tslm(ride_ts ~ trend + I(trend^2))

pred <- forecast(fit_quad_trend, h=12)


pred

accuracy(fit_quad_trend)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values")

##########

par(mfrow = c(2,1))
plot(ride_ts)
lines(fit_quad_trend$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)

plot(fit_quad_trend$residuals)

par(mfrow = c(1,1))
##################################

rm(list = ls())

##########################
##Model with seasonality ### seasonality was not included till, now we include seasonality
fit_season <- tslm(ride_ts ~ season)

pred <- forecast(fit_season, h=12)


pred

accuracy(fit_season)

plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values")

######################################

par(mfrow = c(2,1))
plot(ride_ts)
lines(fit_season$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)

plot(fit_season$residuals)

par(mfrow = c(1,1))

##################################
## Model with trend and seasonality ####
fit_season_trend <- tslm(ride_ts ~ trend + I(trend^2) + season)

pred <- forecast(fit_season_trend, h=12)


pred

accuracy(fit_season_trend)

plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values")

#######################################

par(mfrow = c(2,1))
plot(ride_ts)
lines(fit_season_trend$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)

plot(fit_season_trend$residuals)

par(mfrow = c(1,1))

rm(list = ls())

______________________________________________________________________________
#setwd("D:/Shivani 2017/LBS classes/Data Science/Forecasting/Regression_Forecasting")
ride <- read.csv("Amtrak data.csv", header = TRUE, stringsAsFactors = TRUE)
head(ride)
ride_ts <- ts(ride$Ridership, start = c(1991,1), end = c(2004, 3), freq = 12)
ride_ts
plot(ride_ts, xlab = "Time", ylab = "Ridership", ylim = c(1300, 2300))
library(forecast)

#### LINEAR TREND & SEASONALITY ###

fit_tren_sea <- tslm(ride_ts ~ season)


pred <- forecast(fit_tren_sea, h=12)
pred
summary(fit_tren_sea)

accuracy(fit_tren_sea)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values (Linear Trend with
Season)")

##########

par(mfrow = c(2,1))
plot(ride_ts, xlim = c(1990, 2006))
lines(fit_tren_sea$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)

plot(fit_tren_sea$residuals)

par(mfrow = c(1,1))

####### QUADRATIC TREND & SEASONALITY ##########

fit_quad_sea <- tslm(ride_ts ~ trend + I(trend^2) + season)


pred <- forecast(fit_quad_sea, h=12)
pred
summary(fit_quad_sea)
accuracy(fit_quad_sea)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values (Quadratic Trend
with Season)")

##########

par(mfrow = c(2,1))
plot(ride_ts, xlim = c(1990, 2006))
lines(fit_quad_sea$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)

plot(fit_quad_sea$residuals)

par(mfrow = c(1,1))

rm(list = ls())
__________________________________________________________________________________
Revised Partition
#setwd("D:/Shivani 2017/LBS classes/Data Science/Forecasting/Regression_Forecasting")

ride <- read.csv("Amtrak data.csv", header = TRUE, stringsAsFactors = TRUE)

head(ride)

ride_ts <- ts(ride$Ridership, start = c(1991,1), end = c(2004, 3), freq = 12)
ride_ts

plot(ride_ts, xlab = "Time", ylab = "Ridership", ylim = c(1300, 2300))

library(forecast)

#### LINEAR TREND & SEASONALITY ###

fit_tren_sea <- tslm(ride_ts ~ season)


pred <- forecast(fit_tren_sea, h=12)
pred
summary(fit_tren_sea)

accuracy(fit_tren_sea)
plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values (Linear Trend with
Season)")

##########

par(mfrow = c(2,1))
plot(ride_ts, xlim = c(1990, 2006))
lines(fit_tren_sea$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)

plot(fit_tren_sea$residuals)

par(mfrow = c(1,1))

####### QUADRATIC TREND & SEASONALITY ##########

fit_quad_sea <- tslm(ride_ts ~ trend + I(trend^2) + season)


pred <- forecast(fit_quad_sea, h=12)
pred

summary(fit_quad_sea)
accuracy(fit_quad_sea)

plot(pred, xlab = "Time", ylab = "Sales ($)", main = "Actual and forecasted values (Quadratic Trend
with Season)")

##########

par(mfrow = c(2,1))
plot(ride_ts, xlim = c(1990, 2006))
lines(fit_quad_sea$fitted, col = "red", lty = 2)
points(pred$mean, col = "blue", pch = 16)

plot(fit_quad_sea$residuals)

par(mfrow = c(1,1))

rm(list = ls())

--------------------------------------------------------------------------------------------------------------------------------------
ARIMA
install.packages("forecast")

library(forecast)
install.packages("tseries")
library(tseries)

### 1. Ensure that the time series is stationary ###

plot the time series to assess the stationarity


plot(Nile)

# Difference the time series to check for the trend


ndiffs(Nile)

# the series is differenced once (lag = 1 is default) and saved as dNile

dNile <- diff(Nile)


# plot the differenced time series

plot(dNile)

# Apply ADF test to check for stationarity.

adf.test(dNile)
# as now we now we need to difference one timeso we will apply adf to dnile not nile

# ADF test proves that time series is stationary, therefore, proceed further.

### 2. Identifying one or more reasonable models

# possible models are selected based on ACF and PACF plots

par(mfrow = c(2,1))

Acf(dNile)
Pacf(dNile)

par(mfrow = c(1,1))

### 3. Fitting the Models ###

fit <- arima(Nile, order = c(0,1,1))


fit

accuracy(fit)

### 4. Evaluating Model Fit ###

qqnorm(fit$residuals)
qqline(fit$residuals)

Box.test(fit$residuals, type = "Ljung-Box") # box.test value should be >.05 to satisy the test

### 5. Making Forecast ###

forecast(fit, 3)

plot(forecast(fit, 3), xlab = "Year", ylab = "Annual Flow")

####### Automated ARIMA Forecasting ########


fit1 <- auto.arima(Nile)
fit1

forecast(fit1, 3)
accuracy(fit1)

plot(forecast(fit1, 3))
rm(list = ls())

You might also like