#install.packages("quantmod")
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.
library("tidyverse")
## ── Attaching packages ────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.1 ✔ dplyr 0.8.1
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ───────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
#install.packages("fUnitRoots")
library("fUnitRoots")
## Loading required package: timeDate
## Loading required package: timeSeries
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
## Loading required package: fBasics
##
## Attaching package: 'fBasics'
## The following object is masked from 'package:TTR':
##
## volatility
data=read.table("m-unrate-4811.txt", header=T)
gdp=log(data[,1])
head(gdp)
## [1] 1.223775 1.335001 1.386294 1.360977 1.252763 1.280934
m1=ar(diff(gdp), method="mle")
m1$order
## [1] 12
adfTest(gdp,lags=12,type=c("c"))
##
## Title:
## Augmented Dickey-Fuller Test
##
## Test Results:
## PARAMETER:
## Lag Order: 12
## STATISTIC:
## Dickey-Fuller: -2.8398
## P VALUE:
## 0.05503
##
## Description:
## Sun Oct 13 17:43:02 2019 by user:
dgdp=diff(gdp)
tdx=c(1:length(dgdp))
m2=arima(dgdp,order = c(2,0,0),xreg=tdx)
m2
##
## Call:
## arima(x = dgdp, order = c(2, 0, 0), xreg = tdx)
##
## Coefficients:
## ar1 ar2 intercept tdx
## 0.1056 0.2374 0.0018 0
## s.e. 0.0352 0.0353 0.0041 0
##
## sigma^2 estimated as 0.001415: log likelihood = 1425.87, aic = -2841.75
m2$coef
## ar1 ar2 intercept tdx
## 1.055739e-01 2.374440e-01 1.803328e-03 -1.307073e-06
sqrt(diag(m2$var.coef))
## ar1 ar2 intercept tdx
## 3.523565e-02 3.526080e-02 4.130150e-03 3.726187e-05
tratio=m2$coef/sqrt(diag(m2$var.coef))
tratio
## ar1 ar2 intercept tdx
## 2.99622398 6.73393746 0.43662524 -0.03507803
#exercise5
da=read.table("m-aaa-1911.txt",header = T)
vix=log(da$yield)
length(vix)
## [1] 1115
acf(vix)

acf
## function (x, lag.max = NULL, type = c("correlation", "covariance",
## "partial"), plot = TRUE, na.action = na.fail, demean = TRUE,
## ...)
## {
## type <- match.arg(type)
## if (type == "partial") {
## m <- match.call()
## m[[1L]] <- quote(stats::pacf)
## m$type <- NULL
## return(eval(m, parent.frame()))
## }
## series <- deparse(substitute(x))
## x <- na.action(as.ts(x))
## x.freq <- frequency(x)
## x <- as.matrix(x)
## if (!is.numeric(x))
## stop("'x' must be numeric")
## sampleT <- as.integer(nrow(x))
## nser <- as.integer(ncol(x))
## if (is.na(sampleT) || is.na(nser))
## stop("'sampleT' and 'nser' must be integer")
## if (is.null(lag.max))
## lag.max <- floor(10 * (log10(sampleT) - log10(nser)))
## lag.max <- as.integer(min(lag.max, sampleT - 1L))
## if (is.na(lag.max) || lag.max < 0)
## stop("'lag.max' must be at least 0")
## if (demean)
## x <- sweep(x, 2, colMeans(x, na.rm = TRUE), check.margin = FALSE)
## lag <- matrix(1, nser, nser)
## lag[lower.tri(lag)] <- -1
## acf <- .Call(C_acf, x, lag.max, type == "correlation")
## lag <- outer(0:lag.max, lag/x.freq)
## acf.out <- structure(list(acf = acf, type = type, n.used = sampleT,
## lag = lag, series = series, snames = colnames(x)), class = "acf")
## if (plot) {
## plot.acf(acf.out, ...)
## invisible(acf.out)
## }
## else acf.out
## }
## <bytecode: 0x7fe5d175a130>
## <environment: namespace:stats>
m1=arima(vix,order=c(0,1,1))
m1
##
## Call:
## arima(x = vix, order = c(0, 1, 1))
##
## Coefficients:
## ma1
## 0.3695
## s.e. 0.0267
##
## sigma^2 estimated as 0.0004666: log likelihood = 2691.48, aic = -5378.95
Box.test(m1$residuals,lag=10,type="Ljung")
##
## Box-Ljung test
##
## data: m1$residuals
## X-squared = 14.208, df = 10, p-value = 0.1637
pp=1-pchisq(14.20,10)
pp
## [1] 0.1640629
plot(vix)
