将预测结果保存到R

时间:2019-01-28 10:17:53

标签: r dataframe time-series

df=structure(list(X.1 = 1:6, X = c(1L, 1L, 1L, 1L, 1L, 1L), json_data.time.updated = structure(1:6, .Label = c("Jan 19, 2019 15:18:00 UTC", 
"Jan 19, 2019 15:19:00 UTC", "Jan 19, 2019 15:51:00 UTC", "Jan 19, 2019 15:52:00 UTC", 
"Jan 19, 2019 15:54:00 UTC", "Jan 19, 2019 15:55:00 UTC"), class = "factor"), 
    json_data.time.updatedISO = structure(1:6, .Label = c("2019-01-19T15:18:00+00:00", 
    "2019-01-19T15:19:00+00:00", "2019-01-19T15:51:00+00:00", 
    "2019-01-19T15:52:00+00:00", "2019-01-19T15:54:00+00:00", 
    "2019-01-19T15:55:00+00:00"), class = "factor"), json_data.time.updateduk = structure(1:6, .Label = c("Jan 19, 2019 at 15:18 GMT", 
    "Jan 19, 2019 at 15:19 GMT", "Jan 19, 2019 at 15:51 GMT", 
    "Jan 19, 2019 at 15:52 GMT", "Jan 19, 2019 at 15:54 GMT", 
    "Jan 19, 2019 at 15:55 GMT"), class = "factor"), code = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "USD", class = "factor"), rate = structure(c(2L, 
    3L, 6L, 1L, 5L, 4L), .Label = c("3,735.3200", "3,735.7750", 
    "3,735.9150", "3,736.0750", "3,736.7717", "3,736.9100"), class = "factor"), 
    description = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "United States Dollar", class = "factor"), 
    rate_float = structure(c(2L, 3L, 6L, 1L, 5L, 4L), .Label = c("3735.32", 
    "3735.775", "3735.915", "3736.075", "3736.7717", "3736.91"
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L)) 

-

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


ugarchforecast(garch, n.ahead = 5)

我将从spec <- ugarchspec (variance.model = list (model = "sGARCH”这一行开始每5分钟运行一次预测脚本,例如,该脚本是在10:10启动的,预测是通过5个步骤进行的,因此必须将该结果写入csv文件

然后在10:15启动,通过5个步骤进行预测,然后 此结果必须用日期标记写入csv文件

然后在10:20等。每次脚本运行时,如何将预测添加到带有日期标记的一个csv中?

输出可以这样 enter image description here

1 个答案:

答案 0 :(得分:1)

仅作为示例:

linmod <- lm(mpg ~ hp, data = mtcars) # your model

predictions <- predict(linmod) # your vector of predictions

在只保存cbind()之前,先保存时间:

final <- cbind(date_time=format(Sys.time(), format="%Y/%m/%d %H:%M"),
               predictions = predictions)

现在您可以将finalwrite.csv()保存起来。