首页 > 解决方案 > UseMethod(“predict”)中的错误:没有适用于“predict”的方法应用于 R 中类“c('uGARCHfit','GARCHfit','rGARCH')”的对象

问题描述

我通过“ https://api.coindesk.com/v1/bpi/currentprice/USD.json ”的API动态获取财务数据,为此我使用此脚本

library("rjson")
json_file <- "https://api.coindesk.com/v1/bpi/currentprice/USD.json"

numOfTimes <- 2L # how many times to run in total
sleepTime <- 60L  # time to wait between iterations (in seconds)
iteration <- 0L
while (iteration < numOfTimes) {
  # gather data
  json_data <- fromJSON(paste(readLines(json_file), collapse=""))
  # get json content as data.frame
  x = data.frame(json_data$time$updated,json_data$time$updatedISO,json_data$time$updateduk,json_data$bpi$USD)
  # create file to save in 'C:/Myfolder' 
  # alternatively, create just one .csv file and update it in each iteration
  nameToSave <- nameToSave <- paste('C:/Myfolder/', 
                                    gsub('\\D','',format(Sys.time(),'%F%T')), 
                                    'json_data.csv', sep = '_')
  # save the file
  write.csv(x, nameToSave)
  # update counter and wait 
  iteration <- iteration + 1L
  Sys.sleep(sleepTime)
}    

此脚本由 cmd 文件每分钟由 Win8.1 中的调度程序运行

"C:\Program Files\R\R-3.5.1\bin\x64\Rcmd.exe" BATCH "E:\Rdev\bitcoin.R"

因此,带有 csv 的文件夹会被新的 csv 动态更新。

假设我创建了 GARCH-ARMA 模型

require(rugarch)


#We can then compute the ARMA(1,1)-GARCH(1,1) model as an example:



  spec <- ugarchspec(variance.model = list(model = "sGARCH", 
                                           garchOrder = c(1, 1), 
                                           submodel = NULL, 
                                           external.regressors = NULL, 
                                           variance.targeting = FALSE), 

                     mean.model     = list(armaOrder = c(1, 1), 
                                           external.regressors = NULL, 
                                           distribution.model = "norm", 
                                           start.pars = list(), 
                                           fixed.pars = list()))

garch <- ugarchfit(spec = spec, data = df$rate_float, solver.control = list(trace=0))

现在当我这样做的时候

predict(garch, n.ahead = 5)

我得到了错误

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "c('uGARCHfit', 'GARCHfit', 'rGARCH')"

标签: rtime-series

解决方案


似乎您只是使用了错误的功能。试试吧

ugarchforecast(garch, n.ahead = 5)

推荐阅读