-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path03-Do_ARCH_Test.R
54 lines (38 loc) · 1.25 KB
/
03-Do_ARCH_Test.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# A Garch Tutorial with R - Perform ARCH test in series of returns
# Paper at <https://rac.anpad.org.br/index.php/rac/article/view/1420>
#
# This script will test for arch effects for a given vector of returns, given
# lags and save results in .html file.
## OPTIONS
max_lag <- 5
my_html_file <- 'tabs/tab03-Arch_Test.html'
my_xlsx_file <- 'tabs/tab03-Arch_Test.xlsx'
## END OPTIONS
library(tidyverse)
library(knitr)
library(kableExtra)
library(writexl)
my_d <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(my_d)
source('fcts/garch_fcts.R')
# create directory
if (!dir.exists(dirname(my_html_file))) dir.create(dirname(my_html_file))
# get price data
df_prices <- read_rds('data/RAC-GARCH-Data.rds')
# do arch test
tab_out <- do_arch_test(x = df_prices$log_ret, max_lag = max_lag)
tab_out
# remove attributes of table so it can be correctly parsed in html
tab_out <- as.data.frame(
lapply(tab_out, function(x) { attributes(x) <- NULL; x })
)
str(tab_out)
rownames(tab_out) <- NULL
# save table in html
my_tbl <- knitr::kable(tab_out, format = 'html' ) %>%
kable_styling(bootstrap_options = c("striped"),
full_width = FALSE )
my_tbl
cat(my_tbl, file = my_html_file)
# write to excel
write_xlsx(x = tab_out, path = my_xlsx_file)