微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

对使用 rugarch 包

如何解决对使用 rugarch 包

使用 garchFit 包中的 fGARCH 时,我可以使用 stargazer 制作下表:

Desired output

我使用 rugarch 包创建了我“自己的”GARCH 模型:

spec<- ugarchspec(mean.model=list(armaOrder = c(0,0)),variance.model = list(garchOrder =c(1,1),model="sGARCH"),distribution.model="norm")

当尝试将 stargazer 用于具有此规格(适合)的模型时,出现错误

fit<-ugarchfit(spec,data = data)
stargazer(list(fit  ),title="Regression Results",type="text",keep.stat=c("n","ll","aic","bic"),out="kindaresults.doc")

错误:没有为此 S4 类定义 $ 运算符”:

有人可以帮助我制作与“期望输出”一样好的输出吗?我尝试根据另一个线程 How to export GARCH output to latex? 使用 texreg 包。该线程建议:

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.

在编写以下代码时,我得到:

错误:找不到对象“include.bic”。

如果我删除代码的最后一部分,我会得到 Error: object 'include.bic' not found.,其他 include.X 也是如此。

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)
}
Error: object 'include.bic' not found. 

找不到对象可能与我必须通过将 "}" 添加extract.rugarch 代码关闭括号有关。我不知道我对 extract.rugarch 的期望是什么,但是我得到的,如下所示,似乎不是一个函数,因此可能是我收到消息告诉我找不到对象的问题。

#trying to follow code 
extract.rugarch <- function(fit,include.bic = TRUE) {

>   
Error: unexpected ',' in include.rsquared = TRUE,"

# closing the parenthesis
extract.rugarch <- function(fit,include.bic = TRUE) {}

> extract.rugarch
function(fit,include.bic = TRUE){}

有没有人知道我收到找不到对象的错误的原因?此外,此 texreg 程序是否允许我将多个模型输出并排放置,如“所需输出”中那样,还是最终有多个输出每个只显示一个模型?

附言 我确实在链接的问题下写过,因为我作为网站新成员的声誉评分不允许我发表评论。由于我也有问题,因此发布答案似乎具有误导性。当我以后遇到这些问题时,您会建议我上传新问题、写下答案或以其他方式进行吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。