如何将GARCH输出导出到乳胶?

时间:2019-08-01 15:43:36

标签: r latex

我想将装有软件包rugarch的GARCH模型的结果导出到乳胶中,但是找不到适合它的软件包。

通常,软件包stargazer会是完美的选择,但是stargazer仅支持fGarch软件包的输出。 print()也不起作用。

MWE:

x <- rnorm(1:100)

spec <- rugarch::ugarchspec(
          variance.model = list(model = "sGARCH"),
          mean.model = list(armaOrder = c(0, 0),
                            include.mean = TRUE),
          distribution = "std")
fit <- rugarch::ugarchfit(spec = spec, data = x)

2 个答案:

答案 0 :(得分:2)

您可以为此目的使用texreg软件包,并针对ugarchfit软件包的rugarch功能对其进行自定义:


library(texreg)

#define independent variable:

y <- x #to generalize  your case (y is usually the independent variable)

extract.rugarch <- function(fit, 
                            include.rsquared = TRUE, include.loglike = TRUE, include.aic = TRUE, include.bic = TRUE) {

  # extract coefficient table from fit:
  coefnames <- rownames(as.data.frame(fit@fit$coef))
  coefs <- fit@fit$coef
  se <- as.vector(fit@fit$matcoef[, c(2)])
  pvalues <-  as.vector(fit@fit$matcoef[, c(4)])       # numeric vector with p-values

  # create empty GOF vectors and subsequently add GOF statistics from model:
  gof <- numeric()
  gof.names <- character()
  gof.decimal <- logical()
  if (include.rsquared == TRUE) {
    r2 <-  1 - (var(fit@fit$residuals) / var(y))
    gof <- c(gof, r2)
    gof.names <- c(gof.names, "R^2")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.loglike == TRUE) {
    loglike <- fit@fit$LLH
    gof <- c(gof, loglike)
    gof.names <- c(gof.names, "Log likelihood")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.aic == TRUE) {
    aic <- infocriteria(fit)[c(1)]
    gof <- c(gof, aic)
    gof.names <- c(gof.names, "AIC")
    gof.decimal <- c(gof.decimal, TRUE)
  }

  if (include.bic == TRUE) {
    bic <- infocriteria(fit)[c(2)]
    gof <- c(gof, bic)
    gof.names <- c(gof.names, "BIC")
    gof.decimal <- c(gof.decimal, TRUE)
  }

  # create texreg object:
  tr <- createTexreg(
    coef.names = coefnames, 
    coef = coefs,
    se = se,
    pvalues = pvalues, 
    gof.names = gof.names, 
    gof = gof, 
    gof.decimal = gof.decimal
  )
  return(tr)
}

#print table:
texreg(extract.rugarch(fit, include.rsquared = FALSE)) #for latex # as R^2 is zero in this example.

软件包作者Philip Leifeld就如何为不受支持的软件包自定义texreg提供了非常好的详细解释:Print "pretty" tables for h2o models in R

答案 1 :(得分:0)

stargazer失败,因为ugarchfit的结果太含糊。 您只需要提取所需的值。 下面显示了该过程的一种实现。

(此版本仅支持问题中指定的GARCH模型,并且仅涵盖参数估计值)

require("magrittr")

stargazer::stargazer(fit@fit$matcoef, 
  title = "Parameter Estimates of the GARCH(1, 1)") %>% 
  gsub("Std. Error", "Rob. Std. Error", .) %>%  
  gsub("t value", "Rob. t value", .) %>%  
  gsub("mu", "$\\\\mu$", .) %>%
  gsub("alpha1", "$\\\\alpha$", .) %>%
  gsub("omega", "$\\\\omega$", .) %>%  
  gsub("beta1", "$\\\\beta$", .) %>%
  gsub("shape", "$\\\\nu$", .)  %>%
  writeLines("arch_output.tex")