4

I want to export the results of a GARCH model fitted with the package rugarch to latex but I cannot find a suitable package for it.

Usually the package stargazer would be perfect for that but stargazer only supports the output of the fGarch package. print()does not work either.

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)
  • 1
    Depending on how well you want the output formatted you can simply use the `\begin{verbatim} .... \end{verbatim}` environment. copying the output into the environment will keep it's layout. Depending on how neat you want it to be, that might do well enough – Oliver Aug 01 '19 at 17:29
  • Good idea but I was hoping someone has a proper solution –  Aug 01 '19 at 18:15

2 Answers2

5

You can use the texregpackage for this purpose and customize it for the ugarchfit function of the rugarchpackage:


library(texreg)

#define dependent variable:

y <- x #to generalize  your case (y is usually the dependent 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, the package author, provided a really good and detailed explanation on how to customize texreg for unsupported packages: Print "pretty" tables for h2o models in R

Emil
  • 51
  • 1
  • 4
1

stargazer fails since the result from ugarchfit is too ambiguous. You simply need to extract the desired values. Following shows one realization of that procedure.

(This version only supports the GARCH model specified in the question and covers only the parameter estimates)

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")