首页 文章

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

提问于
浏览
2

我的问题与caret: combine createResample and groupKFold中提出的问题非常相似

唯一的区别是:我需要在分组之后创建分层折叠(也重复10次)而不是自举重新采样(据我所知,它没有分层)使用它与插入符号's trainControl. The following code is working with 10-fold repeated CV but I couldn' t包括基于"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次的10倍CV时,我会在训练中获得一组实例,并在验证集中获得一些实例 . 我想避免这种情况,但总的来说我需要对预测值进行分层分区( df$Label ) . (具有相同"ID"的所有实例也具有相同的预测/标签值 . )

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

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

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

1 回答

  • 1

    这是一种用阻塞进行分层重复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

    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 ,它有许多不错的功能,包括阻止 - here is one answer on SO . 它有许多非常好的教程things . 很长一段时间,我以为你要么使用 caretmlr ,但它们相互补充非常好 .

相关问题