## Library Load
rm(list=ls())
library(TSA)
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Setting Path
getwd()
## [1] "D:/RMIT_Lec/Semester 3/Time Series Analysis/Time Series Analysis - Assignment - 2"
setwd("D:/RMIT_Lec/Semester 3/Time Series Analysis/Time Series Analysis - Assignment - 2")
Antarctica <- read.csv("Antarctica.csv")
head(Antarctica)
## Year Mass
## 1 2002 -7.546667
## 2 2003 -115.186364
## 3 2004 -262.302500
## 4 2005 -228.376667
## 5 2006 -129.987500
## 6 2007 -316.476667
## Load Time Series
Antarcticaa<-ts(as.vector(Antarctica$Mass), frequency = 1)
class(Antarcticaa)
## [1] "ts"
## Plotting the data
plot(Antarcticaa,type='o',ylab='Cumulative change in mass (billion metric tons)',xlab = 'Years',main =" Plot for changes in Antarctica's climate from 2002 to 2020 ")
summary(Antarcticaa)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -2539.055 -1785.137 -938.991 -1063.774 -289.390 -7.547
par(mfrow=c(2,1))
acf(Antarcticaa, main ="ACF plot")
pacf(Antarcticaa, main ="PACF plot")
par(mfrow=c(1,1))
# Data for closing Prices of Shares
y = Antarcticaa
# Generating first lag of stocks
x = zlag(Antarcticaa)
# Creating index to get rid of the first NA values and the last 5 missing values in x
index = 2:length(x)
cor(y[index],x[index])
## [1] 0.985486
The correlation of 0.9678333 Y[t] and Y[t-1] appear to be positively connected. As a result, a substantial amount of data can be recovered from previous values (lag 1). By showing the first lag along the x axis and the actual value along the y axis, this correlation can be seen.
plot(y=Antarcticaa,x=zlag(Antarcticaa),ylab='Cumulative change in mass (billion metric tons)', xlab='Years' , main = "Scatter plot for showing autocorrelation between Y[t] and Y[t-1]")
Now navigating towards ARIMA(p,d,q) models which will be used for analyzing the Cumulative change in mass. We will be using model specification tools like ACF-PACF, BIC TABLE, AND EACF. The series has a negative trend which resultant gives us sign of Moving average, where we can see Autoregressive behaviour is visible.
# ACF plot
acf(Antarcticaa, main="ACF plot for Change in Mass")
# PACF plot
pacf(Antarcticaa,main="")
title(main="PACF plot for Change in Mass")
We can see a diminishing pattern in the ACF plot as the lag grows. This demonstrates the series’ nonstationarity. While PACF has a high correlation in the first lag, it begins to fall in the second lag, followed by oscillations. We can conclude that AR(1) is our potential model based on our data.
# Finding he number of significant lags present in dataset
adf.test(Antarcticaa)
##
## Augmented Dickey-Fuller Test
##
## data: Antarcticaa
## Dickey-Fuller = -2.7141, Lag order = 2, p-value = 0.3004
## alternative hypothesis: stationary
We can see that number of significant lags k = 1, we find out that from AIC. Now, we will initially implement ADF test with neither intercept nor time constant.
The series is non-stationary, as we can see from the diagram above. As a result, we’ll use the Box-Cox transformation to make the series stationary.data normal hojaye Power transformation is another name for this transition. Because it only applies to positive numbers, we’ll add a positive constant to any negative values in the Ozone layer data.
# creating a new df by adding constant to all the negative values.
Constant.Antarcticaa = Antarcticaa + abs(min(Antarcticaa))+1
# Box-Cox Transformation
AntarcticaaBoxCox = BoxCox.ar(Constant.Antarcticaa)
## Warning in arima0(x, order = c(i, 0L, 0L), include.mean = demean): possible
## convergence problem: optim gave code = 1
## Warning in arima0(x, order = c(i, 0L, 0L), include.mean = demean): possible
## convergence problem: optim gave code = 1
## Warning in arima0(x, order = c(i, 0L, 0L), include.mean = demean): possible
## convergence problem: optim gave code = 1
## Warning in arima0(x, order = c(i, 0L, 0L), include.mean = demean): possible
## convergence problem: optim gave code = 1
## Warning in arima0(x, order = c(i, 0L, 0L), include.mean = demean): possible
## convergence problem: optim gave code = 1
title(main = 'Log-likelihood vs the values of lambda for Change in Climate')
# Confidence intervals of BOX-COX
AntarcticaaBoxCox$ci
## [1] 0.8 1.4
The lambda value of 1 is included in The CI of BOX-COX transformation, as seen above. As a result, no data transformation is necessary, and we may move directly to differencing the original data.
Differencing is a technique for converting nonstationary data to stationary data. We’ll use differencing, observe the data with a time series plot, then use the ADF test to validate stationarity.
# Differencing of data
DiffAntarcticaa = diff(Antarcticaa)
# Plot of data after the first differencing
plot(DiffAntarcticaa,ylab='Cumulative change in mass (billion metric tons)',xlab='Year',type='o', main = "Change in Climate after the first differencing")
# Finding the number of lags in the data
adf.test(DiffAntarcticaa)
##
## Augmented Dickey-Fuller Test
##
## data: DiffAntarcticaa
## Dickey-Fuller = -2.5664, Lag order = 2, p-value = 0.3566
## alternative hypothesis: stationary
DiffAntarcticaaa = diff(DiffAntarcticaa)
# Plot of data after the second differencing
plot(DiffAntarcticaaa,ylab='Cumulative change in mass (billion metric tons)',xlab='Year',type='o', main = "Change in Climate after the first differencing")
# Finding the number of lags in the data
adf.test(DiffAntarcticaaa)
##
## Augmented Dickey-Fuller Test
##
## data: DiffAntarcticaaa
## Dickey-Fuller = -3.438, Lag order = 2, p-value = 0.0725
## alternative hypothesis: stationary
DiffAntarcticaaaa = diff(DiffAntarcticaaa)
# Plot of data after the third differencing
plot(DiffAntarcticaaaa,ylab='Cumulative change in mass (billion metric tons)',xlab='Year',type='o', main = "Change in Climate after the first differencing")
# Finding the number of lags in the data
adf.test(DiffAntarcticaaaa)
##
## Augmented Dickey-Fuller Test
##
## data: DiffAntarcticaaaa
## Dickey-Fuller = -3.645, Lag order = 2, p-value = 0.04679
## alternative hypothesis: stationary
All of the p-values are less than 0.1, we shall reject the null hypothesis of non-stationarity. As a result, after only the first differencing, we can claim that the ozone data is stationary.
# ACF and PACF plots after differencing
acf(DiffAntarcticaaa, ci.type='ma', main="ACF plot of Cumulative change in climate after differencing")
pacf(DiffAntarcticaaa,main="")
title(main="PACF plot of Cumulative change in climate after differencing")
Significant values lags at 3,7,10 are shown by ACF Plot, while lags at 3,4,6 are shown by PACF plow. The order of difference i.e d=1, because the data becomes stationary after first differencing only. Through above visualizations we can identify the potential ARIMA models as : ARIMA(3,1,3),ARIMA(3,1,2),ARIMA(3,1,1) ARIMA(2,1,3),ARIMA(2,1,2),ARIMA(2,1,1) ARIMA(1,1,3),ARIMA(1,1,2),ARIMA(1,1,1) ARI(3,1) IMA(1,3)
DiffAntarcticaa = diff(DiffAntarcticaaa)
# EACF
eacf(DiffAntarcticaaa, ar.max = 3, ma.max = 3)
## AR/MA
## 0 1 2 3
## 0 o o o o
## 1 x x o o
## 2 o x o o
## 3 o o o o
The above EACF model shows that topleft vertex is p=3 d=3and q=2. Therefore, the potential models are MA(3), MA(4), ARMA(1,3), ARMA(1,4), ARIMA(1,1,3), ARIMA(1,1,4)
# BIC Table
res21 = armasubsets(y=DiffAntarcticaaa,nar=3,nma=3,y.name='Test',ar.method='ols')
## Warning in ar.ols(x, aic = aic, order.max = order.max, na.action = na.action, :
## model order: 9 singularities in the computation of the projection matrix results
## are only valid up to model order 8
## Warning in leaps.setup(x, y, wt = wt, nbest = nbest, nvmax = nvmax, force.in =
## force.in, : 1 linear dependencies found
## Warning in leaps.setup(x, y, wt = wt, nbest = nbest, nvmax = nvmax, force.in =
## force.in, : nvmax reduced to 5
## Warning in leaps.exhaustive(a, really.big): XHAUST returned error code -999
# Plotting
plot(res21)
title(main = 'BIC table of Cumulative Change in Climate', line= 5)
## Conclusion