library(tidyverse)
## -- Attaching packages ---------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1 v purrr 0.3.2
## v tibble 2.1.3 v dplyr 0.8.3
## v tidyr 0.8.3 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## -- Conflicts ------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(mosaic)
## Loading required package: lattice
## Loading required package: ggformula
## Loading required package: ggstance
##
## Attaching package: 'ggstance'
## The following objects are masked from 'package:ggplot2':
##
## geom_errorbarh, GeomErrorbarh
##
## New to ggformula? Try the tutorials:
## learnr::run_tutorial("introduction", package = "ggformula")
## learnr::run_tutorial("refining", package = "ggformula")
## Loading required package: mosaicData
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
## The following object is masked from 'package:tidyr':
##
## expand
## Registered S3 method overwritten by 'mosaic':
## method from
## fortify.SpatialPolygonsDataFrame ggplot2
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected by this.
##
## Note: If you use the Matrix package, be sure to load it BEFORE loading mosaic.
##
## Attaching package: 'mosaic'
## The following object is masked from 'package:Matrix':
##
## mean
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following object is masked from 'package:purrr':
##
## cross
## The following object is masked from 'package:ggplot2':
##
## stat
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cor.test, cov, fivenum, IQR, median,
## prop.test, quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
library(ggformula)
library(readr)
library(fpp2)
## Loading required package: forecast
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## fitted.fracdiff fracdiff
## residuals.fracdiff fracdiff
## Loading required package: fma
## Loading required package: expsmooth
library(vars)
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following objects are masked from 'package:fma':
##
## cement, housing, petrol
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: strucchange
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
##
## Attaching package: 'strucchange'
## The following object is masked from 'package:stringr':
##
## boundary
## Loading required package: urca
## Loading required package: lmtest
##DISCUSSION WEEK 6
## READ IN Ford and GM DATA
uscars <- read_csv("E:/WOODS/ADECXXXX/uscars.csv")
## Parsed with column specification:
## cols(
## Date = col_character(),
## GM = col_double(),
## FORD = col_double()
## )
gmts <- ts(uscars$GM, start = c(2014, 1), frequency = 12)
fts <- ts(uscars$FORD, start = c(2014, 1), frequency = 12)
autoplot(gmts, series = "General Motors", lwd=2) + autolayer(fts, series = "Ford", lwd=2) + xlab("Year") + ylab("Monthly Adjusted Close") + theme_bw()

#How correlated are they anyways?
cor(uscars$GM, uscars$FORD)
## [1] -0.3600055
# Look at selection criteria for number of lags
VARselect(uscars[,2:3], lag.max=12, type="const")[["selection"]]
## AIC(n) HQ(n) SC(n) FPE(n)
## 3 1 1 3
#SC says 1 lag; AIC says 3 ... so I checked 1, 2, and 3! All have high p-values, so none can be rejected. Therefore I will stick with 1 lag.
var1 <- VAR(uscars[,2:3], p=1, type="const")
serial.test(var1, lags.pt = 10, type = "PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object var1
## Chi-squared = 25.372, df = 36, p-value = 0.9069
var2 <- VAR(uscars[,2:3], p=2, type="const")
serial.test(var2, lags.pt = 10, type = "PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object var2
## Chi-squared = 24.568, df = 32, p-value = 0.8231
var3 <- VAR(uscars[,2:3], p=3, type="const")
serial.test(var3, lags.pt = 10, type = "PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object var3
## Chi-squared = 17.553, df = 28, p-value = 0.9368
# VAR model
varmodel <- VAR(uscars[,2:3], p=1, type="both")
summary(varmodel)
##
## VAR Estimation Results:
## =========================
## Endogenous variables: GM, FORD
## Deterministic variables: both
## Sample size: 59
## Log Likelihood: -152.619
## Roots of the characteristic polynomial:
## 0.7436 0.4827
## Call:
## VAR(y = uscars[, 2:3], p = 1, type = "both")
##
##
## Estimation results for equation GM:
## ===================================
## GM = GM.l1 + FORD.l1 + const + trend
##
## Estimate Std. Error t value Pr(>|t|)
## GM.l1 0.76001 0.11173 6.802 7.95e-09 ***
## FORD.l1 -0.13699 0.50401 -0.272 0.787
## const 7.56931 5.01436 1.510 0.137
## trend 0.03965 0.04295 0.923 0.360
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 1.962 on 55 degrees of freedom
## Multiple R-Squared: 0.81, Adjusted R-squared: 0.7996
## F-statistic: 78.14 on 3 and 55 DF, p-value: < 2.2e-16
##
##
## Estimation results for equation FORD:
## =====================================
## FORD = GM.l1 + FORD.l1 + const + trend
##
## Estimate Std. Error t value Pr(>|t|)
## GM.l1 0.03318 0.03229 1.028 0.308559
## FORD.l1 0.46630 0.14564 3.202 0.002272 **
## const 5.69538 1.44897 3.931 0.000239 ***
## trend -0.03663 0.01241 -2.952 0.004639 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.5669 on 55 degrees of freedom
## Multiple R-Squared: 0.7512, Adjusted R-squared: 0.7377
## F-statistic: 55.37 on 3 and 55 DF, p-value: < 2.2e-16
##
##
##
## Covariance matrix of residuals:
## GM FORD
## GM 3.848 0.7350
## FORD 0.735 0.3213
##
## Correlation matrix of residuals:
## GM FORD
## GM 1.0000 0.6609
## FORD 0.6609 1.0000
plot(varmodel)

