插入符号:组合分层的 createMultiFolds (repeatedCV) 和 groupKFold

caret: combine the stratified createMultiFolds (repeatedCV) and groupKFold

我的问题与

唯一的区别:我需要在分组后创建分层折叠(也重复 10 次)而不是自举重采样(据我所知没有分层),以便与插入符号的 trainControl 一起使用。 以下代码使用 10 倍重复 CV,但我无法包含基于 "ID" (df$ID) 的数据分组。

# creating indices
cv.10.folds <- createMultiFolds(rf_label, k = 10, times = 10)
# creating folds    
ctrl.10fold <- trainControl(method = "repeatedcv", number = 10, repeats = 10, index = cv.10.folds)
# train
rf.ctrl10 <- train(rf_train, y = rf_label, method = "rf", tuneLength = 6,
                       ntree = 1000, trControl = ctrl.10fold, importance = TRUE)

这是我的实际问题:我的数据包含许多组,每个组由 20 个实例组成,具有相同的 "ID"。因此,当使用 10 倍 CV 重复 10 次时,我在训练中得到了一组实例,在验证集中得到了一些实例。我想避免这种情况,但总的来说,我需要对预测值进行分层分区 (df$Label)。 (具有相同 "ID" 的所有实例也具有相同的 prediction/label 值。)

在上面 link 提供和接受的答案中(见下面的部分)我想我必须修改 folds2 行以包含分层的 10 倍 CV 而不是自举

folds <- groupKFold(x)
folds2 <- lapply(folds, function(x) lapply(1:10, function(i) sample(x, size = length(x), replace = TRUE)))

但不幸的是我无法弄清楚到底是怎么回事。你能帮我吗?

这是一种使用分块执行分层重复 K 折 CV 的方法。

library(caret)
library(tidyverse)

一些虚假数据,其中 id 将作为阻止因素:

id <- sample(1:55, size = 1000, replace = T)
y <- rnorm(1000)
x <- matrix(rnorm(10000), ncol = 10)
df <- data.frame(id, y, x)

按区组因子总结观察结果:

df %>%
  group_by(id) %>%
  summarise(mean = mean(y)) %>%
  ungroup() -> groups1 

根据分组数据创建分层折叠:

folds <- createMultiFolds(groups1$mean, 10, 3)

将原始 df 返回加入组数据并获取 df 行 id's

folds <- lapply(folds, function(i){
  data.frame(id = i) %>%
    left_join(df %>%
                rowid_to_column()) %>%
    pull(rowid) 
})

检查测试中的数据 ID 是否不在序列中:

lapply(folds, function(i){
  sum(df[i,1] %in% df[-i,1])
})

输出是一串零,这意味着测试折叠中没有 id 在训练折叠中。

如果您的组 ID 不是数字,则有两种方法可以实现此目的:
1 将它们转换为数字:

首先是一些数据

id <- sample(1:55, size = 1000, replace = T)
y <- rnorm(1000)
x <- matrix(rnorm(10000), ncol = 10)
df <- data.frame(id = paste0("id_", id), y, x) #factor id's

df %>%
  mutate(id = as.numeric(id)) %>% #convert to numeric
  group_by(id) %>%
  summarise(mean = mean(y)) %>%
  ungroup() -> groups1 

folds <- createMultiFolds(groups1$mean, 10, 3)

folds <- lapply(folds, function(i){
  data.frame(id = i) %>%
    left_join(df %>%
                mutate(id = as.numeric(id)) %>% #also need to convert to numeric in the original data frame
                rowid_to_column()) %>%
    pull(rowid) 
})  

2 根据折叠索引过滤分组数据中的id,然后按id连接

df %>%
  group_by(id) %>%
  summarise(mean = mean(y)) %>%
  ungroup() -> groups1 

folds <- createMultiFolds(groups1$mean, 10, 3)

folds <- lapply(folds, function(i){
  groups1 %>% #start from grouped data
    select(id) %>% #select id's
    slice(i) %>% #filter id's according to fold index
    left_join(df %>% #join by id 
               rowid_to_column()) %>%
    pull(rowid) 
})

它对插入符号有效吗?

ctrl.10fold <- trainControl(method = "repeatedcv", number = 10, repeats = 3, index = folds)

rf.ctrl10 <- train(x = df[,-c(1:2)], y = df$y, data = df, method = "rf", tuneLength = 1,
                   ntree = 20, trControl = ctrl.10fold, importance = TRUE)

rf.ctrl10$results
#output
  mtry     RMSE    Rsquared       MAE     RMSESD  RsquaredSD      MAESD
1    3 1.041641 0.007534611 0.8246514 0.06953668 0.009488169 0.05934975

此外,我建议您查看库 mlr,它有许多不错的功能,包括阻塞 - . It has very nice tutorials on many things。很长一段时间以来,我认为您要么使用 caret 要么使用 mlr,但它们可以很好地互补。