R - 尝试将 ARIMA 模型数据保存到文本文件



嗨,提前感谢,

所以我有代码,我已经设法生成了我的第一个功能阿里马模型图。

现在我的目标是将 arima 模型预测数据保存到 txt 文件中,但我没有任何运气。

这是我的代码:

library(forecast)
library(ggplot2)
library(reshape2)
#TEST DATA AND TEST CODE#
Quantity <- c(5,3,8,4,0,5,2,7,4,2,6,8,4,7,8,9,4,6)
Time <- c("2010-01-01", "2010-07-02", "2010-08-03", "2011-02-04", "2011-11-05", "2011-12-06", "2012-06-07", "2012-08-30", "2013-04-16", "2013-03-18", "2014-02-22", "2014-01-27", "2015-12-15", "2015-09-28", "2016-05-04", "2017-11-07", "2017-09-22", "2017-04-04")
QuantityFrame <- data.frame(Time,Quantity)
write.table(QuantityFrame,file="C:/....path..../QuantityFrame.txt",quote=F)
#THE FUNCTION#
Frame <- read.table("C:/....path..../QuantityFrame.txt", stringsAsFactors=FALSE, header=TRUE)
Frame$Time <- as.Date(Frame$Time, format= "%Y-%m-%d")
Frame <- ts(Frame$Quantity, start = 1, end=NROW(Frame), frequency=1)
TheForecast <- arima(Frame, order = c(10,1,0),method="ML")
write.table(TheForecast,file="C:/....path..../TheForecast.txt",quote=F)
#CODE TO PROVIDE AN EXAMPLE GRAPH AND EXAMPLE FORECAST DATA#
MyForecast <- plot(forecast(TheForecast,h=10))
print(TheForecast)

因此,我寻求修复的行是:

write.table(TheForecast,file="C:/....path..../TheForecast.txt",quote=F)

当我尝试运行它时,它会生成图形,但不会将预测数据保存到 txt 文件中。我收到此错误:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
cannot coerce class ""Arima"" to a data.frame

谢谢你的帮助。

如果你想保存预测,只需使用 xlsx 包:

library(xlsx)
my_arima <- arima(my_timeseries, order = c(10, 1, 0), method = "ML")
my_forecast <- forecast(my_arima)    
write.xlsx(my_forecast, "my_forecast.xlsx")
#remember to setwd() before. 

如果要导出有马结果,请尝试以下操作:

sink("My_ARIMA.txt") 
my_arima   #since you created it before
sink()

sink("My_ARIMA.txt") 
arima(Y, order = c(10, 1, 0), method = "ML") 
sink()

在Addicction中,请查看Auto.arima Funcion。AR(10( 也许不是完全必要的

最新更新