Vote count:
1
I'm writing function used for forecasting sales on the base of trend and seasonality. I use dummy variables to represent seasonality and time variable for trend. Here is the code:
forecast<-function(data, time, fn) {
n <- length(data)
seasonal <- factor(cycle(data))
new_data <- seq(from=cycle(data)[n]+1, length=fn, by=1)
new_seasonal <- factor(new_data)
trend <- rep(0, n)
new_trend <- rep(0, n)
if (time > 0) {
trend <- vector()
new_trend <- vector()
ttrend <- seq(from=1, to=n, by=1)
tnew_trend <- seq(from=n+1, length=fn, by=1)
for(i in 1:time) {
trend <- cbind(trend, ttrend^i)
new_trend <- cbind(new_trend, tnew_trend^i)
}
model_trend <- lm(data ~ seasonal + trend)
} else {
model_trend <- lm(data ~ seasonal)
}
df <- data.frame(new_seasonal, new_trend)
p <- predict(model_trend, df)
}
forecast(data = dane.ts[,"SALES"], time=2, fn=5)
However I get warning:
Warning message: 'newdata' had 5 rows but variable(s) found have 104 rows
And it seems that sth is wrong with this function. I would appreciate any help.
asked 3 mins ago
Aucun commentaire:
Enregistrer un commentaire