将预测结果保存在 R 中的 csv 文件中 [英] Save forecast result in csv file in R

查看:38
本文介绍了将预测结果保存在 R 中的 csv 文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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", 例如,脚本在 10:10,预测是分5步做的,这个结果必须写入csv文件

I will run the forecast script every 5 minutes from this line of spec <- ugarchspec (variance.model = list (model = "sGARCH", for example, the script was launched at 10:10, the forecast was made by 5 steps, this result must be written into csv file

然后是10点15分启动,预测分5步做,然后此结果必须写入带有日期标记的 csv 文件

then it was launched in 10:15, the forecast was made by 5 steps, then this result must be written into csv file with date mark

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

then at 10:20 and so on. How to append predictions to one csv with date mark every time when script runs?

输出可以这样

推荐答案

举个例子:

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)

现在您可以使用 write.csv() 保存 final.

Now you can save final with write.csv().

这篇关于将预测结果保存在 R 中的 csv 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆