这是一种通过阻塞执行分层重复 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
,它有许多不错的功能,包括阻塞 -这是关于SO的一个答案 https://stackoverflow.com/questions/40422377/how-can-a-blocking-factor-be-included-in-makeclassiftask-from-mlr-package。它在很多方面都有非常好的教程things https://mlr-org.github.io/mlr-tutorial/devel/html/index.html。很长一段时间我认为你要么使用caret
or mlr
但它们非常互补。