英文:
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 <- 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,]))
}
However, this error is reported:
Error in window.default(x, ...) : 'start' cannot be after 'end'
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 <- window(AirPassengers, c(1949,1), c(1958,12))
out.sample <- window(AirPassengers, c(1959,1), c(1960,12))
# 初始化
hw_forecast <- out.sample*NA
for (i in seq_along(out.sample)) {
train.ts <- subset(AirPassengers, end = length(in.sample) + i)
fit <- hw(train.ts, seasonal="additive")
fore1 <- forecast(fit, h=1)
hw_forecast[i] <- 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 <- window(AirPassengers, c(1949,1), c(1958,12))
out.sample <- window(AirPassengers, c(1959,1), c(1960,12))
# initialize
hw_forecast <- out.sample*NA
for (i in seq_along(out.sample)) {
train.ts <- subset(AirPassengers, end = length(in.sample) + i)
fit <- hw(train.ts, seasonal="additive")
fore1 <- forecast(fit, h=1)
hw_forecast[i] <- fore1$mean
}
train.ts
is undefined in your code. I have assumed you mean thets
object created on the previous line.- These are monthly data, so it makes no sense to use
as.Date()
. Instead, I have madehw_forecast
ats
object with the correct start and frequency attributes. as.Date()
can't be used to set thefrequency
of ats
object, as thefrequency
should be numeric.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论