I have this ts:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2015.173 1.657143 0.06698996 3.247296 -0.7747861 4.089072
2015.192 1.657143 0.06698996 3.247296 -0.7747861 4.089072
2015.212 1.657143 0.06698996 3.247296 -0.7747861 4.089072
2015.231 1.657143 0.06698996 3.247296 -0.7747861 4.089072
2015.250 1.657143 0.06698996 3.247296 -0.7747861 4.089072
2015.269 1.657143 0.06698996 3.247296 -0.7747861 4.089072
2015.288 1.657143 0.06698996 3.247296 -0.7747861 4.089072
2015.308 1.657143 0.06698996 3.247296 -0.7747861 4.089072
How can I convert it in ts, I am using:
fct <- data.frame(date=as.Date(index(fca)), Y = melt(fca))
But I get this error:
Error in data.frame(date = as.Date(index(fca)), Y = melt(fca)) :
arguments imply differing number of rows: 10, 16
Someone know how to fix it?
答案 0 :(得分:0)
The forecast
output only looks like a data frame when you print it. But if you look at the structure with str(fca)
, you will see the messy guts of the object.
What you see when you call print(fca)
is a specially tailored output as a visual aid ONLY. It is not meant to hint that the underlying object is a matrix-like array.
The solution is to extract the parts you are after and create a data frame from them.
library(forecast)
fit <- Arima(WWWusage,c(3,1,0))
fca <- forecast(fit)
cbind.data.frame(Point=fca$mean, Lower=fca$lower, Upper=fca$upper)
# Point Lower.80% Lower.95% Upper.80% Upper.95%
# 1 0.005151116 -1.271415 -1.947189 1.281718 1.957491
# 2 -0.170997839 -1.716453 -2.534568 1.374457 2.192572
# 3 -0.205288123 -1.782216 -2.616991 1.371640 2.206415
# 4 -0.221099340 -1.812749 -2.655317 1.370550 2.213119
# 5 -0.229741679 -1.830356 -2.677670 1.370872 2.218186
# 6 -0.234764205 -1.841557 -2.692142 1.372029 2.222614
# 7 -0.237705586 -1.849083 -2.702094 1.373671 2.226683
# 8 -0.239354235 -1.854302 -2.709204 1.375594 2.230495
# 9 -0.240158452 -1.857987 -2.714414 1.377670 2.234097
# 10 -0.240391964 -1.860607 -2.718298 1.379824 2.237514
Edit
The dates are hidden pretty well in the data. I did not find an exact date vector. I believe it is generated by the tsp
attribute indicating the start, end, and length:
dates <- attr(fca$mean, "tsp")
datecol <- seq(from=dates[1], to=dates[2], by=dates[3])