我一直在使用 R 中的预测包,但发现很难将我自己的每日时间序列加载到 ts 对象中,然后将其与预测算法一起使用。我改为使用 zoo 来创建我的每日时间序列对象,但我无法将其直接传递给 R Forecast 包中的预测算法。
任何正确方向的帮助将不胜感激。我对此感到非常困惑。
谢谢
嗨这是一些示例代码。使用简单的季度数据集,我可以进行一些预测,但使用每日数据集,我无法使其正常工作。非常感谢
require("forecast")
require("fpp")
# Example from Forecasting Principles and Practice
# http://otexts.com/fpp/2/5/
#beer2 <- window(ausbeer,start=1992,end=2006-.1)
#start with a really small dataset (only 6 data points)
beer2 <- window(ausbeer,start=2006,end=2006-.1)
print(beer2)
beerfit1 <- meanf(beer2,h=11)
beerfit2 <- rwf(beer2,h=11)
beerfit3 <- snaive(beer2,h=11)
plot(beerfit1, plot.conf=FALSE, main="Forecasts for quarterly beer production")
lines(beerfit2$mean,col=2)
lines(beerfit3$mean,col=3)
lines(ausbeer)
legend("topright", lty=1, col=c(4,2,3), legend=c("Mean method","Naive method","Seasonal naive method"))
beer3 <- window(ausbeer, start=2006)
accuracy(beerfit1, beer3)
accuracy(beerfit2, beer3)
accuracy(beerfit3, beer3)
#now make a really small daily dataset (Each day for two weeks)
forecast_datesequence = seq(from=as.Date("2013-05-06"), to=as.Date("2013-05-19"), by=1)
vals <- c(100,150,300,150,100,45,25,100,150,300,150,100,45,25)
dailyzoo_ts <- zoo(vals, forecast_datesequence)
print(daily_ts)
dailyfit1 <- meanf(coredata(dailyzoo_ts),h=7)
dailyfit2 <- rwf(coredata(dailyzoo_ts),h=7)
dailyfit3 <- snaive(coredata(dailyzoo_ts),h=7)
plot(dailyfit1, plot.conf=FALSE, main="Daily Data Over 2 Week Period")
lines(dailyfit2$mean,col=2)
lines(dailyfit3$mean,col=3)
lines(dailyzoo_ts)
legend("topright", lty=1, col=c(4,2,3), legend=c("Mean method","Naive method","Seasonal naive method"))
这是一个更新的 R 代码,但仍然无法正常工作
#now make a really small daily dataset (Each day for two weeks)
forecast_datesequence = seq(from=as.Date("2013-05-06"), to=as.Date("2013-05-19"), by=1)
vals <- c(100,150,300,150,100,45,25,100,150,300,150,100,45,25)
dailyzoo_ts <- zoo(vals, forecast_datesequence)
print(daily_ts)
z <- zoo(coredata(dailyzoo_ts), 1:14/7)
print(z)
plot(forecast(z))
#stl(z)
dailyfit1 <- meanf(z,h=7)
dailyfit2 <- rwf(z,h=7)
dailyfit3 <- snaive(z,h=7)
plot(dailyfit1, plot.conf=FALSE, main="Daily Data Over 2 Week Period")
lines(dailyfit2$mean,col=2)
lines(dailyfit3$mean,col=3)
lines(z)
legend("topright", lty=1, col=c(4,2,3), legend=c("Mean method","Naive method","Seasonal naive method"))
非常感谢