This is my data frame:
This is its structure:
> 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 ...
I want to predict the range column values for the next six month with R and the library forecast using this code:
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)
However the predicted result is a perfect horizzontal line that it's impossible considering that the data increase in the course of time.
Have you some suggestions to predict those data? Are there errors in my little code?