我一直在R中使用預測包,但發現很難將我自己的日常時間序列加載到ts對象中,然後將其與預測算法一起使用。我已經使用動物園來創建我的每日時間序列對象,但是我無法直接將它傳遞給R預測包中的預測算法。R預測包和每日時間序列
任何幫助正確的方向將不勝感激。我覺得這很困難。
感謝
你好下面是一些示例代碼。用一個簡單的季度數據集,我可以得到一些預測工作,但對於每日數據集,我無法使其工作。非常感謝
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"))
這裏是一個仍然無法正常工作
#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"))
非常感謝
提供一個可重現的問題示例。這適用於我:'預測(動物園(1:5))' – 2013-05-07 13:59:07
嗨 這裏是一些示例代碼。用一個簡單的季度數據集,我可以得到一些預測工作,但對於每日數據集,我無法使其工作。 – orbital 2013-05-07 23:23:40