Kristijan Tornič
Kristijan Tornič

Reputation: 19

R snaive() - number of items to replace is not a multiple of replacement length

I am making a forecasting model for multidimensional data that uses mean and naive methods for forecasting dimensions with small number of observations.

I am saving all resultst into a dataframe. When I try to do that with snaive model, I get an error:

Error in { : task 1 failed - "number of items to replace is not a multiple of replacement length"

This is the part of code that is failing:

if(length(timeseries) < 54){
        fc.resutl <- meanf(timeseries, h = 20, level = c(80, 95))
} else fc.result <- snaive(timeseries, h = 20, level = c(80, 95))

fc.result <- as.data.frame(fc.result)

loop.output <- rbind(loop.output, fc.result)

I tried to print results from meanf and snaive functions and both seem to be in same format:

Point Forecast Lo80 Hi80 Lo95 Hi95

If I change both to meanf, it works fine, so only snaive is returning an error. Any idea what could be the problem?

I checked execution of code line by line and found out that the error is indeed in snaive(). The error trackback is:

9.
.cbind.ts(list(e1, e2), c(deparse(substitute(e1))[1L], 
deparse(substitute(e2))[1L]),union = FALSE) 
8.
Ops.ts(r, tsLag(r, -lag)) 
7.
diff.ts(y, lag = lag) 
6.
diff(y, lag = lag) 
5.
is.data.frame(x) 
4.
var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) 
3.
sd(diff(y, lag = lag), na.rm = TRUE) 
2.
lagwalk(x, lag = frequency(x), h = h, drift = FALSE, level = level, 
    fan = fan, lambda = lambda, biasadj = biasadj) 
1.
snaive(timeseries, h = 20, level = c(80, 95))

Upvotes: 1

Views: 867

Answers (1)

AnscombesGimlet
AnscombesGimlet

Reputation: 145

Sounds like you figured it out but you also have a spelling error in your code on line 2, "fc.resutl" should be "fc.result".

Upvotes: 0

Related Questions