บันทึกผลการคาดการณ์เป็นไฟล์ csv ใน R

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)

ฉันจะเรียกใช้สคริปต์การคาดการณ์ทุกๆ 5 นาทีจากบรรทัด spec <- ugarchspec (variance.model = list (model = "sGARCH นี้" ตัวอย่างเช่น สคริปต์เปิดตัวเมื่อเวลา 10:10 น. การคาดการณ์ดำเนินการ 5 ขั้นตอน ผลลัพธ์นี้จะต้องเขียนลงในไฟล์ csv

จากนั้นเปิดตัวในเวลา 10:15 น. คาดการณ์ไว้ 5 ขั้นตอน จากนั้นผลลัพธ์นี้จะต้องเขียนลงในไฟล์ csv พร้อมเครื่องหมายวันที่

จากนั้นเวลา 10:20 น. เป็นต้น จะเพิ่มการคาดการณ์ต่อท้าย csv หนึ่งรายการพร้อมเครื่องหมายวันที่ทุกครั้งที่สคริปต์ทำงานได้อย่างไร

เอาต์พุตสามารถ ป้อนคำอธิบายรูปภาพที่นี่


person cbool    schedule 28.01.2019    source แหล่งที่มา
comment
Sys.time() จะให้วันที่ + เวลาเมื่อมีการเรียกใช้คำสั่ง ใช้ cbind() เพื่อบันทึกทั้งวันที่และเวลาและการทำนายใช่ไหม   -  person RLave    schedule 28.01.2019
comment
final <- cbind(datetime=Sys.time(), steps=my_predicitons) เช่น   -  person RLave    schedule 28.01.2019
comment
@RLave, my_predicitons มันเป็นชื่อของไฟล์ csv หรือไม่   -  person cbool    schedule 28.01.2019
comment
ไม่ มันคือเวกเตอร์ของการทำนาย คอลัมน์ step ของคุณ   -  person RLave    schedule 28.01.2019


คำตอบ (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)

ตอนนี้คุณสามารถบันทึก final ด้วย write.csv()

person RLave    schedule 28.01.2019