2017-08-09 80 views
0

Hi和在此先感谢,我已经成功地生产我的第一个运作ARIMA模型图的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) 

所以线Im寻求解决,因此是:

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 

谢谢你的帮助。

回答

0

如果你想保存的预测只使用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)可能并非完全必要

相关问题