【问题标题】:For Loop Auto Arima by customerFor Loop Auto Arima by 客户
【发布时间】:2020-11-18 17:11:09
【问题描述】:

我正在合作开展一个项目,该项目需要我使用 R,但迄今为止我还没有任何经验。我正在尝试将自动 arima 应用于我的数据集中的分区/窗口,但我什至不知道如何开始。

基本上,我想使用行 c_id = "none" 在每个 partner_id 上训练一个单独的模型,然后预测/预测每个 partner_id 的最大值(日期)。每个合作伙伴的月数/行数长度不同。对于下面粘贴的这个示例数据框,partner_id = "1A9" 有 12 个月/行,c_id = "none" 而 partner_id = "1B9" 有 13 个月/行,c_id = "none"。每个 partner_id 中扩展到 max(Date) 的月数/行数也有所不同。我基本上想将我的预测和可能的预测间隔加入到我的原始数据帧中。

我已经启动了一些代码,但我不断收到错误消息“if (frequency > 1 && 0

predictions_df <- data.frame(c_id = character(),
                         partner_id = character(),
                         rev_month = character(),
                         rev = double())

partners <- unique(df$partner_id) 

for (i in 1:length(partners)) {
 x1 <- df[df$partner_id %in% partners, ] # likely redundant since I
 x1_train <- x1[x1$c_id == "No-Contract", c(3, 4)] # training data
 x1_test <- x1[x1$c_id != "No-Contract", c(3, 4)] # forecast data
 c_int <- x1[x1$c_id != "No-Contract", 1] # confidence interval data?

# convert training data to time-series object
x1_train_ts <- xts::xts(x1_train$rev, order.by=x1_train$rev_month)
# run auto arima on the time series
tm <- forecast::auto.arima(x1_train_ts, approximation = FALSE, biasadj = TRUE, stepwise = FALSE)
# forecast the number of future steps (rows for to predict data)
fc <- forecast::forecast(tm, nrow(x1_test))

# append predictions back to dataframe
predictions_df <- rbind(predictions_df, data.frame(ar_name, partner_id, rev_month = as.character(x1_test$rev_month),   rev = as.double(fc$mean)))

我在下面包含了一个示例数据集和我的代码。

test <- data.frame("c_id" = c("none","none","none","none","none",
"none","none","none","none","none","none","none","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101", "none","none","none","none","none","none","none","none","none","none","none","none","none","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111"), "partner_id" = c("1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9"), "rev_month" = as.Date(c("2015-12-01","2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01","2016-06-01","2016-07-01","2016-08-01", "2016-09-01","2016-10-01","2016-11-01","2016-12-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01","2017-09-01","2017-10-01","2017-11-01","2017-12-01","2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01","2018-09-01","2018-10-01","2018-11-01","2018-12-01", "2017-01-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01", "2017-09-01","2017-10-01","2017-11-01","2017-12-01","2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01","2018-09-01","2018-10-01","2018-11-01","2018-12-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01","2019-05-01","2019-06-01","2019-07-01","2019-08-01","2019-09-01","2019-10-01","2019-11-01","2019-12-01", "2020-01-01", "2020-02-01", "2020-03-01")), "rev" = c(101.25, 102.25, 103.50, 103.75, 104.15, 104.25, 104.3, 105.00, 105.20, 105.60, 106.00, 106.10, 106.50, 101.50, 100.30, 107.50, 108.30, 108.45, 109.10, 110.10, 112.15, 112.45, 114.65, 115.00, 116.00, 116.50, 117.25, 117.85, 119.25, 119.95, 120.20, 121.50, 122.30, 122.40, 123.25, 123.75, 124.00, 101.25, 102.25, 103.50, 103.75, 104.15, 104.25, 104.3, 105.00, 105.20, 105.60, 106.00, 106.10, 106.50, 101.50, 100.30, 107.50, 108.30, 108.45, 109.10, 110.10, 112.15, 112.45, 114.65, 115.00, 116.00, 116.50, 117.25, 117.85, 119.25, 119.95, 120.20, 121.50, 122.30, 122.40, 123.25, 123.75, 124.00, 124.10, 125.35, 125.45), stingsAsFactors=FALSE)

如果有更好的方法来写这篇文章,我很乐意,但我非常渴望今天完成这项工作。任何帮助是极大的赞赏。提前谢谢你。

【问题讨论】:

    标签: r loops arima


    【解决方案1】:

    备注要点:

    1. 如代码中所述,您必须确保您的数据是干净的,并且按时间顺序为每个“partner_id”排序。
    2. 请记住,如果您有一个大型数据集,请尝试使用“auto.arima”函数的默认参数。
    3. 此外,请确保您有足够的数据以获得更可靠的估计。例如,在您提供的示例数据集中,您的预测范围大于您的训练长度。
    4. 您应该能够计算验证集中的样本内和样本外预测准确度(RMSE、MAE、MAPE 等)(如果您有足够的数据,您应该将其划分为训练集和验证集,使用 70-80/30-20 混合物)。

    这是我的答案:

    # Load libraries
    library(forecast)
    library(dplyr)
    
    # Load the dataset - the one you provided for these purposes
    c_id <- c("none","none","none","none","none","none","none","none","none","none","none","none","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-100","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101","c-101", "none","none","none","none","none","none","none","none","none","none","none","none","none","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-110","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111","c-111")
    partner_id <- c("1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1A9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9","1B9")
    rev_month <- as.Date(c("2015-12-01","2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01","2016-06-01","2016-07-01","2016-08-01", "2016-09-01","2016-10-01","2016-11-01","2016-12-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01","2017-09-01","2017-10-01","2017-11-01","2017-12-01","2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01","2018-09-01","2018-10-01","2018-11-01","2018-12-01", "2017-01-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01","2017-05-01","2017-06-01","2017-07-01","2017-08-01", "2017-09-01","2017-10-01","2017-11-01","2017-12-01","2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01","2018-09-01","2018-10-01","2018-11-01","2018-12-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01","2019-05-01","2019-06-01","2019-07-01","2019-08-01","2019-09-01","2019-10-01","2019-11-01","2019-12-01", "2020-01-01", "2020-02-01", "2020-03-01"))
    rev <- c(101.25, 102.25, 103.50, 103.75, 104.15, 104.25, 104.3, 105.00, 105.20, 105.60, 106.00, 106.10, 106.50, 101.50, 100.30, 107.50, 108.30, 108.45, 109.10, 110.10, 112.15, 112.45, 114.65, 115.00, 116.00, 116.50, 117.25, 117.85, 119.25, 119.95, 120.20, 121.50, 122.30, 122.40, 123.25, 123.75, 124.00, 101.25, 102.25, 103.50, 103.75, 104.15, 104.25, 104.3, 105.00, 105.20, 105.60, 106.00, 106.10, 106.50, 101.50, 100.30, 107.50, 108.30, 108.45, 109.10, 110.10, 112.15, 112.45, 114.65, 115.00, 116.00, 116.50, 117.25, 117.85, 119.25, 119.95, 120.20, 121.50, 122.30, 122.40, 123.25, 123.75, 124.00, 124.10, 125.35, 125.45)
    df <- data.frame(c_id, partner_id, rev_month, rev, stringsAsFactors = FALSE)
    # Make sure that your data is sorted in cronological order
    df <- 
      df %>% 
      arrange(partner_id, rev_month)
    # Create a new column to store the predictions for each model
    df$model_predictions <- 0
    
    # Fit an ARIMA model for each partner and get n-steps forecast
    partners <- unique(df$partner_id)
    for (k in partners) {
      # Keep data for the partner of interest for each iteration
      df_iter <- df[df$partner_id == k,]
      # Get training data
      # Recall that dates don't matter as long as your data is
      # sorted in cronological order and you have one observation per date
      train_data <- df_iter[df_iter$c_id == "none", "rev"]
      # Get number of steps to forecast
      n_ahead <- nrow(df_iter[df_iter$c_id != "none",])
      # Fit the model
      model <-
        auto.arima(train_data, approximation = FALSE, biasadj = TRUE, stepwise = FALSE)
      # Forecast data for the given partner
      fcast <- as.numeric(forecast(model, h = n_ahead, biasadj = TRUE)$mean)
      # Place fitted and forecasted values in the original data frame
      predictions <- c(model$fitted, fcast)
      df[df$partner_id == k, "model_predictions"] <- predictions
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多