autoplot(ibmclose) +
ggtitle('Daily closing prices for IBM stock')
ggAcf(ibmclose)+
ggtitle('ACF - Daily closing prices for IBM stock')
ggPacf(ibmclose)+
ggtitle('PACF - Daily closing prices for IBM stock')
timeserieslist = list('usnetelec' = usnetelec, 'usgdp' = usgdp, 'mcopper' = mcopper, 'enplanements' = enplanements, 'visitors' = visitors)
curiter = 1
for(ts in timeserieslist)
{
print(paste('Time Series =', names(timeserieslist)[curiter]))
print(paste('BoxCox transformation lambda = ', BoxCox.lambda(ts)))
tryCatch(
{
print(paste('Order of seasonal differencing = ', nsdiffs(ts)))
}, error = function(e) {
print('Time series is not seasonal. No Seasonal diff is required')
})
print(paste('Order of differencing = ', ndiffs(ts)))
curiter = curiter + 1
}
## [1] "Time Series = usnetelec"
## [1] "BoxCox transformation lambda = 0.516771443964645"
## [1] "Time series is not seasonal. No Seasonal diff is required"
## [1] "Order of differencing = 1"
## [1] "Time Series = usgdp"
## [1] "BoxCox transformation lambda = 0.366352049520934"
## [1] "Order of seasonal differencing = 0"
## [1] "Order of differencing = 2"
## [1] "Time Series = mcopper"
## [1] "BoxCox transformation lambda = 0.191904709003829"
## [1] "Order of seasonal differencing = 0"
## [1] "Order of differencing = 1"
## [1] "Time Series = enplanements"
## [1] "BoxCox transformation lambda = -0.226946111237065"
## [1] "Order of seasonal differencing = 1"
## [1] "Order of differencing = 1"
## [1] "Time Series = visitors"
## [1] "BoxCox transformation lambda = 0.277524910835111"
## [1] "Order of seasonal differencing = 1"
## [1] "Order of differencing = 1"
getTS = function(param = 0.6)
{
seed = 123
y <- ts(numeric(100))
e <- rnorm(100)
for(i in 2:100)
y[i] <- param*y[i-1] + e[i]
return(y)
}
cbind("Theta = 0.6" = getTS(0.6), "Theta = 0" = getTS(0), "Theta = 1" = getTS(1), "Theta = -1" = getTS(-1)) %>%
autoplot(facets=TRUE)
getTS = function(param = 0.6)
{
seed = 123
y <- ts(numeric(100))
forecast.err <- rnorm(100)
rand.err = rnorm(100)
for(i in 2:100)
y[i] <- param * forecast.err[i-1] + rand.err[i]
return(y)
}
cbind("Theta = 0.6" = getTS(0.6), "Theta = 0" = getTS(0), "Theta = 1" = getTS(1), "Theta = -1" = getTS(-1)) %>%
autoplot(facets=TRUE)
getTS = function(alpha = 0.6, theta = 0.6)
{
seed = 123
y <- ts(numeric(100))
e <- rnorm(100)
rand.error = rnorm(100)
for(i in 2:100)
y[i] <- alpha * y[i-1] + theta * e[i] + rand.error[i]
return(y)
}
getTS() %>%
autoplot(facets =TRUE)
## Warning: Ignoring unknown parameters: facets
getTS = function(alpha1 = -0.8, alpha2 = 0.3)
{
seed = 123
y <- ts(numeric(100))
e <- rnorm(100)
for(i in 3:100)
y[i] <- alpha1 * y[i-1] + alpha2 * y[i-2] + e[i]
return(y)
}
getTS() %>%
autoplot(facets =TRUE)
## Warning: Ignoring unknown parameters: facets
autoplot(wmurders)
ndiffs(wmurders)
## [1] 2
ggAcf(diff(diff(wmurders)))
#### ACF plot show strong autocorrelation till lag 1 so parameter q will be 1
ggPacf(diff(diff(wmurders)))
fit = Arima(wmurders, order = c(0,2,1))
checkresiduals(fit)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(0,2,1)
## Q* = 13.04, df = 9, p-value = 0.1608
##
## Model df: 1. Total lags used: 10
forecast(fit, h=3)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2005 2.504087 2.224876 2.783299 2.077070 2.931105
## 2006 2.418792 2.003603 2.833981 1.783815 3.053769
## 2007 2.333496 1.799788 2.867204 1.517260 3.149732
res = tail(residuals(fit),1)
ts = tail(wmurders,3)
yt = (2 * ts[3])−(ts[2])−(0.89 * res)
print(paste('Forecast for yt = ', (yt)))
## [1] "Forecast for yt = 2.50421843262007"
yt1 = (2 * yt)−(ts[3])−(0.89 * 0)
print(paste('Forecast for yt+1 = ',yt1))
## [1] "Forecast for yt+1 = 2.41905386524014"
yt2 = (2 * yt1)−(yt)−(0.89 * 0)
print(paste('Forecast for yt+2 = ',yt2))
## [1] "Forecast for yt+2 = 2.33388929786021"
autoplot(forecast(fit, h=3))
auto.arima(wmurders)
## Series: wmurders
## ARIMA(1,2,1)
##
## Coefficients:
## ar1 ma1
## -0.2434 -0.8261
## s.e. 0.1553 0.1143
##
## sigma^2 estimated as 0.04632: log likelihood=6.44
## AIC=-6.88 AICc=-6.39 BIC=-0.97