当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


R rsample nested_cv 嵌套或双重重采样

nested_cv 可用于获取一个重采样过程的结果,并在每次分割内进行进一步的重采样。可以使用 rsample 中使用的任何类型的重采样。

用法

nested_cv(data, outside, inside)

参数

data

一个 DataFrame 。

outside

初始重采样规范。这可以是已创建的对象或新对象的表达式(请参见下面的示例)。如果使用后者,则不需要指定 data 参数,如果给出,则将被忽略。

inside

要在初始过程中进行的重采样类型的表达式。

带有 nested_cv 类和外部重采样过程通常包含的任何其他类的 tibble。结果包括外部数据拆分对象的列、一个或多个 id 列以及一列名为 inner_resamples 的嵌套 tibbles 列(带有附加重新采样)。

细节

使用引导作为外部重采样过程是一个坏主意(请参见下面的示例)

例子

## Using expressions for the resampling procedures:
nested_cv(mtcars, outside = vfold_cv(v = 3), inside = bootstraps(times = 5))
#> # Nested resampling:
#> #  outer: 3-fold cross-validation
#> #  inner: Bootstrap sampling
#> # A tibble: 3 × 3
#>   splits          id    inner_resamples
#>   <list>          <chr> <list>         
#> 1 <split [21/11]> Fold1 <boot [5 × 2]> 
#> 2 <split [21/11]> Fold2 <boot [5 × 2]> 
#> 3 <split [22/10]> Fold3 <boot [5 × 2]> 

## Using an existing object:
folds <- vfold_cv(mtcars)
nested_cv(mtcars, folds, inside = bootstraps(times = 5))
#> # Nested resampling:
#> #  outer: `folds`
#> #  inner: Bootstrap sampling
#> # A tibble: 10 × 3
#>    splits         id     inner_resamples
#>    <list>         <chr>  <list>         
#>  1 <split [28/4]> Fold01 <boot [5 × 2]> 
#>  2 <split [28/4]> Fold02 <boot [5 × 2]> 
#>  3 <split [29/3]> Fold03 <boot [5 × 2]> 
#>  4 <split [29/3]> Fold04 <boot [5 × 2]> 
#>  5 <split [29/3]> Fold05 <boot [5 × 2]> 
#>  6 <split [29/3]> Fold06 <boot [5 × 2]> 
#>  7 <split [29/3]> Fold07 <boot [5 × 2]> 
#>  8 <split [29/3]> Fold08 <boot [5 × 2]> 
#>  9 <split [29/3]> Fold09 <boot [5 × 2]> 
#> 10 <split [29/3]> Fold10 <boot [5 × 2]> 

## The dangers of outer bootstraps:
set.seed(2222)
bad_idea <- nested_cv(mtcars,
  outside = bootstraps(times = 5),
  inside = vfold_cv(v = 3)
)
#> Warning: Using bootstrapping as the outer resample is dangerous since the inner resample might have the same data point in both the analysis and assessment set.

first_outer_split <- bad_idea$splits[[1]]
outer_analysis <- as.data.frame(first_outer_split)
sum(grepl("Volvo 142E", rownames(outer_analysis)))
#> [1] 0

## For the 3-fold CV used inside of each bootstrap, how are the replicated
## `Volvo 142E` data partitioned?
first_inner_split <- bad_idea$inner_resamples[[1]]$splits[[1]]
inner_analysis <- as.data.frame(first_inner_split)
inner_assess <- as.data.frame(first_inner_split, data = "assessment")

sum(grepl("Volvo 142E", rownames(inner_analysis)))
#> [1] 0
sum(grepl("Volvo 142E", rownames(inner_assess)))
#> [1] 0
源代码:R/nest.R

相关用法


注:本文由纯净天空筛选整理自Hannah Frick等大神的英文原创作品 Nested or Double Resampling。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。