r预测库奇怪的行为

时间:2016-09-14 10:08:59

标签: r forecasting

这是我的数据框:

my data frame

这是它的结构:

> str(agg.forec)
'data.frame':   29 obs. of  5 variables:
 $ ricavo   : num  6807 34271 66053 74892 35364 ...
 $ costo    : num  5801 24064 45815 53518 25482 ...
 $ range    : num  1006 10206 20238 21374 9882 ...
 $ annomese : Date, format: "2014-05-01" "2014-06-01" "2014-07-01" ...
 $ annomese2:Class 'yearmon'  num [1:29] 2014 2014 2014 2015 2015 ...

我想使用此代码预测接下来六个月的范围列值以及库预测:

library(zoo)
library(forecast)

forec <- read.csv("C:/Users/ALBINO/Desktop/prove/export_v_bi_performances.csv", sep = ';')

forec.two <- forec[,-c(3:7,10,11)]

agg.forec <-aggregate(forec.two, by=list(forec.two$mese,forec.two$anno), 
                    FUN=sum)
agg.forec <- agg.forec[,-c(3,4)]
colnames(agg.forec) <- c("mese","anno","costo","ricavo")
agg.forec <- agg.forec[,c("anno","mese","ricavo","costo")]

agg.forec$range <- agg.forec$ricavo - agg.forec$costo
agg.forec$annomese <- cbind(paste(agg.forec[,1],agg.forec[,2], sep="-"))
agg.forec[,1:2] <- NULL
agg.forec$annomese <- as.Date(as.yearmon(agg.forec$annomese))
agg.forec$annomese2 <- as.yearmon(agg.forec$annomese)

#model.aggforec <- lm(range[2:28] ~ annomese2[2:28], data = agg.forec)
#plot(x = agg.forec$annomese2, y = agg.forec$range, type = "l")
#abline(model.aggforec, col = "red")

rangeTs <- ets(agg.forec$range[2:28])
rangeTsYear <- forecast(rangeTs, h = 6)
plot(rangeTsYear)

然而,预测结果是完美的水平线,考虑到数据随着时间的推移而增加是不可能的。

Resulted plotted data

您有预测这些数据的建议吗?我的小代码中是否有错误?

0 个答案:

没有答案