【问题标题】:R snaive() - number of items to replace is not a multiple of replacement lengthR snaive() - 要替换​​的项目数不是替换长度的倍数
【发布时间】:2017-08-09 12:56:09
【问题描述】:

我正在为多维数据创建一个预测模型,该模型使用均值和朴素方法来预测具有少量观察值的维度。

我将所有结果保存到数据框中。当我尝试使用 snaive 模型执行此操作时,出现错误:

{ 中的错误:任务 1 失败 - “要替换的项目数不是 替换长度的倍数"

这是失败的代码部分:

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)

我尝试打印 meanf 和 snaive 函数的结果,它们的格式似乎相同:

Point Forecast Lo80 Hi80 Lo95 Hi95

如果我将两者都更改为 meanf,它可以正常工作,所以只有 snaive 会返回错误。知道可能是什么问题吗?

我逐行检查代码的执行,发现错误确实在snaive()中。错误引用为:

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))

【问题讨论】:

  • 请提供一个可重现的最小示例。
  • 没关系,snaive 只接受 int 频率时间序列,而我的每周预测设置为 365,25/7。

标签: r forecasting


【解决方案1】:

听起来您已经明白了,但您在第 2 行的代码中也有拼写错误,“fc.resutl”应该是“fc.result”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2020-02-02
    相关资源
    最近更新 更多