The time series analysis with log changes in Real Personal Consumption Expenditures.
Time series data for Real Gross Domestic Product.
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
rpce <- Quandl("FRED/PCECC96", type="xts")
summary (rpce)
## Index rpce
## Min. :1947 Min. : 1199
## 1st Qu.:1964 1st Qu.: 2253
## Median :1982 Median : 4064
## Mean :1982 Mean : 5097
## 3rd Qu.:1999 3rd Qu.: 7753
## Max. :2017 Max. :11670
head(rpce,5)
## [,1]
## 1947 Q1 1199.4
## 1947 Q2 1219.3
## 1947 Q3 1223.3
## 1947 Q4 1223.6
## 1948 Q1 1229.8
tail(rpce,5)
## [,1]
## 2015 Q4 11319.3
## 2016 Q1 11365.2
## 2016 Q2 11484.9
## 2016 Q3 11569.0
## 2016 Q4 11669.8
plot(rpce, type= "l",xlab= "year", ylab="real personal consumption expenditures", main="RPCE", major.format="%Y Q%q")
##Stationary Data We make the difference to make our data stationary
drpce <-diff(log(rpce))
plot(drpce, type= "l", xlab= "year", ylab="log change in RPCE", main="Log change in RPCE, Quaterly",major.format="%Y Q%q")
## Data Over Period of Time ACF and PACF helps to understand the relation between data over period of time.
library(forecast)
par(mfrow=c(2,1), cex=1, mar=c(3,4,3,3))
Acf(drpce,type='correlation',lag=280, main="Auto-correlation Function")
Acf(drpce,type='correlation',lag=280, main="Partial Auto-correlation Function")
#AR(p)
ar1 <- arima(drpce, order=c(1,0,0))
ar1
##
## Call:
## arima(x = drpce, order = c(1, 0, 0))
##
## Coefficients:
## ar1 intercept
## 0.0913 0.0082
## s.e. 0.0596 0.0005
##
## sigma^2 estimated as 6.561e-05: log likelihood = 947.75, aic = -1889.5
par(mar = rep(2, 4))
tsdiag(ar1, gof.lag=24)
ar2 <- arima(drpce, order=c(2,0,0))
ar2
##
## Call:
## arima(x = drpce, order = c(2, 0, 0))
##
## Coefficients:
## ar1 ar2 intercept
## 0.0612 0.3189 0.0082
## s.e. 0.0566 0.0566 0.0007
##
## sigma^2 estimated as 5.888e-05: log likelihood = 962.73, aic = -1917.47
par(mar = rep(2, 4))
tsdiag(ar2, gof.lag=24)
ar3 <- arima(drpce, order=c(3,0,0))
ar3
##
## Call:
## arima(x = drpce, order = c(3, 0, 0))
##
## Coefficients:
## ar1 ar2 ar3 intercept
## 0.0561 0.3178 0.0156 0.0082
## s.e. 0.0599 0.0567 0.0599 0.0008
##
## sigma^2 estimated as 5.887e-05: log likelihood = 962.77, aic = -1915.53
par(mar = rep(2, 4))
tsdiag(ar3, gof.lag=280)
ar4 <- arima(drpce, order=c(4,0,0))
ar4
##
## Call:
## arima(x = drpce, order = c(4, 0, 0))
##
## Coefficients:
## ar1 ar2 ar3 ar4 intercept
## 0.0585 0.3648 0.0238 -0.1446 0.0082
## s.e. 0.0593 0.0594 0.0593 0.0592 0.0007
##
## sigma^2 estimated as 5.762e-05: log likelihood = 965.72, aic = -1919.44
par(mar = rep(2, 4))
tsdiag(ar4, gof.lag=24)
ar5 <- arima(drpce, order=c(5,0,0))
ar5
##
## Call:
## arima(x = drpce, order = c(5, 0, 0))
##
## Coefficients:
## ar1 ar2 ar3 ar4 ar5 intercept
## 0.0583 0.3649 0.0244 -0.1445 -0.0013 0.0082
## s.e. 0.0600 0.0594 0.0635 0.0593 0.0599 0.0007
##
## sigma^2 estimated as 5.762e-05: log likelihood = 965.72, aic = -1917.44
par(mar = rep(2, 4))
tsdiag(ar5, gof.lag=280)
BIC(ar1)
## [1] -1878.603
BIC(ar2)
## [1] -1902.94
BIC(ar3)
## [1] -1897.377
BIC(ar4)
## [1] -1897.654
BIC(ar5)
## [1] -1892.023
Box.test(residuals(ar1),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ar1)
## X-squared = 192.54, df = 200, p-value = 0.6346
par(mar = rep(2, 4))
tsdiag(ar1, gof.lag=280)
Box.test(residuals(ar2),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ar2)
## X-squared = 164.34, df = 200, p-value = 0.9691
par(mar = rep(2, 4))
tsdiag(ar2, gof.lag=280)
Box.test(residuals(ar3),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ar3)
## X-squared = 164.54, df = 200, p-value = 0.9683
par(mar = rep(2, 4))
tsdiag(ar3, gof.lag=280)
Box.test(residuals(ar4),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ar4)
## X-squared = 161.55, df = 200, p-value = 0.9787
par(mar = rep(2, 4))
tsdiag(ar4, gof.lag=280)
Box.test(residuals(ar5),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ar5)
## X-squared = 161.57, df = 200, p-value = 0.9786
par(mar = rep(2, 4))
tsdiag(ar5, gof.lag=280)
Among all 5 AR models, AR(4) has the lowest AIC which is -1919.28, but AR(2) has the lowest BIC which is -1902.818,so AR(2) is a good model. Moreover, in Ljung Box all 5 models has p-values higher than 0.6, and AR(2) plot for ACF of residual is zero and p values for Ljung Box is 0.9722. Thus AR(2) is an adequate model.
ma1 <- arima(drpce, order=c(0,0,1))
ma1
##
## Call:
## arima(x = drpce, order = c(0, 0, 1))
##
## Coefficients:
## ma1 intercept
## 0.0559 0.0082
## s.e. 0.0468 0.0005
##
## sigma^2 estimated as 6.582e-05: log likelihood = 947.29, aic = -1888.59
par(mar = rep(2, 4))
tsdiag(ma1, gof.lag=280)
ma2 <- arima(drpce, order=c(0,0,2))
ma2
##
## Call:
## arima(x = drpce, order = c(0, 0, 2))
##
## Coefficients:
## ma1 ma2 intercept
## 0.0278 0.3657 0.0082
## s.e. 0.0564 0.0581 0.0006
##
## sigma^2 estimated as 5.811e-05: log likelihood = 964.54, aic = -1921.09
par(mar = rep(2, 4))
tsdiag(ma2, gof.lag=24)
ma3 <- arima(drpce, order=c(0,0,3))
ma3
##
## Call:
## arima(x = drpce, order = c(0, 0, 3))
##
## Coefficients:
## ma1 ma2 ma3 intercept
## 0.0557 0.3687 0.0708 0.0082
## s.e. 0.0600 0.0575 0.0575 0.0007
##
## sigma^2 estimated as 5.779e-05: log likelihood = 965.3, aic = -1920.6
par(mar = rep(2, 4))
tsdiag(ma3, gof.lag=280)
ma4 <- arima(drpce, order=c(0,0,4))
ma4
##
## Call:
## arima(x = drpce, order = c(0, 0, 4))
##
## Coefficients:
## ma1 ma2 ma3 ma4 intercept
## 0.0556 0.3676 0.0710 -0.0042 0.0082
## s.e. 0.0599 0.0605 0.0575 0.0742 0.0007
##
## sigma^2 estimated as 5.779e-05: log likelihood = 965.3, aic = -1918.61
par(mar = rep(2, 4))
tsdiag(ma4, gof.lag=280)
ma5 <- arima(drpce, order=c(0,0,5))
ma5
##
## Call:
## arima(x = drpce, order = c(0, 0, 5))
##
## Coefficients:
## ma1 ma2 ma3 ma4 ma5 intercept
## 0.0558 0.3672 0.0788 -0.0056 0.0117 0.0082
## s.e. 0.0600 0.0606 0.0743 0.0748 0.0707 0.0007
##
## sigma^2 estimated as 5.778e-05: log likelihood = 965.32, aic = -1916.63
par(mar = rep(2, 4))
tsdiag(ma5, gof.lag=280)
BIC(ma1)
## [1] -1877.693
BIC(ma2)
## [1] -1906.561
BIC(ma3)
## [1] -1902.448
BIC(ma4)
## [1] -1896.82
BIC(ma5)
## [1] -1891.216
Box.test(residuals(ma1),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ma1)
## X-squared = 196.63, df = 200, p-value = 0.5541
par(mar = rep(2, 4))
tsdiag(ma1, gof.lag=280)
Box.test(residuals(ma2),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ma2)
## X-squared = 157.44, df = 200, p-value = 0.9883
par(mar = rep(2, 4))
tsdiag(ma2, gof.lag=280)
Box.test(residuals(ma3),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ma3)
## X-squared = 153.37, df = 200, p-value = 0.9939
par(mar = rep(2, 4))
tsdiag(ma3, gof.lag=280)
Box.test(residuals(ma4),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ma4)
## X-squared = 152.83, df = 200, p-value = 0.9945
par(mar = rep(2, 4))
tsdiag(ma4, gof.lag=280)
Box.test(residuals(ma5),lag=200,type="Ljung")
##
## Box-Ljung test
##
## data: residuals(ma5)
## X-squared = 152.8, df = 200, p-value = 0.9945
par(mar = rep(2, 4))
tsdiag(ma5, gof.lag=280)
Among all 5 MA models, MA(2) has the lowest AIC which is -1920.92, and has the lowest BIC which is -1906.397,so MA(2) is a good model. Moreover, in Ljung Box all 5 models has p-values higher than 0.5, and AR(2) plot for ACF of residual is zero and p values for Ljung Box is 0.9897. Thus MA(2) is an adequate model.