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
library("tseries")
#1()Log transformed Retail and Food Services Sales FRED/RSAFS
ret<-Quandl("FRED/RSAFS", type="zoo")
lret<-log(ret)
par(mfrow=c(1,2))
plot(lret,xlab="Time", ylab="lret")

acf(lret,lag=12)

pacf(lret,lag=12)

#Acf shows very slowely decaying.from the plot and acf,we can see that data are nonstationary time series.so we will take difference for the log#
dlret<-diff(lret,1)

plot(dlret,xlab="Time", ylab="dlret")

#we made stationarity by removing time trend#
#now we will Use Zivot-Andrews test to analyze the presence of a unit root and structural breaks#

# Zivot-Andrews Unit Root Test #
library(urca)
dlret.ur.za <- ur.za(dlret, model="intercept") 
summary(dlret.ur.za)
## 
## ################################ 
## # Zivot-Andrews Unit Root Test # 
## ################################ 
## 
## 
## Call:
## lm(formula = testmat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.041348 -0.004687  0.000487  0.005258  0.058832 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.151e-03  1.381e-03   5.903 1.02e-08 ***
## y.l1        -1.418e-01  5.880e-02  -2.411 0.016527 *  
## trend       -4.082e-05  1.107e-05  -3.688 0.000271 ***
## du           6.291e-03  2.027e-03   3.104 0.002100 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.009557 on 283 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.05593,    Adjusted R-squared:  0.04593 
## F-statistic: 5.589 on 3 and 283 DF,  p-value: 0.000976
## 
## 
## Teststatistic: -19.4178 
## Critical values: 0.01= -5.34 0.05= -4.8 0.1= -4.58 
## 
## Potential break point at position: 207
# du capture break level and it is significant, so there is level break#
library(urca)
dlret.ur.za <- ur.za(dlret, model="trend") 
summary(dlret.ur.za)
## 
## ################################ 
## # Zivot-Andrews Unit Root Test # 
## ################################ 
## 
## 
## Call:
## lm(formula = testmat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.042397 -0.004596  0.000906  0.005455  0.058688 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  7.019e-03  1.380e-03   5.087 6.63e-07 ***
## y.l1        -1.207e-01  5.898e-02  -2.047   0.0416 *  
## trend       -2.647e-05  1.053e-05  -2.513   0.0125 *  
## dt           5.651e-05  3.545e-05   1.594   0.1120    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.009675 on 283 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.03247,    Adjusted R-squared:  0.02222 
## F-statistic: 3.166 on 3 and 283 DF,  p-value: 0.02488
## 
## 
## Teststatistic: -19.0013 
## Critical values: 0.01= -4.93 0.05= -4.42 0.1= -4.11 
## 
## Potential break point at position: 201
#dt capture break in trend.here, dt is not significant.so there is not break in trend#
#2()Log transformed Personal consumption expenditures: Nondurable goods (chain-type price index)#
pce<-Quandl("FRED/DNDGRG3M086SBEA", type="zoo")
lpce<-log(pce)
par(mfrow=c(1,2))
plot(lpce,xlab="Time", ylab="lpce")

acf(lpce,lag=12)

pacf(lpce,lag=12)

#Acf shows very slowely decaying.from the plot and acf,we can see that data are nonstationary time series.so we will take difference for the log#
dlpce<-diff(lpce,1)

plot(dlpce,xlab="Time", ylab="dlpce")

#we made stationarity by removing time trend#
#now we will Use Zivot-Andrews test to analyze the presence of a unit root and structural breaks#

# Zivot-Andrews Unit Root Test #
library(urca)
dlpce.ur.za <- ur.za(dlpce, model="intercept") 
summary(dlpce.ur.za)
## 
## ################################ 
## # Zivot-Andrews Unit Root Test # 
## ################################ 
## 
## 
## Call:
## lm(formula = testmat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.039497 -0.002393 -0.000126  0.002315  0.022933 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.541e-03  4.312e-04   3.574 0.000377 ***
## y.l1         4.218e-01  3.478e-02  12.126  < 2e-16 ***
## trend       -6.061e-06  1.501e-06  -4.037 6.03e-05 ***
## du           2.567e-03  6.959e-04   3.688 0.000244 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.005112 on 679 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.2279, Adjusted R-squared:  0.2245 
## F-statistic: 66.82 on 3 and 679 DF,  p-value: < 2.2e-16
## 
## 
## Teststatistic: -16.624 
## Critical values: 0.01= -5.34 0.05= -4.8 0.1= -4.58 
## 
## Potential break point at position: 161
# du capture break level and it is significant, so there is level break#
library(urca)
dlpce.ur.za <- ur.za(dlpce, model="trend") 
summary(dlpce.ur.za)
## 
## ################################ 
## # Zivot-Andrews Unit Root Test # 
## ################################ 
## 
## 
## Call:
## lm(formula = testmat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.039595 -0.002356 -0.000150  0.002173  0.023049 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2.566e-04  7.004e-04  -0.366 0.714205    
## y.l1         4.160e-01  3.492e-02  11.914  < 2e-16 ***
## trend        1.830e-05  5.140e-06   3.559 0.000398 ***
## dt          -2.437e-05  6.078e-06  -4.010 6.74e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.005103 on 679 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.2307, Adjusted R-squared:  0.2273 
## F-statistic: 67.87 on 3 and 679 DF,  p-value: < 2.2e-16
## 
## 
## Teststatistic: -16.7268 
## Critical values: 0.01= -4.93 0.05= -4.42 0.1= -4.11 
## 
## Potential break point at position: 178
#dt capture break in trend.here, dt is not significant.so there is not break in trend#
#3()
ahep<-Quandl("FRED/DNDGRG3M086SBEA", type="zoo")
pcec<-Quandl("FRED/PCECTPI", type="zoo")
rwph<-ahep/pcec
## Warning in merge.zoo(e1, e2, all = FALSE, retclass = NULL): Index vectors
## are of different classes: yearmon yearqtr
lrwph<-log(rwph)
par(mfrow=c(1,2))
plot(lrwph,xlab="Time", ylab="lrwph")

dlrwph<-diff(lrwph,3)
par(mfrow=c(1,2))
plot(dlrwph,xlab="Time", ylab="dlrwph")

#we made stationarity by removing time trend#
#now we will Use Zivot-Andrews test to analyze the presence of a unit root and structural breaks#

# Zivot-Andrews Unit Root Test #
library(urca)
dlrwph.ur.za <- ur.za(dlrwph, model="intercept") 
summary(dlrwph.ur.za)
## 
## ################################ 
## # Zivot-Andrews Unit Root Test # 
## ################################ 
## 
## 
## Call:
## lm(formula = testmat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.064461 -0.003474 -0.000454  0.003796  0.031664 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -1.536e-03  1.287e-03  -1.193    0.234  
## y.l1        -7.722e-02  6.713e-02  -1.150    0.251  
## trend        3.688e-05  1.794e-05   2.056    0.041 *
## du          -5.586e-03  2.403e-03  -2.325    0.021 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.00934 on 222 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.02682,    Adjusted R-squared:  0.01367 
## F-statistic: 2.039 on 3 and 222 DF,  p-value: 0.1093
## 
## 
## Teststatistic: -16.0466 
## Critical values: 0.01= -5.34 0.05= -4.8 0.1= -4.58 
## 
## Potential break point at position: 89
# du capture break level and it is  significant, so there is  level break#
library(urca)
dlrwph.ur.za <- ur.za(dlrwph, model="trend") 
summary(dlrwph.ur.za)
## 
## ################################ 
## # Zivot-Andrews Unit Root Test # 
## ################################ 
## 
## 
## Call:
## lm(formula = testmat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.064536 -0.003444 -0.000455  0.004481  0.031424 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -1.505e-03  1.300e-03  -1.157   0.2484  
## y.l1        -6.795e-02  6.702e-02  -1.014   0.3118  
## trend        9.658e-06  1.043e-05   0.926   0.3554  
## dt          -5.313e-04  2.728e-04  -1.948   0.0527 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.009373 on 222 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.01989,    Adjusted R-squared:  0.00664 
## F-statistic: 1.501 on 3 and 222 DF,  p-value: 0.2151
## 
## 
## Teststatistic: -15.9353 
## Critical values: 0.01= -4.93 0.05= -4.42 0.1= -4.11 
## 
## Potential break point at position: 211
#dt capture break in trend.here, dt is  significant.so there is  break in trend. but du is stronger than  dt. so, we suggest that there is level break.

ح