x<- read.csv(url("http://research.stlouisfed.org/fred2/data/PCECC96.csv"))
## Error in open.connection(file, "rt"): cannot open the connection
plot(x, xlab=" Years(1947-2015) ", main="exp_quarter")
## Error in plot(x, xlab = " Years(1947-2015) ", main = "exp_quarter"): object 'x' not found
###### The purpose of plotting the data is to check whether our variable is stationary or non-stationary. In this case, the expenditure variable is non-stationary. For this reason we have to run the Augmented Dickey-Fuller Test. But first, we have to take the first difference of the logarithm of the data.
dlx <-diff(log(x[,2]))
## Error in diff(log(x[, 2])): object 'x' not found
plot(x[2:276,1],dlx, xlab="Years(1947-2015) ", main="log(exp_quarter)")
## Error in plot(x[2:276, 1], dlx, xlab = "Years(1947-2015) ", main = "log(exp_quarter)"): object 'x' not found
##### Now the variable has became stationary,so we are able to use the ACF and PACF.
acf(dlx, type="correlation", lag=36, main="Sample ACF")
## Error in as.ts(x): object 'dlx' not found
acf(dlx, type="partial", lag=36, main="Sample PACF")
## Error in stats::pacf(x = dlx, lag.max = 36, main = "Sample PACF"): object 'dlx' not found
###### We have to try the AR(p) models(for p=1,2,3,4. We will check the AR(1), AR(2), AR(3), AR(4).
ar1 <- arima(dlx, order=c(1,0,0))
## Error in NCOL(x): object 'dlx' not found
ar1
## Error in eval(expr, envir, enclos): object 'ar1' not found
ar2 <- arima(dlx, order=c(2,0,0))
## Error in NCOL(x): object 'dlx' not found
ar2
## Error in eval(expr, envir, enclos): object 'ar2' not found
ar3 <- arima(dlx, order=c(3,0,0))
## Error in NCOL(x): object 'dlx' not found
ar3
## Error in eval(expr, envir, enclos): object 'ar3' not found
ar4 <- arima(dlx, order=c(4,0,0))
## Error in NCOL(x): object 'dlx' not found
ar4
## Error in eval(expr, envir, enclos): object 'ar4' not found
###### According to our analysis, the minimum AIC value is comming from AR(4). Then this is the best model if residuals are not correlated.
#Residulas testing using Box test
Box.test(residuals(ar4),lag=36,type="Ljung")
## Error in residuals(ar4): object 'ar4' not found
tsdiag(ar4, gof.lag=36)
## Error in tsdiag(ar4, gof.lag = 36): object 'ar4' not found
###### Residuals are not correlated since p-value is large. In addition, graphically we can see it Best ar model is the AR(4) model.
###### We already have the graphs of ACF and PACF (from previous part).Now, we will test some MA models (MA(1), MA(2), MA(3), MA(4) in order to find "q" by looking at the ACF.
ma1 <- arima(dlx, order=c(0,0,1))
## Error in NCOL(x): object 'dlx' not found
ma1
## Error in eval(expr, envir, enclos): object 'ma1' not found
ma2 <- arima(dlx, order=c(0,0,2))
## Error in NCOL(x): object 'dlx' not found
ma2
## Error in eval(expr, envir, enclos): object 'ma2' not found
ma3 <- arima(dlx, order=c(0,0,3))
## Error in NCOL(x): object 'dlx' not found
ma3
## Error in eval(expr, envir, enclos): object 'ma3' not found
ma4 <- arima(dlx, order=c(0,0,4))
## Error in NCOL(x): object 'dlx' not found
ma4
## Error in eval(expr, envir, enclos): object 'ma4' not found
###### We see that the MA(2) gives the minimum AIC value.
#Test for the correlation of residuals
Box.test(residuals(ma2),lag=12,type="Ljung")
## Error in residuals(ma2): object 'ma2' not found
tsdiag(ma2, gof.lag=12)
## Error in tsdiag(ma2, gof.lag = 12): object 'ma2' not found