“Error in window.default(): ‘start’不能在R时间序列分析中晚于’end'”

huangapple go评论67阅读模式
英文:

Error in window.default(): 'start' cannot be after 'end' in R time series analysis

问题

以下是翻译好的部分:

我正在尝试拟合一个一步预测的时间序列模型,所以我需要在每次迭代中扩展我的训练集,如下所示:

library(forecast)
(AirPassengers)
in.sample <- window(AirPassengers, c(1949,1), c(1958,12))
out.sample <- window(AirPassengers, c(1959,1), c(1960,12))
# initialize
hw_forecast <- numeric(); day_fcast <- character()
class(day_fcast) <- "Date"
 
for (i in 1:length(out.sample)) {
         in.sample <- window(AirPassengers, start = c(1991, 1), 
                              end = as.Date(time(out.sample[i]))-1)
         fit= hw(train.ts, seasonal="additive")
         fore1 <- forecast(fit, h=1)
         hw_forecast[i] <- fore1$mean # fore1[[2]][[1]]
         day_fcast[i] <- as.Date(time(out.sample[i,]))
     }

然而,报告了以下错误:

Error in window.default(x, ...) : 'start' cannot be after 'end'

如果您想知道如何确定窗口的结束日期,请告诉我。

英文:

I am trying to fit a one-step ahead time series model, so I need to extend my train set at each iteration as follows:

library(forecast)
(AirPassengers)
in.sample  &lt;- window(AirPassengers, c(1949,1), c(1958,12))
out.sample &lt;- window(AirPassengers, c(1959,1), c(1960,12))
# initialize
hw_forecast &lt;- numeric(); day_fcast &lt;- character()
class(day_fcast) &lt;- &quot;Date&quot;
 
for (i in 1:length(out.sample)) {
         in.sample &lt;- window(AirPassengers, start = c(1991, 1), 
                              end = as.Date(time(out.sample[i]))-1)
         fit= hw(train.ts, seasonal=&quot;additive&quot;)
         fore1 &lt;- forecast(fit, h=1)
         hw_forecast[i] &lt;- fore1$mean # fore1[[2]][[1]]
         day_fcast[i] &lt;- as.Date(time(out.sample[i,]))
     }

However, this error is reported:

Error in window.default(x, ...) : &#39;start&#39; cannot be after &#39;end&#39;

I would appreciated it if you could let me know how to determine the end of window?

答案1

得分: 1

以下是要翻译的代码部分:

使用`subset()`函数来解决这个问题更简单。

```{r}
library(forecast)

in.sample  &lt;- window(AirPassengers, c(1949,1), c(1958,12))
out.sample &lt;- window(AirPassengers, c(1959,1), c(1960,12))
# 初始化
hw_forecast &lt;- out.sample*NA

for (i in seq_along(out.sample)) {
  train.ts &lt;- subset(AirPassengers, end = length(in.sample) + i)
  fit &lt;- hw(train.ts, seasonal=&quot;additive&quot;)
  fore1 &lt;- forecast(fit, h=1)
  hw_forecast[i] &lt;- fore1$mean
}
  • 在你的代码中未定义train.ts。我假设你指的是在前一行创建的ts对象。
  • 这些是月度数据,因此使用as.Date()没有意义。相反,我已经将hw_forecast设置为具有正确起始和频率属性的ts对象。
  • as.Date()不能用于设置ts对象的frequency,因为frequency应该是数值型的。

<details>
<summary>英文:</summary>

It is simpler to use the `subset()` function for this problem.

```{r}
library(forecast)

in.sample  &lt;- window(AirPassengers, c(1949,1), c(1958,12))
out.sample &lt;- window(AirPassengers, c(1959,1), c(1960,12))
# initialize
hw_forecast &lt;- out.sample*NA

for (i in seq_along(out.sample)) {
  train.ts &lt;- subset(AirPassengers, end = length(in.sample) + i)
  fit &lt;- hw(train.ts, seasonal=&quot;additive&quot;)
  fore1 &lt;- forecast(fit, h=1)
  hw_forecast[i] &lt;- fore1$mean
}
  • train.ts is undefined in your code. I have assumed you mean the ts object created on the previous line.
  • These are monthly data, so it makes no sense to use as.Date(). Instead, I have made hw_forecast a ts object with the correct start and frequency attributes.
  • as.Date() can't be used to set the frequency of a ts object, as the frequency should be numeric.

huangapple
  • 本文由 发表于 2023年5月21日 02:33:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定