title: “Homework3-2”
author: “Omar Alenezy”
date: “February 24, 2016”
output: html_document
library("Quandl")
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
uc <- Quandl("FRED/UNRATE", type ="zoo")
str(uc)
## 'zooreg' series from Jan 1948 to Jan 2016
##   Data: num [1:817] 3.4 3.8 4 3.9 3.5 3.6 3.6 3.9 3.8 3.7 ...
##   Index: Class 'yearmon'  num [1:817] 1948 1948 1948 1948 1948 ...
##   Frequency: 12
plot (uc, xlab="Period", ylab="Unemployment")

plot(log(uc))

library(tseries)
adf.test(uc)
## Warning in adf.test(uc): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  uc
## Dickey-Fuller = -4.0267, Lag order = 9, p-value = 0.01
## alternative hypothesis: stationary
duc <- diff(uc)
plot(duc)

library("tseries")
library("forecast")
## Loading required package: timeDate
## This is forecast 6.2
adf.test(uc)
## Warning in adf.test(uc): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  uc
## Dickey-Fuller = -4.0267, Lag order = 9, p-value = 0.01
## alternative hypothesis: stationary
kpss.test(uc, null = "Trend")
## Warning in kpss.test(uc, null = "Trend"): p-value smaller than printed p-
## value
## 
##  KPSS Test for Trend Stationarity
## 
## data:  uc
## KPSS Trend = 0.49797, Truncation lag parameter = 6, p-value = 0.01
adf.test(duc)
## Warning in adf.test(duc): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  duc
## Dickey-Fuller = -8.2434, Lag order = 9, p-value = 0.01
## alternative hypothesis: stationary
kpss.test(duc)
## Warning in kpss.test(duc): p-value greater than printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  duc
## KPSS Level = 0.064885, Truncation lag parameter = 6, p-value = 0.1
ucall <-uc
uc1 <-window(ucall, end=c(2014,12))
## Warning in `<=.default`(all.indexes, end): longer object length is not a
## multiple of shorter object length
uc2 <- window(ucall,start=c(2015,01))
## Warning in `>=.default`(all.indexes, start): longer object length is not a
## multiple of shorter object length
duc<-diff(uc)
sduc<-diff(diff(uc),12)
acf(as.data.frame(duc),type ='correlation',lag =36,main="sample ACF")

acf(as.data.frame(duc),type = 'partial', lag=36, main="simple PACF")

acf(as.data.frame(sduc),type= 'correlation',lag = 36,main="sample ACF")

acf(as.data.frame(sduc),type= 'partial',lag=36, main="simple PACF")

am <- arima(sduc,order=c(2,0,2),seasonal=list(order=c(0,0,2),period=12))
am
## 
## Call:
## arima(x = sduc, order = c(2, 0, 2), seasonal = list(order = c(0, 0, 2), period = 12))
## 
## Coefficients:
##          ar1      ar2      ma1     ma2     sma1    sma2  intercept
##       1.1161  -0.2416  -1.1037  0.4033  -1.2431  0.2431     -4e-04
## s.e.  0.2453   0.2282   0.2343  0.1828   0.0505  0.0472      6e-04
## 
## sigma^2 estimated as 0.03768:  log likelihood = 148.1,  aic = -280.2
tsdiag(am,gof.lag=36)

am.LB <- Box.test(am$residuals, lag=24, type="Ljung")
am.LB
## 
##  Box-Ljung test
## 
## data:  am$residuals
## X-squared = 38.983, df = 24, p-value = 0.02742
par(mfrow=c(1,1), cex=0.75)
am.forecast <- forecast(am, h=25)
plot(am.forecast, xlim=c(2009,2018))
lines(diff(diff(ucall)))