library(AER)
## Loading required package: car
## Loading required package: lmtest
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: survival
data("UKNonDurables")
plot(UKNonDurables)

tsp(UKNonDurables)
## [1] 1955.00 1988.75    4.00
time(UKNonDurables)
##         Qtr1    Qtr2    Qtr3    Qtr4
## 1955 1955.00 1955.25 1955.50 1955.75
## 1956 1956.00 1956.25 1956.50 1956.75
## 1957 1957.00 1957.25 1957.50 1957.75
## 1958 1958.00 1958.25 1958.50 1958.75
## 1959 1959.00 1959.25 1959.50 1959.75
## 1960 1960.00 1960.25 1960.50 1960.75
## 1961 1961.00 1961.25 1961.50 1961.75
## 1962 1962.00 1962.25 1962.50 1962.75
## 1963 1963.00 1963.25 1963.50 1963.75
## 1964 1964.00 1964.25 1964.50 1964.75
## 1965 1965.00 1965.25 1965.50 1965.75
## 1966 1966.00 1966.25 1966.50 1966.75
## 1967 1967.00 1967.25 1967.50 1967.75
## 1968 1968.00 1968.25 1968.50 1968.75
## 1969 1969.00 1969.25 1969.50 1969.75
## 1970 1970.00 1970.25 1970.50 1970.75
## 1971 1971.00 1971.25 1971.50 1971.75
## 1972 1972.00 1972.25 1972.50 1972.75
## 1973 1973.00 1973.25 1973.50 1973.75
## 1974 1974.00 1974.25 1974.50 1974.75
## 1975 1975.00 1975.25 1975.50 1975.75
## 1976 1976.00 1976.25 1976.50 1976.75
## 1977 1977.00 1977.25 1977.50 1977.75
## 1978 1978.00 1978.25 1978.50 1978.75
## 1979 1979.00 1979.25 1979.50 1979.75
## 1980 1980.00 1980.25 1980.50 1980.75
## 1981 1981.00 1981.25 1981.50 1981.75
## 1982 1982.00 1982.25 1982.50 1982.75
## 1983 1983.00 1983.25 1983.50 1983.75
## 1984 1984.00 1984.25 1984.50 1984.75
## 1985 1985.00 1985.25 1985.50 1985.75
## 1986 1986.00 1986.25 1986.50 1986.75
## 1987 1987.00 1987.25 1987.50 1987.75
## 1988 1988.00 1988.25 1988.50 1988.75
window(UKNonDurables, end = c(1956, 4))
##       Qtr1  Qtr2  Qtr3  Qtr4
## 1955 24030 25620 26209 27167
## 1956 24620 25972 26285 27659
data("UKDriverDeaths")
plot(UKDriverDeaths)
lines(filter(UKDriverDeaths, c(1/2, rep(1, 11), 1/2)/12), col = 2)

plot(rollapply(UKDriverDeaths, 12, sd))

set.seed(1234)
x <- filter(rnorm(100), 0.9, method = "recursive")

dd_dec <- decompose(log(UKDriverDeaths))
dd_stl <- stl(log(UKDriverDeaths), s.window = 13)
plot(dd_stl)

plot(dd_dec)

plot(dd_dec$trend, ylab = "trend")
lines(dd_stl$time.series[,"trend"], lty = 2, lwd = 2)

dd_stl$time.series[,"seasonal"]
##               Jan          Feb          Mar          Apr          May
## 1969  0.020857258 -0.080546568 -0.082494920 -0.148892549 -0.039306849
## 1970  0.019647917 -0.081731087 -0.082668514 -0.148720640 -0.040358149
## 1971  0.018437762 -0.082917302 -0.082844686 -0.148548845 -0.041407100
## 1972  0.017503932 -0.084913836 -0.083022098 -0.148052794 -0.043257722
## 1973  0.016549716 -0.086894123 -0.083146631 -0.147444786 -0.044937310
## 1974  0.013198516 -0.093574302 -0.084856058 -0.146434992 -0.048156623
## 1975  0.009201642 -0.100919265 -0.087249378 -0.146108139 -0.052057923
## 1976  0.010400372 -0.107837662 -0.076796288 -0.144873859 -0.062796320
## 1977  0.012524956 -0.113923584 -0.065604099 -0.143018056 -0.073030768
## 1978  0.013150970 -0.118646340 -0.061926115 -0.142167676 -0.074733165
## 1979  0.013519729 -0.123545299 -0.058343280 -0.141358835 -0.076423492
## 1980  0.015377249 -0.127514823 -0.057659521 -0.142278567 -0.074286362
## 1981  0.017452391 -0.131291007 -0.056806702 -0.143063296 -0.072048288
## 1982  0.017706471 -0.132556577 -0.056193580 -0.143344045 -0.071128319
## 1983  0.017860134 -0.133902440 -0.055640628 -0.143666455 -0.070231501
## 1984  0.017490394 -0.135032898 -0.055496100 -0.143900618 -0.069419201
##               Jun          Jul          Aug          Sep          Oct
## 1969 -0.086060311 -0.024216107 -0.027974354 -0.020741394  0.056439124
## 1970 -0.085945038 -0.024797495 -0.028442562 -0.018512081  0.056776558
## 1971 -0.085822934 -0.025367570 -0.028892667 -0.016257876  0.057144700
## 1972 -0.085352895 -0.025988763 -0.029857737 -0.013458404  0.057921046
## 1973 -0.084642885 -0.026301047 -0.030454556 -0.010231340  0.059155686
## 1974 -0.081766458 -0.028478151 -0.032858257  0.001649325  0.060553925
## 1975 -0.079546356 -0.031285916 -0.035839555  0.013005458  0.061508387
## 1976 -0.082903745 -0.039915022 -0.034822355  0.015037994  0.065630689
## 1977 -0.085881147 -0.048288102 -0.033666323  0.017092166  0.069671712
## 1978 -0.089350324 -0.051730635 -0.033827907  0.017677638  0.083232062
## 1979 -0.092780309 -0.055106855 -0.033921328  0.018333122  0.096853122
## 1980 -0.095491703 -0.057790807 -0.035141434  0.022095515  0.103232292
## 1981 -0.098144642 -0.060458793 -0.036389602  0.025785820  0.109500002
## 1982 -0.098870026 -0.061189632 -0.036446435  0.027183591  0.112670109
## 1983 -0.099606211 -0.061918920 -0.036493810  0.028598728  0.115862649
## 1984 -0.100206762 -0.062364308 -0.036475377  0.029971162  0.118616896
##               Nov          Dec
## 1969  0.190171755  0.245157456
## 1970  0.190174562  0.247073779
## 1971  0.190213892  0.249028397
## 1972  0.189794188  0.251960613
## 1973  0.189863480  0.255364610
## 1974  0.188948347  0.262327473
## 1975  0.187670192  0.269045422
## 1976  0.188353942  0.273378190
## 1977  0.188853498  0.277433649
## 1978  0.190354912  0.269959878
## 1979  0.191907733  0.262535002
## 1980  0.192721683  0.257213233
## 1981  0.193384802  0.251712130
## 1982  0.193388018  0.248926514
## 1983  0.193418736  0.246171405
## 1984  0.193508342  0.244021671
dd_dec$seasonal
##               Jan          Feb          Mar          Apr          May
## 1969  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1970  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1971  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1972  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1973  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1974  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1975  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1976  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1977  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1978  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1979  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1980  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1981  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1982  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1983  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
## 1984  0.017380123 -0.109709556 -0.067863203 -0.144705844 -0.058401685
##               Jun          Jul          Aug          Sep          Oct
## 1969 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1970 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1971 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1972 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1973 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1974 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1975 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1976 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1977 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1978 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1979 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1980 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1981 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1982 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1983 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
## 1984 -0.092592037 -0.038760357 -0.029809125  0.003004741  0.083667864
##               Nov          Dec
## 1969  0.189815739  0.247973341
## 1970  0.189815739  0.247973341
## 1971  0.189815739  0.247973341
## 1972  0.189815739  0.247973341
## 1973  0.189815739  0.247973341
## 1974  0.189815739  0.247973341
## 1975  0.189815739  0.247973341
## 1976  0.189815739  0.247973341
## 1977  0.189815739  0.247973341
## 1978  0.189815739  0.247973341
## 1979  0.189815739  0.247973341
## 1980  0.189815739  0.247973341
## 1981  0.189815739  0.247973341
## 1982  0.189815739  0.247973341
## 1983  0.189815739  0.247973341
## 1984  0.189815739  0.247973341
dd_past <- window(UKDriverDeaths, end = c(1982, 12))
dd_hw <- HoltWinters(dd_past)
dd_pred <- predict(dd_hw, n.ahead = 24)
plot(dd_hw, dd_pred, ylim = range(UKDriverDeaths))
lines(UKDriverDeaths)

acf(x)

pacf(x)

ar(x)
## 
## Call:
## ar(x = x)
## 
## Coefficients:
##      1  
## 0.9279  
## 
## Order selected 1  sigma^2 estimated as  1.286
nd <- window(log(UKNonDurables), end = c(1970, 4))
acf(diff(nd), ylim = c(-1, 1))

pacf(diff(nd), ylim = c(-1, 1))

acf(diff(diff(nd, 4)), ylim = c(-1, 1))

pacf(diff(diff(nd, 4)), ylim = c(-1, 1))

nd_pars <- expand.grid(ar = 0:2, diff = 1, ma = 0:2, sar = 0:1, sdiff = 1, sma = 0:1)
nd_aic <- rep(0, nrow(nd_pars))
for(i in seq(along = nd_aic)) nd_aic[i] <- AIC(arima(nd,
   unlist(nd_pars[i, 1:3]), unlist(nd_pars[i, 4:6])), k = log(length(nd)))
nd_pars[which.min(nd_aic),]   
##    ar diff ma sar sdiff sma
## 22  0    1  1   0     1   1
nd_arima <- arima(nd, order = c(0,1,1), seasonal = c(0,1,1))
nd_arima
## 
## Call:
## arima(x = nd, order = c(0, 1, 1), seasonal = c(0, 1, 1))
## 
## Coefficients:
##          ma1     sma1
##       -0.353  -0.5828
## s.e.   0.143   0.1382
## 
## sigma^2 estimated as 9.649e-05:  log likelihood = 188.14,  aic = -370.27
tsdiag(nd_arima)

nd_pred <- predict(nd_arima, n.ahead = 18 * 4)
plot(log(UKNonDurables))
lines(nd_pred$pred, col = 2)

data("PepperPrice")
plot(PepperPrice, plot.type = "single", col = 1:2)
legend("topleft", c("black", "white"), bty = "n",col = 1:2, lty = rep(1,2))

library("tseries")
adf.test(log(PepperPrice[, "white"]))
## 
##  Augmented Dickey-Fuller Test
## 
## data:  log(PepperPrice[, "white"])
## Dickey-Fuller = -1.744, Lag order = 6, p-value = 0.6838
## alternative hypothesis: stationary
adf.test(diff(log(PepperPrice[, "white"])))
## Warning in adf.test(diff(log(PepperPrice[, "white"]))): p-value smaller
## than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(log(PepperPrice[, "white"]))
## Dickey-Fuller = -5.336, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
pp.test(log(PepperPrice[, "white"]), type = "Z(t_alpha)")
## 
##  Phillips-Perron Unit Root Test
## 
## data:  log(PepperPrice[, "white"])
## Dickey-Fuller Z(t_alpha) = -1.6439, Truncation lag parameter = 5,
## p-value = 0.726
## alternative hypothesis: stationary
kpss.test(log(PepperPrice[, "white"]))
## Warning in kpss.test(log(PepperPrice[, "white"])): p-value smaller than
## printed p-value
## 
##  KPSS Test for Level Stationarity
## 
## data:  log(PepperPrice[, "white"])
## KPSS Level = 0.91295, Truncation lag parameter = 3, p-value = 0.01
po.test(log(PepperPrice))
## 
##  Phillips-Ouliaris Cointegration Test
## 
## data:  log(PepperPrice)
## Phillips-Ouliaris demeaned = -24.099, Truncation lag parameter =
## 2, p-value = 0.02404
po.test(log(PepperPrice[,2:1]))
## 
##  Phillips-Ouliaris Cointegration Test
## 
## data:  log(PepperPrice[, 2:1])
## Phillips-Ouliaris demeaned = -22.676, Truncation lag parameter =
## 2, p-value = 0.03354
library("urca")
pepper_jo <- ca.jo(log(PepperPrice), ecdet = "const", type = "trace")
summary(pepper_jo)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 4.931953e-02 1.350807e-02 2.081668e-17
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  3.66  7.52  9.24 12.97
## r = 0  | 17.26 17.85 19.96 24.60
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##            black.l2 white.l2   constant
## black.l2  1.0000000  1.00000   1.000000
## white.l2 -0.8892307 -5.09942   2.280911
## constant -0.5569943 33.02742 -20.032441
## 
## Weights W:
## (This is the loading matrix)
## 
##            black.l2    white.l2      constant
## black.d -0.07472300 0.002453210 -4.958157e-18
## white.d  0.02015611 0.003537005  8.850353e-18
dd <- log(UKDriverDeaths)
dd_dat <- ts.intersect(dd, dd1 = lag(dd, k = -1), dd12 = lag(dd, k = -12))
lm(dd ~ dd1 + dd12, data = dd_dat)
## 
## Call:
## lm(formula = dd ~ dd1 + dd12, data = dd_dat)
## 
## Coefficients:
## (Intercept)          dd1         dd12  
##      0.4205       0.4310       0.5112
library("dynlm")
dynlm(dd ~ L(dd) + L(dd, 12))
## 
## Time series regression with "ts" data:
## Start = 1970(1), End = 1984(12)
## 
## Call:
## dynlm(formula = dd ~ L(dd) + L(dd, 12))
## 
## Coefficients:
## (Intercept)        L(dd)    L(dd, 12)  
##      0.4205       0.4310       0.5112
library(strucchange)
dd_ocus <- efp(dd ~ dd1 + dd12, data = dd_dat, type = "OLS-CUSUM")
sctest(dd_ocus)
## 
##  OLS-based CUSUM test
## 
## data:  dd_ocus
## S0 = 1.4866, p-value = 0.02407
plot(dd_ocus)

dd_fs <- Fstats(dd ~ dd1 + dd12, data = dd_dat, from = 0.1)
plot(dd_fs)

sctest(dd_fs)
## 
##  supF test
## 
## data:  dd_fs
## sup.F = 19.333, p-value = 0.006721
data("GermanM1")
LTW <- dm ~ dy2 + dR + dR1 + dp + m1 + y1 + R1 + season
m1_re <- efp(LTW, data = GermanM1, type = "RE")
plot(m1_re)

dd_bp <- breakpoints(dd ~ dd1 + dd12, data = dd_dat, h = 0.1)
plot(dd_bp)

coef(dd_bp, breaks = 2)
##                    (Intercept)       dd1      dd12
## 1970(1) - 1973(10)    1.457762 0.1173226 0.6944798
## 1973(11) - 1983(1)    1.534214 0.2182144 0.5723300
## 1983(2) - 1984(12)    1.686897 0.5486088 0.2141655
plot(dd)
lines(fitted(dd_bp, breaks = 2), col = 4)
lines(confint(dd_bp, breaks = 2))

dd_struct <- StructTS(log(UKDriverDeaths))
plot(cbind(fitted(dd_struct), residuals(dd_struct)))

data("MarkPound")
mp <- garch(MarkPound, grad = "numerical", trace = FALSE)
summary(mp)
## 
## Call:
## garch(x = MarkPound, grad = "numerical", trace = FALSE)
## 
## Model:
## GARCH(1,1)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -6.797391 -0.537032 -0.002637  0.552327  5.248671 
## 
## Coefficient(s):
##     Estimate  Std. Error  t value Pr(>|t|)    
## a0  0.010867    0.001297    8.376   <2e-16 ***
## a1  0.154604    0.013882   11.137   <2e-16 ***
## b1  0.804420    0.016046   50.133   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Diagnostic Tests:
##  Jarque Bera Test
## 
## data:  Residuals
## X-squared = 1060, df = 2, p-value < 2.2e-16
## 
## 
##  Box-Ljung test
## 
## data:  Squared.Residuals
## X-squared = 2.4776, df = 1, p-value = 0.1155