EC313 Lecture, Week 8

J James Reade

03/03/2015

Introduction

Mock/Sample and Prep

Preparation

library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols("GBRCPIALLMINMEI",src="FRED",return.class="zoo") # CPI since 1955 (from FRED)
##     As of 0.4-0, 'getSymbols' uses env=parent.frame() and
##  auto.assign=TRUE by default.
## 
##  This  behavior  will be  phased out in 0.5-0  when the call  will
##  default to use auto.assign=FALSE. getOption("getSymbols.env") and 
##  getOptions("getSymbols.auto.assign") are now checked for alternate defaults
## 
##  This message is shown once per session and may be disabled by setting 
##  options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
## [1] "GBRCPIALLMINMEI"
#need inflation not CPI
infl <- diff(log(GBRCPIALLMINMEI),lag=12,differences=1)
getSymbols("LMUNRRTTGBM156S",src="FRED",return.class="zoo") # unemployment since 1955, SA (unfortunately)
## [1] "LMUNRRTTGBM156S"
getSymbols("UKNGDP",src="FRED",return.class="zoo") # nominal GDP (from FRED)
## [1] "UKNGDP"
#need GDP growth not GDP
gdp.g <- diff(log(UKNGDP),lag=4,differences=1)
#combination involves first making monthly data quarterly. We need aggregate() for this.
#a) need first to create variable to aggregate using
infl.yq <- as.numeric(format(index(infl),"%Y"))+0.25*(as.numeric(format(index(infl),"%m"))/12>0.25)+0.25*(as.numeric(format(index(infl),"%m"))/12>0.5)+0.25*(as.numeric(format(index(infl),"%m"))/12>0.75)
LMUNRRTTGBM156S.yq <- as.numeric(format(index(LMUNRRTTGBM156S),"%Y"))+0.25*(as.numeric(format(index(LMUNRRTTGBM156S),"%m"))/12>0.25)+0.25*(as.numeric(format(index(LMUNRRTTGBM156S),"%m"))/12>0.5)+0.25*(as.numeric(format(index(LMUNRRTTGBM156S),"%m"))/12>0.75)
head(index(infl))
## [1] "1956-01-01" "1956-02-01" "1956-03-01" "1956-04-01" "1956-05-01"
## [6] "1956-06-01"
head(format(index(infl),"%Y"))
## [1] "1956" "1956" "1956" "1956" "1956" "1956"
head(as.numeric(format(index(infl),"%Y")))
## [1] 1956 1956 1956 1956 1956 1956
head(0.25*(as.numeric(format(index(infl),"%m"))/12>0.25))
## [1] 0.00 0.00 0.00 0.25 0.25 0.25

Rest of Mock/Sample

  1. Direct download:
    1. Download data on UK unemployment, CPI inflation and GDP growth from the Fred website using the quantmod package.
library(quantmod)
getSymbols("GBRCPIALLMINMEI",src="FRED",return.class="zoo") # CPI since 1955 (from FRED)
## [1] "GBRCPIALLMINMEI"
#need inflation not CPI
infl <- diff(log(GBRCPIALLMINMEI),lag=12,differences=1)
getSymbols("LMUNRRTTGBM156S",src="FRED",return.class="zoo") # unemployment since 1955, SA (unfortunately)
## [1] "LMUNRRTTGBM156S"
getSymbols("UKNGDP",src="FRED",return.class="zoo") # nominal GDP (from FRED)
## [1] "UKNGDP"
#need GDP growth not GDP
gdp.g <- diff(log(UKNGDP),lag=4,differences=1)
b. Combine the data into a data frame, and create lags of inflation and GDP growth.
#combination involves first making monthly data quarterly. We need aggregate() for this.
#a) need first to create variable to aggregate using
infl.yq <- as.numeric(format(index(infl),"%Y"))+0.25*(as.numeric(format(index(infl),"%m"))/12>0.25)+0.25*(as.numeric(format(index(infl),"%m"))/12>0.5)+0.25*(as.numeric(format(index(infl),"%m"))/12>0.75)
LMUNRRTTGBM156S.yq <- as.numeric(format(index(LMUNRRTTGBM156S),"%Y"))+0.25*(as.numeric(format(index(LMUNRRTTGBM156S),"%m"))/12>0.25)+0.25*(as.numeric(format(index(LMUNRRTTGBM156S),"%m"))/12>0.5)+0.25*(as.numeric(format(index(LMUNRRTTGBM156S),"%m"))/12>0.75)
#b) next to aggregate it taking mean value for month
infl.q <- aggregate(infl,by=list(infl.yq),FUN=mean,freq=4)
unr.q <- aggregate(LMUNRRTTGBM156S,by=list(LMUNRRTTGBM156S.yq),FUN=mean)
data.f <- data.frame("date"=seq(1956,2014.75,0.25),
                     "gdp.g"=as.numeric(coredata(gdp.g)),"gdp.g.1"=as.numeric(lag(gdp.g,k=-1,na.pad=T)),
                     "unr"=unr.q[5:NROW(unr.q)],"unr.1"=as.numeric(lag(unr.q[5:NROW(unr.q)],k=-1,na.pad=T)),
                     "infl"=infl.q,"infl.1"=as.numeric(lag(infl.q,k=-1,na.pad=T)))
str(data.f)
## 'data.frame':    236 obs. of  7 variables:
##  $ date   : num  1956 1956 1956 1957 1957 ...
##  $ gdp.g  : num  0.0833 0.092 0.0729 0.0787 0.0551 ...
##  $ gdp.g.1: num  NA 0.0833 0.092 0.0729 0.0787 ...
##  $ unr    : num  1.07 1.1 1.2 1.23 1.47 ...
##  $ unr.1  : num  NA 1.07 1.1 1.2 1.23 ...
##  $ infl   : num  0.0539 0.0635 0.0456 0.032 0.0375 ...
##  $ infl.1 : num  NA 0.0539 0.0635 0.0456 0.032 ...
summary(data.f)
##       date          gdp.g             gdp.g.1              unr        
##  Min.   :1956   Min.   :-0.04316   Min.   :-0.04316   Min.   : 1.067  
##  1st Qu.:1971   1st Qu.: 0.04824   1st Qu.: 0.04830   1st Qu.: 2.400  
##  Median :1985   Median : 0.06452   Median : 0.06456   Median : 3.550  
##  Mean   :1985   Mean   : 0.07677   Mean   : 0.07694   Mean   : 4.452  
##  3rd Qu.:2000   3rd Qu.: 0.09763   3rd Qu.: 0.09777   3rd Qu.: 5.917  
##  Max.   :2015   Max.   : 0.23333   Max.   : 0.23333   Max.   :10.567  
##                                    NA's   :1                          
##      unr.1             infl               infl.1         
##  Min.   : 1.067   Min.   :-0.004542   Min.   :-0.004542  
##  1st Qu.: 2.400   1st Qu.: 0.020723   1st Qu.: 0.020732  
##  Median : 3.600   Median : 0.037324   Median : 0.037385  
##  Mean   : 4.460   Mean   : 0.050378   Mean   : 0.050554  
##  3rd Qu.: 5.933   3rd Qu.: 0.060556   3rd Qu.: 0.060746  
##  Max.   :10.567   Max.   : 0.235589   Max.   : 0.235589  
##  NA's   :1                            NA's   :1
c. Using either `arx` or `lm`, run a regression of unemployment on at least one lag of unemployment, inflation and GDP growth (with lags).
#using lm (although arx probably better given part d)
lm.reg <- lm(unr ~ unr.1 + gdp.g + gdp.g.1 + infl + infl.1, data=data.f)
summary(lm.reg)
## 
## Call:
## lm(formula = unr ~ unr.1 + gdp.g + gdp.g.1 + infl + infl.1, data = data.f)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.68635 -0.11649  0.00906  0.11946  0.57683 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.106929   0.032274   3.313  0.00107 ** 
## unr.1        0.992317   0.004388 226.151  < 2e-16 ***
## gdp.g       -4.307051   0.726983  -5.925 1.14e-08 ***
## gdp.g.1     -0.917721   0.760156  -1.207  0.22857    
## infl         1.411607   1.206499   1.170  0.24322    
## infl.1       5.232749   1.143129   4.578 7.72e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.184 on 229 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.9957, Adjusted R-squared:  0.9956 
## F-statistic: 1.053e+04 on 5 and 229 DF,  p-value: < 2.2e-16
step(lm.reg)
## Start:  AIC=-789.74
## unr ~ unr.1 + gdp.g + gdp.g.1 + infl + infl.1
## 
##           Df Sum of Sq     RSS     AIC
## - infl     1      0.05    7.80 -790.34
## - gdp.g.1  1      0.05    7.80 -790.25
## <none>                    7.75 -789.74
## - infl.1   1      0.71    8.46 -771.17
## - gdp.g    1      1.19    8.94 -758.23
## - unr.1    1   1731.24 1738.99  480.35
## 
## Step:  AIC=-790.34
## unr ~ unr.1 + gdp.g + gdp.g.1 + infl.1
## 
##           Df Sum of Sq     RSS     AIC
## - gdp.g.1  1      0.05    7.84 -790.96
## <none>                    7.80 -790.34
## - gdp.g    1      1.14    8.94 -760.23
## - infl.1   1      6.86   14.66 -644.08
## - unr.1    1   1765.78 1773.58  482.97
## 
## Step:  AIC=-790.96
## unr ~ unr.1 + gdp.g + infl.1
## 
##          Df Sum of Sq     RSS     AIC
## <none>                   7.84 -790.96
## - gdp.g   1      4.26   12.10 -691.05
## - infl.1  1      7.43   15.27 -636.35
## - unr.1   1   1766.75 1774.60  481.11
## 
## Call:
## lm(formula = unr ~ unr.1 + gdp.g + infl.1, data = data.f)
## 
## Coefficients:
## (Intercept)        unr.1        gdp.g       infl.1  
##     0.09693      0.99167     -4.81527      6.26879
library(gets)
arx.reg <- arx(data.f$unr,ar=1:5,mc=T,mxreg=zooreg(data.f[-c(1,grep("unr",colnames(data.f)))]))

arx.reg
## 
## Date: Tue Mar  3 15:56:26 2015 
## Method: Ordinary Least Squares (OLS)
## Variance-Covariance: Ordinary 
## No. of observations (mean eq.): 231 
## Sample: 6 to 236 
## 
## Mean equation:
## 
##                coef  std.error     t-stat      p-value
## mconst   0.03207239 0.02298112  1.3955975 1.642364e-01
## ar1      1.75799515 0.06757266 26.0163658 3.282717e-69
## ar2     -0.75049474 0.13410507 -5.5963189 6.448207e-08
## ar3     -0.07472538 0.14246402 -0.5245210 6.004419e-01
## ar4      0.02827777 0.13226730  0.2137926 8.309059e-01
## ar5      0.03350361 0.06203351  0.5400889 5.896796e-01
## gdp.g   -2.34346378 0.52278800 -4.4826273 1.183842e-05
## gdp.g.1  0.92148283 0.54815307  1.6810684 9.416230e-02
## infl     2.20169989 0.84402616  2.6085683 9.712584e-03
## infl.1  -0.16826634 0.86921860 -0.1935835 8.466799e-01
## 
## Diagnostics:
## 
##                        Chi-sq df      p-value
## Ljung-Box AR(6)     3.4144913  6 7.553093e-01
## Ljung-Box ARCH(1)  33.9914011  1 5.535617e-09
## Jarque-Bera       163.1973520  2 0.000000e+00
## R-squared           0.9979854 NA           NA
d. Run model selection on this, and outlier/break detection methods.
isat.reg <- isat(data.f$unr,ar=1:5,mc=T,mxreg=zooreg(data.f[-c(1,grep("unr",colnames(data.f)))]),sis=T)
## 
## IIS block 1 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## IIS block 2 of 8...
## 
## Searching path no. 1 out of 27
## Searching path no. 2 out of 27
## Searching path no. 3 out of 27
## Searching path no. 4 out of 27
## Searching path no. 5 out of 27
## Searching path no. 6 out of 27
## Searching path no. 7 out of 27
## Searching path no. 8 out of 27
## Searching path no. 9 out of 27
## Searching path no. 10 out of 27
## Searching path no. 11 out of 27
## Searching path no. 12 out of 27
## Searching path no. 13 out of 27
## Searching path no. 14 out of 27
## Searching path no. 15 out of 27
## Searching path no. 16 out of 27
## Searching path no. 17 out of 27
## Searching path no. 18 out of 27
## Searching path no. 19 out of 27
## Searching path no. 20 out of 27
## Searching path no. 21 out of 27
## Searching path no. 22 out of 27
## Searching path no. 23 out of 27
## Searching path no. 24 out of 27
## Searching path no. 25 out of 27
## Searching path no. 26 out of 27
## Searching path no. 27 out of 27
## 
## IIS block 3 of 8...
## 
## Searching path no. 1 out of 27
## Searching path no. 2 out of 27
## Searching path no. 3 out of 27
## Searching path no. 4 out of 27
## Searching path no. 5 out of 27
## Searching path no. 6 out of 27
## Searching path no. 7 out of 27
## Searching path no. 8 out of 27
## Searching path no. 9 out of 27
## Searching path no. 10 out of 27
## Searching path no. 11 out of 27
## Searching path no. 12 out of 27
## Searching path no. 13 out of 27
## Searching path no. 14 out of 27
## Searching path no. 15 out of 27
## Searching path no. 16 out of 27
## Searching path no. 17 out of 27
## Searching path no. 18 out of 27
## Searching path no. 19 out of 27
## Searching path no. 20 out of 27
## Searching path no. 21 out of 27
## Searching path no. 22 out of 27
## Searching path no. 23 out of 27
## Searching path no. 24 out of 27
## Searching path no. 25 out of 27
## Searching path no. 26 out of 27
## Searching path no. 27 out of 27
## 
## IIS block 4 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## IIS block 5 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## IIS block 6 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## IIS block 7 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## IIS block 8 of 8...
## 
## Searching path no. 1 out of 35
## Searching path no. 2 out of 35
## Searching path no. 3 out of 35
## Searching path no. 4 out of 35
## Searching path no. 5 out of 35
## Searching path no. 6 out of 35
## Searching path no. 7 out of 35
## Searching path no. 8 out of 35
## Searching path no. 9 out of 35
## Searching path no. 10 out of 35
## Searching path no. 11 out of 35
## Searching path no. 12 out of 35
## Searching path no. 13 out of 35
## Searching path no. 14 out of 35
## Searching path no. 15 out of 35
## Searching path no. 16 out of 35
## Searching path no. 17 out of 35
## Searching path no. 18 out of 35
## Searching path no. 19 out of 35
## Searching path no. 20 out of 35
## Searching path no. 21 out of 35
## Searching path no. 22 out of 35
## Searching path no. 23 out of 35
## Searching path no. 24 out of 35
## Searching path no. 25 out of 35
## Searching path no. 26 out of 35
## Searching path no. 27 out of 35
## Searching path no. 28 out of 35
## Searching path no. 29 out of 35
## Searching path no. 30 out of 35
## Searching path no. 31 out of 35
## Searching path no. 32 out of 35
## Searching path no. 33 out of 35
## Searching path no. 34 out of 35
## Searching path no. 35 out of 35
## 
## GETS of union of retained IIS indicators... 
## 
## 
## SIS block 1 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## SIS block 2 of 8...
## 
## Searching path no. 1 out of 26
## Searching path no. 2 out of 26
## Searching path no. 3 out of 26
## Searching path no. 4 out of 26
## Searching path no. 5 out of 26
## Searching path no. 6 out of 26
## Searching path no. 7 out of 26
## Searching path no. 8 out of 26
## Searching path no. 9 out of 26
## Searching path no. 10 out of 26
## Searching path no. 11 out of 26
## Searching path no. 12 out of 26
## Searching path no. 13 out of 26
## Searching path no. 14 out of 26
## Searching path no. 15 out of 26
## Searching path no. 16 out of 26
## Searching path no. 17 out of 26
## Searching path no. 18 out of 26
## Searching path no. 19 out of 26
## Searching path no. 20 out of 26
## Searching path no. 21 out of 26
## Searching path no. 22 out of 26
## Searching path no. 23 out of 26
## Searching path no. 24 out of 26
## Searching path no. 25 out of 26
## Searching path no. 26 out of 26
## 
## SIS block 3 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## SIS block 4 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## SIS block 5 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## SIS block 6 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## SIS block 7 of 8...
## 
## Searching path no. 1 out of 28
## Searching path no. 2 out of 28
## Searching path no. 3 out of 28
## Searching path no. 4 out of 28
## Searching path no. 5 out of 28
## Searching path no. 6 out of 28
## Searching path no. 7 out of 28
## Searching path no. 8 out of 28
## Searching path no. 9 out of 28
## Searching path no. 10 out of 28
## Searching path no. 11 out of 28
## Searching path no. 12 out of 28
## Searching path no. 13 out of 28
## Searching path no. 14 out of 28
## Searching path no. 15 out of 28
## Searching path no. 16 out of 28
## Searching path no. 17 out of 28
## Searching path no. 18 out of 28
## Searching path no. 19 out of 28
## Searching path no. 20 out of 28
## Searching path no. 21 out of 28
## Searching path no. 22 out of 28
## Searching path no. 23 out of 28
## Searching path no. 24 out of 28
## Searching path no. 25 out of 28
## Searching path no. 26 out of 28
## Searching path no. 27 out of 28
## Searching path no. 28 out of 28
## 
## SIS block 8 of 8...
## 
## Searching path no. 1 out of 34
## Searching path no. 2 out of 34
## Searching path no. 3 out of 34
## Searching path no. 4 out of 34
## Searching path no. 5 out of 34
## Searching path no. 6 out of 34
## Searching path no. 7 out of 34
## Searching path no. 8 out of 34
## Searching path no. 9 out of 34
## Searching path no. 10 out of 34
## Searching path no. 11 out of 34
## Searching path no. 12 out of 34
## Searching path no. 13 out of 34
## Searching path no. 14 out of 34
## Searching path no. 15 out of 34
## Searching path no. 16 out of 34
## Searching path no. 17 out of 34
## Searching path no. 18 out of 34
## Searching path no. 19 out of 34
## Searching path no. 20 out of 34
## Searching path no. 21 out of 34
## Searching path no. 22 out of 34
## Searching path no. 23 out of 34
## Searching path no. 24 out of 34
## Searching path no. 25 out of 34
## Searching path no. 26 out of 34
## Searching path no. 27 out of 34
## Searching path no. 28 out of 34
## Searching path no. 29 out of 34
## Searching path no. 30 out of 34
## Searching path no. 31 out of 34
## Searching path no. 32 out of 34
## Searching path no. 33 out of 34
## Searching path no. 34 out of 34
## 
## GETS of union of retained SIS indicators... 
## 
## 
## GETS of union of ALL retained indicators...
## 
## regressor-matrix is column rank deficient, so dropping 2 regressors
## 
## Searching path no. 1 out of 1

isat.reg
## 
## Date: Tue Mar  3 15:56:35 2015 
## Method: Ordinary Least Squares (OLS)
## Variance-Covariance: Ordinary 
## No. of observations (mean eq.): 231 
## Sample (mean eq.): 6 to 236 
## 
## GUM mean equation:
## 
##         reg.no keep        coef  std.error     t-stat      p-value
## mconst       1    1  0.04680773 0.03151855  1.4850851 1.389997e-01
## ar1          2    1  1.72291164 0.06145632 28.0347332 2.041552e-73
## ar2          3    1 -0.77807254 0.11402720 -6.8235697 9.066915e-11
## ar3          4    1 -0.01008055 0.11508134 -0.0875950 9.302809e-01
## ar4          5    1  0.06898956 0.10689380  0.6454028 5.193608e-01
## ar5          6    1 -0.01888275 0.05091644 -0.3708575 7.111122e-01
## gdp.g        7    1 -2.16653144 0.43890423 -4.9362283 1.605776e-06
## gdp.g.1      8    1  0.77028368 0.46129283  1.6698367 9.642060e-02
## infl         9    1  1.62616814 0.69250193  2.3482507 1.977811e-02
## infl.1      10    1  0.84141267 0.72688642  1.1575573 2.483411e-01
## iis61       11    0 -0.48505428 0.10524655 -4.6087429 6.975714e-06
## iis62       12    0  0.68688562 0.10997376  6.2459045 2.254956e-09
## sis61       13    0 -0.11237633 0.04036519 -2.7839907 5.852637e-03
## sis90       14    0  0.16238079 0.03708494  4.3786180 1.872054e-05
## sis139      15    0  0.19520422 0.05712475  3.4171566 7.579053e-04
## sis143      16    0 -0.20591040 0.05912908 -3.4823879 6.029509e-04
## sis164      17    0 -0.32407056 0.07749526 -4.1818111 4.224977e-05
## sis166      18    0  0.29198408 0.07664250  3.8096890 1.820456e-04
## 
## Diagnostics:
## 
##                         Chi-sq df    p-value
## Ljung-Box AR(6)   5.510171e+00  6 0.48022828
## Ljung-Box ARCH(1) 1.292075e-05  1 0.99713197
## Jarque-Bera       6.576589e+00  2 0.03731745
## 
## Paths searched: 
## 
## path 1 : 13 15 16 18 
## 
## Terminal models: 
## 
## spec 1 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
## spec 2 : 11 12 14 17 1 2 3 4 5 6 7 8 9 10 
## 
##                info(sc)      logl   n  k
## spec 1 (gum):  3.237491 -324.9485 231 18
## spec 2:       -1.355095  194.6104 231 14
## 
## SPECIFIC mean equation:
## 
##                coef  std.error     t-stat      p-value
## mconst   0.09212236 0.02903453  3.1728546 1.728205e-03
## ar1      1.78066822 0.06376966 27.9234382 8.845949e-74
## ar2     -0.86172407 0.11936618 -7.2191642 8.693162e-12
## ar3     -0.01880892 0.12173925 -0.1545017 8.773578e-01
## ar4      0.09572712 0.11282481  0.8484580 3.971179e-01
## ar5     -0.02458151 0.05355460 -0.4589990 6.466943e-01
## gdp.g   -2.27176046 0.45541408 -4.9883404 1.246815e-06
## gdp.g.1  0.68913367 0.47868962  1.4396253 1.514145e-01
## infl     1.58022525 0.72675398  2.1743606 3.075956e-02
## infl.1   0.33304251 0.75795676  0.4393951 6.608125e-01
## iis61   -0.54309683 0.10896314 -4.9842254 1.270850e-06
## iis62    0.66515364 0.11426592  5.8211028 2.083935e-08
## sis90    0.16815372 0.03596532  4.6754404 5.152003e-06
## sis164  -0.14067021 0.03259903 -4.3151649 2.423264e-05
## 
## Diagnostics:
## 
##                      Chi-sq df   p-value
## Ljung-Box AR(1)   0.6062682  1 0.4361964
## Ljung-Box ARCH(1) 0.6607086  1 0.4163099
## Jarque-Bera       1.7593833  2 0.4149108
#now want found dummies to insert into original regression (so diagnostic tests pass)
isat.dums <- isat.reg$aux$mX[,grep("is\\d+$",isat.reg$aux$mXnames)]
#lack first five observations so add zeros
isat.dums <- rbind(matrix(0,5,4),isat.dums)
#add to existing dataset
data.f <- cbind(data.f,isat.dums)
arx.reg.2 <- arx(data.f$unr,ar=1:5,mc=T,mxreg=zooreg(data.f[-c(1,grep("unr",colnames(data.f)))]))

arx.reg.2
## 
## Date: Tue Mar  3 15:56:35 2015 
## Method: Ordinary Least Squares (OLS)
## Variance-Covariance: Ordinary 
## No. of observations (mean eq.): 231 
## Sample: 6 to 236 
## 
## Mean equation:
## 
##                coef  std.error     t-stat      p-value
## mconst   0.09212236 0.02903453  3.1728546 1.728205e-03
## ar1      1.78066822 0.06376966 27.9234382 8.845949e-74
## ar2     -0.86172407 0.11936618 -7.2191642 8.693162e-12
## ar3     -0.01880892 0.12173925 -0.1545017 8.773578e-01
## ar4      0.09572712 0.11282481  0.8484580 3.971179e-01
## ar5     -0.02458151 0.05355460 -0.4589990 6.466943e-01
## gdp.g   -2.27176046 0.45541408 -4.9883404 1.246815e-06
## gdp.g.1  0.68913367 0.47868962  1.4396253 1.514145e-01
## infl     1.58022525 0.72675398  2.1743606 3.075956e-02
## infl.1   0.33304251 0.75795676  0.4393951 6.608125e-01
## iis61   -0.54309683 0.10896314 -4.9842254 1.270850e-06
## iis62    0.66515364 0.11426592  5.8211028 2.083935e-08
## sis90    0.16815372 0.03596532  4.6754404 5.152003e-06
## sis164  -0.14067021 0.03259903 -4.3151649 2.423264e-05
## 
## Diagnostics:
## 
##                      Chi-sq df   p-value
## Ljung-Box AR(6)   7.1643797  6 0.3059128
## Ljung-Box ARCH(1) 0.6607086  1 0.4163099
## Jarque-Bera       1.7593833  2 0.4149108
## R-squared         0.9985679 NA        NA
gets.m <- getsm(arx.reg.2)
## Searching path no. 1 out of 5
## Searching path no. 2 out of 5
## Searching path no. 3 out of 5
## Searching path no. 4 out of 5
## Searching path no. 5 out of 5

gets.m
## 
## Date: Tue Mar  3 15:56:35 2015 
## Method: Ordinary Least Squares (OLS)
## Variance-Covariance: Ordinary 
## No. of observations (mean eq.): 231 
## Sample (mean eq.): 6 to 236 
## 
## GUM mean equation:
## 
##         reg.no keep        coef  std.error     t-stat      p-value
## mconst       1    0  0.09212236 0.02903453  3.1728546 1.728205e-03
## ar1          2    0  1.78066822 0.06376966 27.9234382 8.845949e-74
## ar2          3    0 -0.86172407 0.11936618 -7.2191642 8.693162e-12
## ar3          4    0 -0.01880892 0.12173925 -0.1545017 8.773578e-01
## ar4          5    0  0.09572712 0.11282481  0.8484580 3.971179e-01
## ar5          6    0 -0.02458151 0.05355460 -0.4589990 6.466943e-01
## gdp.g        7    0 -2.27176046 0.45541408 -4.9883404 1.246815e-06
## gdp.g.1      8    0  0.68913367 0.47868962  1.4396253 1.514145e-01
## infl         9    0  1.58022525 0.72675398  2.1743606 3.075956e-02
## infl.1      10    0  0.33304251 0.75795676  0.4393951 6.608125e-01
## iis61       11    0 -0.54309683 0.10896314 -4.9842254 1.270850e-06
## iis62       12    0  0.66515364 0.11426592  5.8211028 2.083935e-08
## sis90       13    0  0.16815372 0.03596532  4.6754404 5.152003e-06
## sis164      14    0 -0.14067021 0.03259903 -4.3151649 2.423264e-05
## 
## Diagnostics:
## 
##                      Chi-sq df   p-value
## Ljung-Box AR(6)   7.1643797  6 0.3059128
## Ljung-Box ARCH(1) 0.6607086  1 0.4163099
## Jarque-Bera       1.7593833  2 0.4149108
## 
## Paths searched: 
## 
## path 1 : 4 6 10 8 
## path 2 : 5 10 4 8 6 -6 
## path 3 : 6 4 10 8 
## path 4 : 8 4 6 10 
## path 5 : 10 4 6 8 
## 
## Terminal models: 
## 
## spec 1 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
## spec 2 : 1 2 3 5 7 9 11 12 13 14 
## spec 3 : 1 2 3 7 9 11 12 13 14 6 
## 
##                info(sc)      logl   n  k
## spec 1 (gum):  3.143823 -325.0146 231 14
## spec 2:       -1.437120  193.1994 231 10
## spec 3:       -1.432945  192.7172 231 10
## 
## SPECIFIC mean equation:
## 
##               coef  std.error     t-stat      p-value
## mconst  0.09964363 0.02713925   3.671570 3.023382e-04
## ar1     1.78047769 0.05681561  31.337826 2.861002e-83
## ar2    -0.86370347 0.07636828 -11.309715 1.070079e-23
## ar4     0.05491079 0.02559062   2.145739 3.298397e-02
## gdp.g  -1.78886277 0.34177246  -5.234075 3.849793e-07
## infl    2.06966030 0.33333729   6.208907 2.610012e-09
## iis61  -0.53291663 0.10796571  -4.935980 1.569956e-06
## iis62   0.66787823 0.11324919   5.897422 1.371156e-08
## sis90   0.16805386 0.03447422   4.874769 2.079686e-06
## sis164 -0.14432157 0.03106127  -4.646351 5.803478e-06
## 
## Diagnostics:
## 
##                      Chi-sq df   p-value
## Ljung-Box AR(6)   7.4499114  6 0.2812340
## Ljung-Box ARCH(1) 0.5488519  1 0.4587872
## Jarque-Bera       1.1509321  2 0.5624427
e. Select a training period and evaluate the forecasts your model creates.
data.f.0 <- data.f[1:230,]
data.f.1 <- data.f[231:236,]
library(forecast)
## Loading required package: timeDate
## This is forecast 5.8
forc.mod.0 <- Arima(data.f.0$unr,order=c(4,0,0),xreg=zooreg(data.f.0[-c(1,grep("unr",colnames(data.f.0)))]))
forc.0 <- forecast(forc.mod.0,h=6,xreg=zooreg(data.f.1[-c(1,grep("unr",colnames(data.f.1)))]))
plot(forc.0,include=100)

accuracy(forc.0,data.f.1$unr)
##                        ME      RMSE        MAE          MPE      MAPE
## Training set  0.003834849 0.1175692 0.08979836   0.02280558  2.817975
## Test set     -0.688445035 0.7774792 0.68844503 -22.34160016 22.341600
##                  MASE         ACF1
## Training set 0.497111 -0.004044291
## Test set     3.811134           NA
  1. Manual download:
    1. Download data on David Cameron, Nick Clegg, Nigel Farage and Ed Miliband from Google Trends, and manipulate the data so it can be imported into R.
ldr <- read.csv("/home/readejj/Dropbox/Teaching/Reading/ec313/2015/midterm-code/leaders.csv",stringsAsFactors=F)
ldr$date <- as.Date(substr(ldr$Week,1,10))
#for purposes of simplicity, aggregating to monthly data preferable
ldr$ym <- format(ldr$date,"%Y-%m")
ldr.m <- aggregate(ldr[,2:5],by=list(ldr$ym),FUN=mean)
b. Find the "best" ARIMA model for David Cameron. How well does it forecast?
dave.t <- ts(ldr.m$David.Cameron,start=c(2004,1),freq=12)
dave.t.0 <- window(dave.t,end=c(2014,6))
dave.t.1 <- window(dave.t,start=c(2014,7))
dave.t.auto <- auto.arima(dave.t) #more marks for follownig manual process to get best ARIMA order!
dave.arima.0 <- Arima(dave.t.0,order=c(1,1,1))
dave.arima.f <- forecast(dave.arima.0,h=8)
plot(window(dave.t,start=c(2013,1)))
lines(dave.arima.f$mean,col=2)

c. Find the "best" exponential smoothing model for David Cameron. How well does it forecast?
dave.t.ets <- ets(dave.t)
dave.ets.0 <- ets(dave.t.0,model="ANN")
dave.ets.f <- forecast(dave.ets.0,h=8)
plot(window(dave.t,start=c(2013,1)))
lines(dave.arima.f$mean,col=2)
lines(dave.ets.f$mean,col=3)
legend("topleft",lty=1,col=c(1,2,3),legend=c("Actual","ARIMA","ETS"))

d. Find the "best" time series decomposition model for David Cameron. How well does it forecast?
dave.t.ts <- stl(dave.t,s.window=8,t.window=5)
dave.ts.0 <- stl(dave.t.0,s.window=8,t.window=5)
dave.ts.f <- forecast(dave.ts.0,h=8)
plot(window(dave.t,start=c(2013,1)),
     ylim=range(c(window(dave.t,start=c(2013,1)),dave.arima.f$mean,dave.ets.f$mean,dave.ts.f$mean)))
lines(dave.arima.f$mean,col=2)
lines(dave.ets.f$mean,col=3)
lines(dave.ts.f$mean,col=4)
legend("topleft",lty=1,col=c(1,2,3,4),legend=c("Actual","ARIMA","ETS","TS"))

e. Which model is best, and do any of the other leaders help in forecasting?
ldr.t <- ts(ldr.m[,2:5],start=c(2004,1),freq=12)
ldr.t.0 <- window(ldr.t,end=c(2014,6))
ldr.t.1 <- window(ldr.t,start=c(2014,7))
dave.t.mod <- Arima(ldr.t.0[,1],order=c(1,1,1),xreg=ldr.t.0[,2:4])
dave.t.f <- forecast(dave.t.mod,xreg=ldr.t.1[,2:4])
plot(window(dave.t,start=c(2013,1)),
     ylim=range(c(window(dave.t,start=c(2013,1)),dave.arima.f$mean,
                  dave.ets.f$mean,dave.ts.f$mean,dave.t.f$mean)))
lines(dave.arima.f$mean,col=2)
lines(dave.ets.f$mean,col=3)
lines(dave.ts.f$mean,col=4)
lines(dave.t.f$mean,col=5)
legend("topleft",lty=1,col=c(1,2,3,4,5),legend=c("Actual","ARIMA","ETS","TS","Other"),bty="n")

Answer appears to be “yes”, other leaders help, but bear in mind that this model cheats in the sense that these forecasts are made knowing the popularity of other leaders in a given month. A better test would be to look at lagged popularity of leaders. What this model appears to suggest is that there are likely reasons why politicians get less coverage in particular weeks, for example if some big sporting event takes place distracting news attention away from politicians.