1
load("USA_data.RData")
attach(USA_data)
2
yo<-ts(YO, start= 1960, frequency=1)
io<-ts(IO, start= 1960, frequency=1)
lnyo<-log(yo)
lnio<-log(io)
ts.plot(lnyo,lnio, col= c("blue", "red")); legend("topleft", legend = c("lnyo", "lnio"), col = c("blue", "red"), lty = c(1,1))

# Not statinary, clear trend, changing mean etc.
dlnyo<-diff(lnyo)
dlnio<-diff(lnio)
ts.plot(dlnyo,dlnio, col= c("blue", "red")); legend("topleft", legend = c("dlnyo", "dlnio"), col = c("blue", "red"), lty = c(1,1))

# They are weak stationary to statinary, there are some cyclical small tends in both, but the mean or variance does not change much.
3
n <- length(lnio)
t <- 1:n
r3<-lm(lnio~t)
ts.plot(resid(r3))

#Resids represent the excess value that is not captured by lnio's correlation with timme. In our case it is likely factors other than inflation, or any other time dependent variable. So basically a time series graph.
#The regression has a deterministic trend as the independent variable increases 1 in every period.
#Trend stationary means that a trend is stationary when a serie is stationary with respect to mean axis.
ny <- length(lnyo)
ty <- 1:n
r3y<-lm(lnyo~t)
ts.plot(resid(r3y))

# Still deterministic, even though, the plots seem very different as the variable is still t.
4
#H0: has a unit root. HA: doesn't have.
library(bootUR)
dflnio <- adf(lnio, deterministics = "trend", max_lag = 0)
dflnio
##
## Two-step ADF test (with trend) on a single time series
##
## data: lnio
## null hypothesis: Series has a unit root
## alternative hypothesis: Series is stationary
##
## estimate largest root statistic p-value
## lnio 0.7457 -3.11 0.1129
# p is 0.11. We cannot reject unit root.
#c)
# Unit root is not rejected, probably stochastic
dflnyo <- adf(lnyo, deterministics = "trend", max_lag = 0)
dflnyo
##
## Two-step ADF test (with trend) on a single time series
##
## data: lnyo
## null hypothesis: Series has a unit root
## alternative hypothesis: Series is stationary
##
## estimate largest root statistic p-value
## lnyo 0.9153 -1.979 0.601
#d) ECM. Taking diff if stochastic. If deterministic, detrending.
#e)
# not rejected, stochastic
5
#a
adflnio <- adf(lnio, deterministics = "trend")
adflnio
##
## Two-step ADF test (with trend) on a single time series
##
## data: lnio
## null hypothesis: Series has a unit root
## alternative hypothesis: Series is stationary
##
## estimate largest root statistic p-value
## lnio 0.7457 -3.11 0.1147
#b
adflnyo <- adf(lnyo, deterministics = "trend")
adflnyo
##
## Two-step ADF test (with trend) on a single time series
##
## data: lnyo
## null hypothesis: Series has a unit root
## alternative hypothesis: Series is stationary
##
## estimate largest root statistic p-value
## lnyo 0.894 -2.435 0.3582
#c
6
#a
unionlnio <- boot_union(lnio)
## Progress: |------------------|
## ********************
unionlnio #p > 0.05, do not reject, stochastic, ln(1)
##
## AWB bootstrap union test on a single time series
##
## data: lnio
## null hypothesis: Series has a unit root
## alternative hypothesis: Series is stationary
##
## estimate largest root statistic p-value
## lnio NA -1.001 0.1206
#bootsrap uses different residual sets everytime as it resamples.
#b
ulnyo <- boot_union(lnyo)
## Progress: |------------------|
## ********************
ulnyo #p > 0.05, do not reject, stochastic, ln(1)
##
## AWB bootstrap union test on a single time series
##
## data: lnyo
## null hypothesis: Series has a unit root
## alternative hypothesis: Series is stationary
##
## estimate largest root statistic p-value
## lnyo NA -0.7879 0.4752
7
ulnio <- boot_union(dlnio)
## Progress: |------------------|
## ********************
ulnio
##
## AWB bootstrap union test on a single time series
##
## data: dlnio
## null hypothesis: Series has a unit root
## alternative hypothesis: Series is stationary
##
## estimate largest root statistic p-value
## dlnio NA -3.076 0
# p=0 reject null, I(0) stationary
# after differencing ln(1) we obtain ln(0) which is stationary unlike ln(1)
ulnyo <- boot_union(dlnyo)
## Progress: |------------------|
## ********************
ulnyo
##
## AWB bootstrap union test on a single time series
##
## data: dlnyo
## null hypothesis: Series has a unit root
## alternative hypothesis: Series is stationary
##
## estimate largest root statistic p-value
## dlnyo NA -2.077 0
8
a) Since all variables are non stationary, it is possible that it is
spurious regression
b) No, even though there is no bias, as they are not stationary, the
t values are wrong. Thus, it cannot be properly interpreted
c) R2 mainly demonstrates strong comovement, which clues us into
spurious regression. It does not reflect true explanotary power.
d) Make variables stationary, using dynamic models, ECM,
detrending
e) We made them stationary and used dynamic models
9
a) No, variables are stationary
b) Yes, no bias or wrong t
c) Not spurious, we can interpret
10
#a) No as the y will depend u as it is an ARDL model and a y from a certain period is independent variable. This will make the peg of 0 to the mean hard to protect. It is not exogenous.
#b)
llnio <- embed(lnio, dimension = 2)
head(llnio)
## [,1] [,2]
## [1,] 13.34799 13.31652
## [2,] 13.44161 13.34799
## [3,] 13.49743 13.44161
## [4,] 13.56089 13.49743
## [5,] 13.65956 13.56089
## [6,] 13.74639 13.65956
llnio0 <- llnio[, 1]
llnio1 <- llnio[, 2]
llnyo<-embed(lnyo, dimension =2)
llnyo0<-llnyo[, 1]
llnyo1<-llnyo[, 2]
ardl11 <- lm(llnio0 ~ llnyo0 + llnyo1 + llnio1)
summary(ardl11)
##
## Call:
## lm(formula = llnio0 ~ llnyo0 + llnyo1 + llnio1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.071637 -0.012508 0.003956 0.012111 0.080946
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.5464 0.2457 -6.294 4.18e-08 ***
## llnyo0 2.9219 0.1681 17.382 < 2e-16 ***
## llnyo1 -2.5986 0.1764 -14.731 < 2e-16 ***
## llnio1 0.7416 0.0517 14.342 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02561 on 59 degrees of freedom
## Multiple R-squared: 0.9984, Adjusted R-squared: 0.9983
## F-statistic: 1.207e+04 on 3 and 59 DF, p-value: < 2.2e-16
B <- coef(ardl11)
(B["llnyo0"] + B["llnyo1"]) / (1 - B["llnio1"]) # = 1.251296
## llnyo0
## 1.251296
#c) theta1 = B1 = 2.9219
#d)
2.9219 - 2.5986 + 0.7416 * 2.9219 #= 2.490181
## [1] 2.490181
#e)
(2.9219 - 2.5986) / (1 - 0.7416) #= 1.251161
## [1] 1.251161
#f)
2.9219 / 0.1681 #= 17.38192, yes it is different than 0
## [1] 17.38192
#g)
nls <- nls(llnio0 ~ beta0 + ((theta2 - beta2) / (1 + beta3)) * llnyo0 +beta2 * llnyo1 + beta3 * llnio1,
start = list(
beta0 = B["(Intercept)"],
beta2 = B["llnyo1"],
beta3 = B["llnio1"],
theta2 = 0))
# any reasonable start, e.g. your 2-year multiplier
summary(nls)
##
## Formula: llnio0 ~ beta0 + ((theta2 - beta2)/(1 + beta3)) * llnyo0 + beta2 *
## llnyo1 + beta3 * llnio1
##
## Parameters:
## Estimate Std. Error t value Pr(>|t|)
## beta0 -1.5464 0.2457 -6.294 4.18e-08 ***
## beta2 -2.5986 0.1764 -14.731 < 2e-16 ***
## beta3 0.7416 0.0517 14.342 < 2e-16 ***
## theta2 2.4902 0.1562 15.947 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02561 on 59 degrees of freedom
##
## Number of iterations to convergence: 2
## Achieved convergence tolerance: 3.211e-07
# p is nearly 0, it is significant, sd is 0.1562
#h)
q <- coef(ardl11)
tht <- (q["llnyo0"] + q["llnyo1"]) / (1 - q["llnio1"])
nlsth <- nls(llnio0 ~ beta0 + (tht * (1 - beta3) - beta2) * llnyo0 + beta2 * llnyo1 + beta3 * llnio1, start = list(
beta0 = B["(Intercept)"],
beta2 = B["llnyo1"],
beta3 = B["llnio1"], tht=tht))
summary(nlsth)
##
## Formula: llnio0 ~ beta0 + (tht * (1 - beta3) - beta2) * llnyo0 + beta2 *
## llnyo1 + beta3 * llnio1
##
## Parameters:
## Estimate Std. Error t value Pr(>|t|)
## beta0.(Intercept) -1.54642 0.24568 -6.294 4.18e-08 ***
## beta2.llnyo1 -2.59857 0.17640 -14.731 < 2e-16 ***
## beta3.llnio1 0.74157 0.05170 14.342 < 2e-16 ***
## tht.llnyo0 1.25130 0.03102 40.342 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02561 on 59 degrees of freedom
##
## Number of iterations to convergence: 0
## Achieved convergence tolerance: 1.182e-07
#yes, p is nearly 0