【问题标题】:Month by month rolling CV in mlr3在 mlr3 中逐月滚动 CV
【发布时间】:2023-01-26 18:43:26
【问题描述】:

我的目标是使用使用某种滚动 CV 的 mlr3 包创建重采样。更具体地说,我想在训练集中使用 n 个月的数据(比如 6 个月),在测试集中使用一个月的数据。

这是我的数据集的示例:

DT <- structure(list(Not_FLS_positive = c(0.408197129345391, 0.765784452003651, 
                                          0.44694266987472, 0.261843524433751, 0.823612378660914, 0.463701982908819, 
                                          0.50286235791919, 0.202937028125778, 0.728864183190907, 0.396498796980005, 
                                          0.0645482452501452, 0.386210901850162, 0.518874968887414, 0.748527337592301, 
                                          0.453414087778976, 0.758566332033519, 0.544926574296856, 0.758151497552477, 
                                          0.641583008379657, 0.15000414834481, 0.271384717497718, 0.516634862689787, 
                                          0.379988384634531, 0.220277109433336, 0.368373019165353, 0.367294449514644, 
                                          0.924583091346553, 0.702895544677674, 0.560192483199204, 0.61212976022567, 
                                          0.0189164523355181, 0.308139052518045), Not_FLS_negative = c(0.690284576453995, 
                                                                                                       0.406288890732598, 0.965402804281092, 0.981830249730358, 0.750850410686136, 
                                                                                                       0.884676014270306, 0.978760474570646, 0.846013440637186, 0.319754417987223, 
                                                                                                       0.70256367709284, 0.0308636853895296, 0.247905085870738, 0.886999087364142, 
                                                                                                       0.28017920849581, 0.697253795735502, 0.720069692192815, 0.838131585497387, 
                                                                                                       0.967559943582511, 0.755745457562433, 0.97593960009956, 0.886833153571725, 
                                                                                                       0.587156724466938, 0.959097320169252, 0.0548411183937609, 0.957769849829918, 
                                                                                                       0.479382726292209, 0.626897867750767, 0.772670704388949, 0.9822450842114, 
                                                                                                       0.736829005226914, 0.420642163776653, 0.723886169418402), bin_aroundzero_ret_excess_stand_22 = structure(c(2L, 
                                                                                                                                                                                                                  1L, 3L, 1L, 1L, 3L, 1L, 1L, 2L, 2L, 2L, 1L, 3L, 1L, 2L, 2L, 1L, 
                                                                                                                                                                                                                  1L, 1L, 3L, 2L, 1L, 3L, 2L, 2L, 2L, 3L, 2L, 1L, 2L, 3L, 2L), levels = c("0", 
                                                                                                                                                                                                                                                                                          "1", "-1"), class = "factor"), monthid = c("20141", "20141", 
                                                                                                                                                                                                                                                                                                                                     "20141", "20141", "20141", "20141", "20141", "20141", "20141", 
                                                                                                                                                                                                                                                                                                                                     "20141", "20142", "20142", "20142", "20142", "20142", "20142", 
                                                                                                                                                                                                                                                                                                                                     "20142", "20142", "20142", "20142", "20142", "20143", "20143", 
                                                                                                                                                                                                                                                                                                                                     "20143", "20143", "20143", "20143", "20143", "20143", "20143", 
                                                                                                                                                                                                                                                                                                                                     "20143", "20143")), row.names = c(NA, -32L), class = c("data.table", 
                                                                                                                                                                                                                                                                                                                                                                                            "data.frame"))


现在,我想在训练集中使用 20141 和 20142 月份,在测试集中使用 20143 月份。我认为最好的方法是设置 monthid 组角色:

task <- as_task_classif(DT, id = "aroundzero", target = "bin_aroundzero_ret_excess_stand_22")
task$set_col_roles("monthid", "group")

现在使用mlr3temporal包中的ResamplingRollingWindowCV

resampling = rsmp("forecast_cv", folds = 5, fixed_window = TRUE, horizon = 1L, window_size = 6)
resampling$instantiate(task)

但这会返回一个错误:

Error in max(ids) - self$param_set$values$horizon : 
  non-numeric argument to binary operator

然后我尝试使用自定义重采样:

custom = rsmp("custom")
train_sets = list(1:2)
test_sets = list(3)
custom$instantiate(task, train_sets, test_sets)
custom$train_set(1)
custom$test_set(1)

但这只返回训练集中的 1,2 和测试集中的 3。它似乎不使用群体 bt 个人观察。 是否可以在当前的重采样方案中按月调整我的简历,或者我应该为它创建全新的类?

【问题讨论】:

    标签: r cross-validation mlr3


    【解决方案1】:

    ResamplingForecastCV 不支持分组。您可以尝试使用Leave-time-out resampling 并使用"time" 行角色标记monthid。但是使用这种方法,您还可以预测过去的几个月,例如训练集是2014320142,测试集是20141。如果这对您不起作用,您需要使用ResamplingCustom

    这仅返回训练集中的 1,2 和测试集中的 3

    这是正确的行为。您传递任务的行 ID。您必须先自己进行分组,然后将分组的 ID 传递给重采样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2019-09-05
      • 1970-01-01
      • 2021-09-13
      • 2022-06-29
      • 1970-01-01
      相关资源
      最近更新 更多