if (!require("fpp2")) install.packages("fpp2")
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("gridExtra")) install.packages("gridExtra")
library(fma)
library(forecast)
library(tseries)Figure 8.31
All three three plots indicate that the data is white noise. This is because none of the spikes are larger than the critical value range for any of the plots
The formula for the critical values is \(\pm 1.96/(\sqrt{T - d})\) where T is the sample size and d is the amount of differencing used. As the sample size increases the critical values get smaller. This explains why the cricial value region gets smaller (from left to right in the plot) as the sample size increases.
ibmclose). Use R to plot the daily closing prices for IBM stock and the ACF and PACF. Explain how each plot shows that the series is non-stationary and should be differenced.There is clearly a trend element throughout the plot. The ACF plot shows that there are significant autocorrelations throughout. Therefore the data should be differenced in order to remove autocorrelation.
usnetelecIt is almost linearly increasing data. It looked like that the data only need first differencing.
##
## Box-Ljung test
##
## data: diff(usnetelec)
## X-squared = 0.8508, df = 1, p-value = 0.3563
first differenced usnetelec data can be thought of as a white noise series
## Warning in kpss.test(diff(usnetelec)): p-value greater than printed p-value
##
## KPSS Test for Level Stationarity
##
## data: diff(usnetelec)
## KPSS Level = 0.15848, Truncation lag parameter = 3, p-value = 0.1
kpss test result also shows that first differencing made the data stationary.
usgdpIt is almost linearly increasing data. It looked like that the data only need first differencing
##
## Box-Ljung test
##
## data: diff(usgdp)
## X-squared = 39.187, df = 1, p-value = 3.85e-10
first differenced usnetelec data cannot be thought of as a white noise series.
There is still a trend left in the differenced data. It looked like one more differencing would be enough, but use ndiffs function to check the number of differencing needed.
## [1] 2
Plot shows that the twice differenced data is like white noise series.
##
## Box-Ljung test
##
## data: diff(diff(usgdp))
## X-squared = 53.294, df = 1, p-value = 2.872e-13
## Warning in kpss.test(diff(diff(usnetelec))): p-value greater than printed p-
## value
##
## KPSS Test for Level Stationarity
##
## data: diff(diff(usnetelec))
## KPSS Level = 0.098532, Truncation lag parameter = 3, p-value = 0.1
But kpss test result shows that differencing twice was enough to make the data stationary. Therefore in usgdp data case, even if twice differencing didn’t make the data like white noise series, it made the data stationary
mcoppermcopper data have increasing trend. And they have bigger variation for bigger prices. Therefore I’ll use Box-Cox transformation before differencing
##
## Box-Ljung test
##
## data: diff(BoxCox(mcopper, lambda_mcopper))
## X-squared = 57.517, df = 1, p-value = 3.353e-14
Plot result looked like BoxCox transformation and first differencing made the data like white noise series. But Ljung-Box test shows that it didn’t.
## Warning in kpss.test(diff(BoxCox(mcopper, lambda_mcopper))): p-value greater
## than printed p-value
##
## KPSS Test for Level Stationarity
##
## data: diff(BoxCox(mcopper, lambda_mcopper))
## KPSS Level = 0.057275, Truncation lag parameter = 6, p-value = 0.1
But kpss test result shows that differencing with Box-Cox transformation was enough to make the data stationary. Even if differencing with Box-Cox transformation didn’t make the data like white noise series, it made the data stationary.
enplanementsenplanements data have seasonality and increasing trend even if the number of enplanements fell in 2001. Therefore, I think that the data need seasonal differencing, too. The variations are bigger for bigger numbers. Therefore I’ll use Box-Cox transformation before differencing
## [1] 1
##
## Box-Ljung test
##
## data: diff_enplane
## X-squared = 16.55, df = 1, p-value = 4.739e-05
Plot result looked like BoxCox transformation and multiple differencings made the data like white noise series. But Ljung-Box test shows that it didn’t.
## Warning in kpss.test(diff_enplane): p-value greater than printed p-value
##
## KPSS Test for Level Stationarity
##
## data: diff_enplane
## KPSS Level = 0.015112, Truncation lag parameter = 5, p-value = 0.1
visitorsvisitors data are similar to enplanements data. They have seasonality and increasing trend. It looked like they also need Box-Cox transformation, first and seasonal differencing.
## [1] 1
##
## Box-Ljung test
##
## data: diff_visit
## X-squared = 21.922, df = 1, p-value = 2.839e-06
similar results like earliear example
## Warning in kpss.test(diff_visit): p-value greater than printed p-value
##
## KPSS Test for Level Stationarity
##
## data: diff_visit
## KPSS Level = 0.051907, Truncation lag parameter = 4, p-value = 0.1
But kpss test result shows that differencings with Box-Cox transformation was enough to make the data stationary. In visitors data case, even if differencings with Box-Cox transformation didn’t make the data like white noise series, it made the data stationary.
library(readxl)
library(seasonal)
retaildata <- readxl::read_excel("C:/Users/patel/Documents/Data_624/retail.xlsx", skip=1)
myts <- ts(retaildata[,"A3349335T"],
frequency=12, start=c(1982,4))
plot(myts)## Warning in kpss.test(diff(BoxCox(myts, BoxCox.lambda(myts)))): p-value greater
## than printed p-value
##
## KPSS Test for Level Stationarity
##
## data: diff(BoxCox(myts, BoxCox.lambda(myts)))
## KPSS Level = 0.010432, Truncation lag parameter = 5, p-value = 0.1
To make retail.ts data stationary, I did Box-Cox transformation, 1 first differencing
autoplot(ar.1(0.006), series = "0.006") +
geom_line(colour = "red") +
autolayer(ar.1(0.06), series = "0.06") +
autolayer(ar.1(0.6), series = "0.6") +
ylab("Data") +
guides(colour = guide_legend(title = "Phi"))par(mfrow=c(1,3))
acf(ar.1(0.006),main='Phi=0.006')
acf(ar.1(0.06),main='Phi=0.06')
acf(ar.1(0.6),main='Phi=0.6')set.seed(17)
ma.1 <- function(theta, sd=1, n=100){
y <- ts(numeric(n))
e <- rnorm(n, sd=sd)
e[1] <- 0
for(i in 2:n)
y[i] <- theta*e[i-1] + e[i]
return(y)
}autoplot(ma.1(0.006), series = "0.006") +
geom_line(colour = "red") +
autolayer(ma.1(0.06), series = "0.06") +
autolayer(ma.1(0.6), series = "0.6") +
ylab("Data") +
guides(colour = guide_legend(title = "Theta"))par(mfrow=c(1,3))
acf(ma.1(0.006),main='Theta=0.006')
acf(ma.1(0.06),main='Theta=0.06')
acf(ma.1(0.6),main='Theta=0.6')y1 <- ts(numeric(100))
e <- rnorm(100, sd=1)
for(i in 2:100)
y1[i] <- 0.6*y1[i-1] + 0.6*e[i-1] + e[i]
plot(y1)y2 <- ts(numeric(100))
e <- rnorm(100, sd=1)
for(i in 3:100)
y2[i] <- -0.8*y2[i-1] + 0.3*y2[i-2] + e[i]
plot(y2)data from an AR(2) model increased with oscillation. They are non-staionary data. But data from an ARMA(1, 1) model were stationary.