1

我想制作一个正则化逻辑回归模型来预测 OneR 包中发现的乳腺癌数据集中的类。我想使用 tidymodels 框架将这一切放入一个整洁的工作流程中。

library(tidymodels)
library(OneR)

#specify model
bc.lr = logistic_reg(
  mode="classification",
  penalty = tune(),
  mixture=1
) %>%
  set_engine("glmnet")

#tune penalty term using 4-fold cv
cv_splits<-vfold_cv(breastcancer,v=4,strata="Class")

#simple recipe to scale all predictors and remove observations with NAs
bc.recipe <- recipe (Class ~., data = breastcancer) %>%
  step_normalize(all_predictors()) %>%
  step_naomit(all_predictors(), all_outcomes()) %>%
  prep()

#set up a grid of tuning parameters
tuning_grid = grid_regular(penalty(range = c(0, 0.5)),
                           levels = 10,
                           original = F)

#put everything together into a workflow
bc.wkfl <- workflow() %>%
  add_recipe(bc.recipe) %>%
  add_model(bc.lr)

#model fit
tune = tune_grid(bc.wkfl,
                 resample = cv_splits,
                 grid = tuning_grid,
                 metrics = metric_set(accuracy),
                 control = control_grid(save_pred = T))


当我尝试调用 tune_grid 时出现一个奇怪的错误。

Fold1: model 1/1 (predictions): Error: Column `.row` must be length ....
4

1 回答 1

1

这里的问题是NA配方步骤对值的处理。这是您需要仔细考虑“跳过”的步骤。从那篇文章:

在进行重采样或训练/测试拆分时,某些操作对于用于建模的数据是有意义的,但对于新样本或测试集是有问题的。

library(tidymodels)
#> ── Attaching packages ────────────────────────────────────────── tidymodels 0.1.0 ──
#> ✓ broom     0.5.6      ✓ recipes   0.1.12
#> ✓ dials     0.0.6      ✓ rsample   0.0.6 
#> ✓ dplyr     0.8.5      ✓ tibble    3.0.1 
#> ✓ ggplot2   3.3.0      ✓ tune      0.1.0 
#> ✓ infer     0.5.1      ✓ workflows 0.1.1 
#> ✓ parsnip   0.1.1      ✓ yardstick 0.0.6 
#> ✓ purrr     0.3.4
#> ── Conflicts ───────────────────────────────────────────── tidymodels_conflicts() ──
#> x purrr::discard()  masks scales::discard()
#> x dplyr::filter()   masks stats::filter()
#> x dplyr::lag()      masks stats::lag()
#> x ggplot2::margin() masks dials::margin()
#> x recipes::step()   masks stats::step()
library(OneR)

lasso_spec <- logistic_reg(penalty = tune(), mixture = 1) %>%
  set_engine("glmnet")

## cross validation split
cancer_splits <- vfold_cv(breastcancer, v = 4, strata = Class)

## preprocessing recipe (note skip = TRUE)
cancer_rec <- recipe(Class ~ ., data = breastcancer) %>%
  step_naomit(all_predictors(), skip = TRUE) %>%
  step_normalize(all_predictors())

## grid of tuning parameters
tuning_grid <- grid_regular(penalty(),
                            levels = 10)

## put everything together into a workflow
cancer_wf <- workflow() %>%
  add_recipe(cancer_rec) %>%
  add_model(lasso_spec)

## fit
cancer_res <- tune_grid(
  cancer_wf,
  resamples = cancer_splits,
  grid = tuning_grid,
  control = control_grid(save_pred = TRUE)
)

cancer_res
#> #  4-fold cross-validation using stratification 
#> # A tibble: 4 x 5
#>   splits            id    .metrics          .notes           .predictions       
#>   <list>            <chr> <list>            <list>           <list>             
#> 1 <split [523/176]> Fold1 <tibble [20 × 4]> <tibble [0 × 1]> <tibble [1,760 × 6…
#> 2 <split [524/175]> Fold2 <tibble [20 × 4]> <tibble [0 × 1]> <tibble [1,750 × 6…
#> 3 <split [525/174]> Fold3 <tibble [20 × 4]> <tibble [0 × 1]> <tibble [1,740 × 6…
#> 4 <split [525/174]> Fold4 <tibble [20 × 4]> <tibble [0 × 1]> <tibble [1,740 × 6…

reprex 包(v0.3.0)于 2020-05-14 创建

请注意,设置skip = TRUE允许您以NA适当的方式处理新数据的值。

于 2020-05-14T23:53:14.103 回答