rm(list=ls())
source("http://klein.uk/R/myfunctions.R")
setwd("~/Desktop/Cam/MFin Lectures Lent")
ls()
oj <- read.csv("http://klein.uk/R/Lent/oj.csv")
str(oj)
ojfro <- ts(oj$ojfro)
cojfro <- ( (ojfro/lag(ojfro,-1)) -1 )*100
plot(cojfro)
install.packages("tseries")
library(tseries)
adf.test(na.omit(cojfro), k=0)
frz <- ts(oj$frz)
data2b <- ts.union(frz, cojfro)
lm2b <- lm(cojfro ~ frz, data=data2b)
summary(lm2b)
library(sandwich)
?vcovHC
sqrt(diag(vcovHC(lm2b, type="HC0")))
shccm(lm2b)
?vcovHAC
sqrt(diag(vcovHAC(lm2b, weights=weights(lm2b))))
sqrt(diag(vcovHAC(lm2b)))
shaccm(lm2b)
library(dynlm)
dl2c.18 <- dynlm(cojfro ~ L(frz, 0:18))
summary(dl2c.18)
con2 <- sqrt(diag(vcovHAC(dl2c.18)))*qnorm(p=0.975)
names(con2)[2:20] <- paste("lag", 0:18, "")
plot(0:18, dl2c.18$coef[2:20], type="l", col="blue", ylim=c(-.3,.8), xlab="Lag", ylab="Dynamic Multiplier")
abline(h=0)
con <- summary(dl2c.18)$coef[2:20,2]*qnorm(p=0.975)
lines(0:18, dl2c.18$coef[2:20]+con, col="green")
lines(0:18, dl2c.18$coef[2:20]-con, col="green")
lines(0:18, dl2c.18$coef[2:20]+con2[2:20], col="red")
lines(0:18, dl2c.18$coef[2:20]-con2[2:20], col="red")
legend("topright",legend=c("Dynamic multiplier","95% confidence band","95% confidence band with HAC errors"),fill=c("blue","green","red"),bty="n")
dl2c.12 <- dynlm(cojfro ~ L(frz, c(0,1,12)))
summary(dl2c.12)
dl2c.7 <- dynlm(cojfro ~ L(frz, 0:7))
summary(dl2c.7)
dl2c.1 <- dynlm(cojfro ~ L(frz, 0:1))
summary(dl2c.1)
mul <- cumsum(dl2c.18$coef[2:20])
plot(0:18, mul, type="l", col="red", ylim=c(-.4,1.6), xlab="Lag", ylab="Cumulative dynamic multiplier")
abline(h=0)
dl2e <- dynlm(cojfro ~ L(frz, 18) + L(d(frz, 1), 0:17))
summary(dl2e)
mul <- dl2e$coef[c(3:20,2)]
lines(0:18, mul, col="blue")
con <- summary(dl2e)$coef[c(3:20,2),2]*qnorm(p=0.975)
lines(0:18, mul+con, col="green")
lines(0:18, mul-con, col="green")
con2 <- sqrt(diag(vcovHAC(dl2e)))[c(3:20,2)]*qnorm(p=0.975)
lines(0:18, mul+con2, col="red")
lines(0:18, mul-con2, col="red")
legend("topright",legend=c("Cumulative dynamic multiplier","95% confidence band","95% confidence band with HAC errors"),fill=c("blue","green","red"),bty="n")
dl2e <- dynlm(cojfro ~ L(frz, 1) + d(frz, 1))
summary(dl2e)
L.frz <- dl2e$coef[2]
d.frz <- dl2e$coef[3]
plot(c(d.frz, L.frz) ~ c(0,1), type="l", col="blue", ylim=c(.4,.8))
se.L.frz <- summary(dl2e)$coef[2,2]
se.d.frz <- summary(dl2e)$coef[3,2]
lines(c(d.frz + 2*se.d.frz, L.frz + 2*se.L.frz) ~ c(0,1), col="green")
lines(c(d.frz - 2*se.d.frz, L.frz - 2*se.L.frz) ~ c(0,1), col="green")