指定要在 caret::train 函数中使用的留一法(jack-knife)交叉验证中使用的选定数据范围
Specifiying a selected range of data to be used in leave-one-out (jack-knife) cross-validation for use in the caret::train function
这个问题基于我在这里提出的问题:)。
我正在处理的数据如下所示:
df <- data.frame(Effect = rep(seq(from = 0.05, to = 1, by = 0.05), each = 5), Time = rep(c(1:20,1:20), each = 5), Replicate = c(1:5))
基本上我想做的是创建自定义分区,就像由 caret::groupKFold
函数生成的分区一样,但这些折叠要超过指定范围(即 > 15 天)并且每次折叠要与-保留一个点作为测试集,所有其他数据用于训练。这将在每次迭代中重复,直到指定范围内的每个点都被用作测试集。 @Missuse 为此编写了一些代码,它接近上面 link 中此问题的所需输出。
我会尝试向您展示所需的输出,但老实说,caret::groupKFold 函数的输出让我感到困惑,所以希望以上描述就足够了。不过很高兴尝试澄清!
这是一种使用 tidyverse
创建所需分区的方法:
library(tidyverse)
df %>%
mutate(id = row_number()) %>% #create a column called id which will hold the row numbers
filter(Time > 15) %>% #subset data frame according to your description
split(.$id) %>% #split the data frame into lists by id (row number)
map(~ .x %>% select(id) %>% #clean up so it works with indexOut argument in trainControl
unlist %>%
unname) -> folds_cv
编辑:似乎 indexOut
参数没有按预期执行,但是 index
参数在使 folds_cv
之后执行,可以使用 setdiff
得到逆:
folds_cv <- lapply(folds_cv, function(x) setdiff(1:nrow(df), x))
现在:
test_control <- trainControl(index = folds_cv,
savePredictions = "final")
quad.lm2 <- train(Time ~ Effect,
data = df,
method = "lm",
trControl = test_control)
带有警告:
Warning message:
In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, :
There were missing values in resampled performance measures.
> quad.lm2
Linear Regression
200 samples
1 predictor
No pre-processing
Resampling: Bootstrapped (50 reps)
Summary of sample sizes: 199, 199, 199, 199, 199, 199, ...
Resampling results:
RMSE Rsquared MAE
3.552714e-16 NaN 3.552714e-16
Tuning parameter 'intercept' was held constant at a value of TRUE
所以每次重新采样使用 199 行并预测 1,重复我们想要一次保留的所有 50 行。这可以在以下方面得到验证:
quad.lm2$pred
为什么缺少 Rsquared
我不确定我是否会深入挖掘。
这个问题基于我在这里提出的问题:
我正在处理的数据如下所示:
df <- data.frame(Effect = rep(seq(from = 0.05, to = 1, by = 0.05), each = 5), Time = rep(c(1:20,1:20), each = 5), Replicate = c(1:5))
基本上我想做的是创建自定义分区,就像由 caret::groupKFold
函数生成的分区一样,但这些折叠要超过指定范围(即 > 15 天)并且每次折叠要与-保留一个点作为测试集,所有其他数据用于训练。这将在每次迭代中重复,直到指定范围内的每个点都被用作测试集。 @Missuse 为此编写了一些代码,它接近上面 link 中此问题的所需输出。
我会尝试向您展示所需的输出,但老实说,caret::groupKFold 函数的输出让我感到困惑,所以希望以上描述就足够了。不过很高兴尝试澄清!
这是一种使用 tidyverse
创建所需分区的方法:
library(tidyverse)
df %>%
mutate(id = row_number()) %>% #create a column called id which will hold the row numbers
filter(Time > 15) %>% #subset data frame according to your description
split(.$id) %>% #split the data frame into lists by id (row number)
map(~ .x %>% select(id) %>% #clean up so it works with indexOut argument in trainControl
unlist %>%
unname) -> folds_cv
编辑:似乎 indexOut
参数没有按预期执行,但是 index
参数在使 folds_cv
之后执行,可以使用 setdiff
得到逆:
folds_cv <- lapply(folds_cv, function(x) setdiff(1:nrow(df), x))
现在:
test_control <- trainControl(index = folds_cv,
savePredictions = "final")
quad.lm2 <- train(Time ~ Effect,
data = df,
method = "lm",
trControl = test_control)
带有警告:
Warning message:
In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, :
There were missing values in resampled performance measures.
> quad.lm2
Linear Regression
200 samples
1 predictor
No pre-processing
Resampling: Bootstrapped (50 reps)
Summary of sample sizes: 199, 199, 199, 199, 199, 199, ...
Resampling results:
RMSE Rsquared MAE
3.552714e-16 NaN 3.552714e-16
Tuning parameter 'intercept' was held constant at a value of TRUE
所以每次重新采样使用 199 行并预测 1,重复我们想要一次保留的所有 50 行。这可以在以下方面得到验证:
quad.lm2$pred
为什么缺少 Rsquared
我不确定我是否会深入挖掘。