读取程序包
##读取BBmisc程序包
if(!suppressPackageStartupMessages(require('BBmisc'))) {
utils::install.packages('BBmisc', dependencies = TRUE, INSTALL_opts = '--no-lock')
}
suppressPackageStartupMessages(require('BBmisc'))
##读取程序包一栏
lib('openxlsx', 'plyr', 'dplyr', 'tibble', 'rugarch', 'fable', 'tidyr', 'DT', 'knitr', 'kableExtra')
## openxlsx plyr dplyr tibble rugarch fable tidyr
## TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## DT knitr kableExtra
## TRUE TRUE TRUE
读取数据
lchmaizezpr <- read.xlsx('lchmaizezpr.xlsx', detectDates = TRUE)
lchmaizezpr %>%
datatable(
caption = "Data Sample",
escape = FALSE, filter = 'top', rownames = FALSE,
extensions = list('ColReorder' = NULL, 'RowReorder' = NULL,
'Buttons' = NULL, 'Responsive' = NULL),
options = list(dom = 'BRrltpi', autoWidth = FALSE, scrollX = TRUE,
lengthMenu = list(c(10, 50, 100, -1), c('10', '50', '100', 'All')),
ColReorder = TRUE, rowReorder = TRUE,
buttons = list('copy', 'print',
list(extend = 'collection',
buttons = c('csv', 'excel', 'pdf'),
text = 'Download'), I('colvis'))))
函数
test_garch <- function(data, model = 'sGARCH', distribution.model = 'snorm') {
g_specs <- ugarchspec(
variance.model = list(model = model, garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(1, 0), include.mean = TRUE),
distribution.model = distribution.model)
g_fit <- tryCatch({
suppressWarnings(ugarchfit(data = data, spec = g_specs))
}, error = function(cond) {
message('error!')
return(NULL)
})
return(g_fit)
}
info_aic <- function(fit) {
tryCatch({
aic <- suppressWarnings(infocriteria(fit))
aic <- tibble(info = rownames(aic), value = aic[,1])
}, error = function(cond) {
aic <- tibble(
info = c('Akaike', 'Bayes', 'Shibata', 'Hannan-Quinn'),
value = rep(NA, 4))
})
}
比较条件分布模型
cond <- c('norm', 'snorm', 'sstd', 'ged', 'sged', 'nig', 'ghyp', 'jsu')
g_md <- cond %>%
llply(., function(x) {
cat('distribution.model =', x, '\n')
test_garch(data = lchmaizezpr, model = 'sGARCH', distribution.model = x)
})
## distribution.model = norm
## distribution.model = snorm
## distribution.model = sstd
## distribution.model = ged
## distribution.model = sged
## distribution.model = nig
## distribution.model = ghyp
## distribution.model = jsu
names(g_md) <- cond
g_coef <- g_md %>%
llply(., function(x) {
if(!is.null(coef(x))) {
coef(x)
} else {
tibble(mu = NA, ar1 = NA, omega = NA,
alpha1 = NA, beta1 = NA,
skew = NA, shape = NA, ghlambda = NA)
}
}) %>%
bind_rows
g_coef <- tibble(.id = names(g_md), g_coef)
g_coef %>%
kbl('html', caption = 'GARCH 模型系数', escape = FALSE) %>%
## https://www.w3schools.com/cssref/css_colors.asp
row_spec(0, background = 'DimGrey') %>%
column_spec(1, background = 'CornflowerBlue') %>%
column_spec(2, background = 'DarkGrey', color = 'yellow') %>%
column_spec(3, background = 'Gainsboro', color = 'red') %>%
column_spec(4, background = 'LightGray', color = 'red') %>%
column_spec(5, background = 'Gainsboro', color = 'red') %>%
column_spec(6, background = 'LightGray', color = 'red') %>%
column_spec(7, background = 'Gainsboro', color = 'goldenrod') %>%
column_spec(8, background = 'LightGray', color = 'goldenrod') %>%
column_spec(9, background = 'Gainsboro', color = 'red') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
kable_material(full_width = FALSE) %>% ##`full_width = FALSE` will auto adjust every single columns width to fit the table full width.
scroll_box(width = '100%', fixed_thead = TRUE)#, height = '400px')
GARCH
.id
|
mu
|
ar1
|
omega
|
alpha1
|
beta1
|
skew
|
shape
|
ghlambda
|
norm
|
20617.00
|
0.9974666
|
211.494231
|
0
|
0.00e+00
|
NA
|
NA
|
NA
|
snorm
|
24286.02
|
0.9988917
|
2.882237
|
0
|
6.00e-06
|
0.9537219
|
NA
|
NA
|
sstd
|
22629.39
|
0.9987934
|
216.872399
|
0
|
7.10e-06
|
0.5369691
|
2.0220015
|
NA
|
ged
|
20617.73
|
0.9977462
|
4.084777
|
0
|
1.42e-05
|
NA
|
0.8329239
|
NA
|
sged
|
NA
|
NA
|
NA
|
NA
|
NA
|
NA
|
NA
|
NA
|
nig
|
20951.11
|
0.9994626
|
2884.463954
|
0
|
6.40e-06
|
-0.9900000
|
0.0100550
|
NA
|
ghyp
|
20741.73
|
0.9979270
|
14.700261
|
0
|
7.00e-06
|
-0.7733294
|
0.2500041
|
-0.7083291
|
jsu
|
NA
|
NA
|
NA
|
NA
|
NA
|
NA
|
NA
|
NA
|
最优条件分布模型
aic_tb <- g_md %>%
ldply(info_aic) %>%
spread(info, value)
## 最低 aic 值为最优模型
aic_tb %>% dplyr::mutate(
Akaike = ifelse(
rank(Akaike) <= 3,
cell_spec(
paste0(round(Akaike, 7), ' (rank: ', sprintf('%1.f', rank(Akaike)), ')'),
'html', color = 'darkgoldenrod', bold = TRUE),
cell_spec(
paste0(round(Akaike, 7), ' (rank: ', sprintf('%1.f', rank(Akaike)), ')'),
'html', color = 'grey', italic = TRUE)),
Bayes = ifelse(
rank(Bayes) <= 3,
cell_spec(
paste0(round(Bayes, 7), ' (rank: ', sprintf('%1.f', rank(Bayes)), ')'),
'html', color = 'darkgoldenrod', bold = TRUE),
cell_spec(
paste0(round(Bayes, 7), ' (rank: ', sprintf('%1.f', rank(Bayes)), ')'),
'html', color = 'grey', italic = TRUE)),
`Hannan-Quinn` = ifelse(
rank(`Hannan-Quinn`) <= 3,
cell_spec(
paste0(round(`Hannan-Quinn`, 7), ' (rank: ', sprintf('%1.f', rank(`Hannan-Quinn`)), ')'),
'html', color = 'darkgoldenrod', bold = TRUE),
cell_spec(
paste0(round(`Hannan-Quinn`, 7), ' (rank: ', sprintf('%1.f', rank(`Hannan-Quinn`)), ')'),
'html', color = 'grey', italic = TRUE)),
Shibata = ifelse(
rank(Shibata) <= 3,
cell_spec(
paste0(round(Shibata, 7), ' (rank: ', sprintf('%1.f', rank(Shibata)), ')'),
'html', color = 'darkgoldenrod', bold = TRUE),
cell_spec(
paste0(round(Shibata, 7), ' (rank: ', sprintf('%1.f', rank(Shibata)), ')'),
'html', color = 'grey', italic = TRUE))) %>%
kbl('html', caption = 'GARCH 模型 AIC', escape = FALSE) %>%
## https://www.w3schools.com/cssref/css_colors.asp
row_spec(0, background = 'DimGrey') %>%
column_spec(1, background = 'CornflowerBlue') %>%
column_spec(2, background = 'DarkGrey') %>%
#column_spec(3, background = 'LightSlateGrey') %>%
column_spec(3, background = 'LightGray') %>%
column_spec(4, background = 'Gainsboro') %>%
column_spec(5, background = 'LightGray') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
kable_material(full_width = FALSE) %>% ##`full_width = FALSE` will auto adjust every single columns width to fit the table full width.
scroll_box(width = '100%', fixed_thead = TRUE)#, height = '400px')
GARCH AIC
.id
|
Akaike
|
Bayes
|
Hannan-Quinn
|
Shibata
|
ged
|
4.2173225 (rank: 4)
|
4.3048587 (rank: 4)
|
4.2526011 (rank: 4)
|
4.2160925 (rank: 4)
|
ghyp
|
4.0284844 (rank: 3)
|
4.1451994 (rank: 3)
|
4.0755226 (rank: 3)
|
4.0263211 (rank: 3)
|
jsu
|
NA (rank: 7)
|
NA (rank: 7)
|
NA (rank: 7)
|
NA (rank: 7)
|
nig
|
2.556921 (rank: 1)
|
2.6590466 (rank: 1)
|
2.5980794 (rank: 1)
|
2.5552559 (rank: 1)
|
norm
|
8.2757366 (rank: 6)
|
8.3486835 (rank: 6)
|
8.3051355 (rank: 6)
|
8.2748779 (rank: 6)
|
sged
|
NA (rank: 8)
|
NA (rank: 8)
|
NA (rank: 8)
|
NA (rank: 8)
|
snorm
|
4.8931782 (rank: 5)
|
4.9807144 (rank: 5)
|
4.9284569 (rank: 5)
|
4.8919483 (rank: 5)
|
sstd
|
3.3929488 (rank: 2)
|
3.4950744 (rank: 2)
|
3.4341073 (rank: 2)
|
3.3912837 (rank: 2)
|