For循环和追加到R中的向量

vlf7wbxs  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(172)

我试图在R中创建一个For循环,用预测值填充一个Vector,预测值是通过auto.arima函数生成的。
我是R的新手,所以不确定这是否做对了。
我使用的代码如下:

library(dplyr)
library(forecast)
dfts <- ts(df$Price_REG1) 
fc=c()
for (i in 0:7) {
  modArima <- auto.arima(dfts[0+(i*24):168+(i*24)])
  forecast <- forecast(modArima, h=24)
  forecast_values <- forecast$mean
  fc <- append(fc, forecast_values)
}

我在现实中使用较长的集合,但在这里使其变小,使其更容易理解。
我尝试实现的是使用第一周的数据(一周中的168小时)来估计模型的系数。然后,我希望将训练集之后的前24小时生成的预测放入向量fc中。然后,我希望在某一天移动窗口,重新估计系数并生成第二天的预测,然后将它们保存到向量中。
我对dfts[0+(i*24):168+(i*24)]部分有点不确定,因为df <- df[0:168]不起作用,但需要df <- df[0:168,]。但如果我输入dfts[0+(i*24):168+(i*24)],则得到
[. default(离散度,0+(i * 24):874+(i * 24),)中出错:维数不正确
编辑:数据样本:

structure(c(28.78, 28.45, 27.9, 27.52, 27.54, 26.55, 25.83, 25.07, 
25.65, 26.15, 26.77, 27.4, 28.08, 28.69, 29.37, 29.97, 30.46, 
30.39, 30.06, 29.38, 27.65, 27.33, 25.88, 24.81, 12.07, 13.13, 
19.07, 21.12, 24.29, 26.27, 27.74, 28.39, 29.37, 29.95, 29.91, 
29.96, 29.94, 29.94, 30.18, 30.96, 31.2, 30.98, 30.35, 29.27, 
28.17, 28.02, 27.69, 24.39, 18.93, 9.98, 1.53, 0.14, 0.85, 9.92, 
24.48, 26.68, 28.12, 28.58, 28.16, 28.78, 28.31, 28.44, 28.96, 
29.86, 30.15, 30.07, 29.54, 29.11, 27.91, 27.03, 25.7, 22.04, 
21.73, 15.95, 16.23, 6.45, 3.83, 4.03, 4.04, 19.07, 17.49, 24.18, 
24.94, 25.11, 24.94, 24.95, 25.25, 26.33, 27.36, 28.88, 29.58, 
29.42, 27.71, 27.4, 27.37, 25.77, 26.65, 27.13, 27.11, 27.42), tsp = c(1, 
5.125, 24), class = "ts")
w8biq8rn

w8biq8rn1#

下面是一个内置数据集AirPassengers的示例,说明如何使用程序包forecast运行滚动预测。
下面的代码使用了时间序列函数

  • window到类"ts"的子集对象;
  • frequencystart来获取这些属性。

输出向量是预先创建的,没有在循环中用append扩展。

library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
data("AirPassengers", package = "datasets")

fc <- ts(
  data = rep(NA, length(AirPassengers)), 
  start = start(AirPassengers), 
  frequency = frequency(AirPassengers)
)

start <- start(AirPassengers)[1]
freq <- frequency(AirPassengers)
i_fc <- seq_len(freq)
fc[i_fc] <- AirPassengers[i_fc]

for(i in 1:11) {
  w <- window(AirPassengers, start = start + i - 1L, end = c(start + i - 1L, freq))
  modArima <- auto.arima(w)
  y <- forecast(modArima, h = freq)$mean
  i_fc <- i_fc + freq
  fc[i_fc] <- y
}

plot(cbind(AirPassengers, fc))

创建于2022年12月20日,使用reprex v2.0.2

编辑

我相信下面的代码预测了给定的初始天数的第二天。

library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

fill_first_periods <- function(x, weeks = 1L, week_days) {
  if(missing(week_days)) week_days <- 7L
  fc <- ts(
    data = rep(NA, length(x)), 
    start = start(x), 
    frequency = frequency(x)
  )
  i_fc <- seq_len(frequency(x) * week_days * weeks)
  fc[i_fc] <- x[i_fc]
  fc
}

# not enough data to run an example for 1 week
# three days only
weeks <- 1L
week_days <- 3L
fc <- fill_first_periods(dfts, weeks = weeks, week_days) 
n <- length(fc)

i_last <- length(fc[!is.na(fc)])
h <- frequency(fc)
curr_start <- start(fc)
curr_end <- c(curr_start[1] + weeks*week_days - 1L, frequency(fc))
for(i in 2:(end(fc)[1] - 1L)) {
  if(n - i_last < h) {
    h <- end(fc)[2]
    i_fc <- tail(seq_len(n), h)
  } else {
    i_fc <- (i_last + 1L):(i_last + h)
    i_last <- i_last + h
  }
  w <- window(dfts, start = curr_start, end = curr_end)
  modArima <- auto.arima(w)
  fc[i_fc] <- forecast(modArima, h = h)$mean
  #
  curr_start[1] <- curr_start[1L] + 1L
  curr_end <- c(curr_end[1L] + 1L, h)
}

plot(cbind(dfts, fc))

创建于2022年12月21日,使用reprex v2.0.2

相关问题