-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path04-Estimate_Garch_Model.R
96 lines (75 loc) · 3.02 KB
/
04-Estimate_Garch_Model.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# A Garch Tutorial with R - Estimate simple garch model
# Paper at <https://rac.anpad.org.br/index.php/rac/article/view/1420>
#
# This script will estimate a simple garch model, with three different distributions,
# and save estimation results in a .html file
# OPTIONS
ar_lag <- 0 # lag used for ar term in mean equation (0 in paper)
ma_lag <- 0 # lag used for ma term in mean equation (0 in paper)
arch_lag <- 1 # lag in arch effect (1 in paper)
garch_lag <- 1 # lag in garch effect (1 in paper)
models_to_estimate <- c('sGARCH', 'eGARCH', 'gjrGARCH') # see rugarch manual for more
distribution_to_estimate <- 'norm' # distribution used in all models
my_html_file <- 'tabs/tab04-estimation_garch.html' # where to save html file?
# END OPTIONS
library(tidyverse)
library(FinTS)
library(texreg)
library(rugarch)
# close all opened windows
graphics.off()
# change working directory
my_d <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(my_d)
# source functions
source('fcts/garch_fcts.R')
# get all combinations of models
df_grid <- expand_grid(ar_lag,
ma_lag,
arch_lag,
garch_lag,
models_to_estimate,
distribution_to_estimate)
# get price data
df_prices <- read_rds('data/RAC-GARCH-Data.rds')
estimate_garch <- function(ar_lag,
ma_lag,
arch_lag,
garch_lag,
models_to_estimate,
distribution_to_estimate) {
message('Estimating ARMA(',ar_lag,',', ma_lag, ')', '-',
models_to_estimate, '(', arch_lag, ',', garch_lag, ') ',
'dist = ', distribution_to_estimate)
# estimate model
my_spec <- ugarchspec(variance.model = list(model = models_to_estimate,
garchOrder = c(arch_lag,
garch_lag)),
mean.model = list(armaOrder = c(ar_lag,
ma_lag)),
distribution.model = distribution_to_estimate)
my_garch <- ugarchfit(spec = my_spec, data = df_prices$log_ret)
return(my_garch)
}
# estimate all models
l_args <- as.list(df_grid)
l_models <- pmap(.l = l_args, .f = estimate_garch)
# make sure dir "tabs" exists
if (!dir.exists('tabs')) dir.create('tabs')
# reformat models for texreg
l_models <- map(l_models, extract.rugarch, include.rsquared = FALSE)
# write custom row
custom_row <- list('Variance Model' = df_grid$models_to_estimate,
'Distribution' = df_grid$distribution_to_estimate)
custom_names <- paste0('Model ', 1:length(l_models))
# save to html
htmlreg(l_models,
file = my_html_file,
custom.gof.rows = custom_row,
custom.model.names = custom_names,
digits = 3)
# print to screen
screenreg(l_models,
custom.gof.rows = custom_row,
custom.model.names = custom_names,
digits = 3)