sweep function generating error in predict() when only 1 row in test split

Can I just check something please. I am moving over a piece of work from caret to tidymodels. I am running a random forest model on my data and then predicting on the test dataset that contains 1 row of predictors.

In caret this runs fine but in tidymodels I am getting the error below;

Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
In addition: Warning message:
In sweep(res[, names(object$sds)], 2, object$sds, "/") :
STATS is longer than the extent of 'dim(x)[MARGIN]'

This error goes away if I duplicate the test data so I have 2 rows in my test dataset. Is there a limitation within tidymodels that you need to predict on >1 rows of predictors? If not then I'll put together a full reprex and send through.

Digging further, the error is during the predict.workflow function "forge_predictors", specifically at the final process below;

mold <- extract_mold(fittedWorkflow)
blueprint <- mold$blueprint
  
 forge <- hardhat::forge(testData, blueprint = mold$blueprint)
  
  cleaned <- blueprint$forge$clean(blueprint = blueprint, new_data = testData, outcomes = TRUE)
  blueprint <- cleaned$blueprint
  predictors <- cleaned$predictors
  outcomes <- cleaned$outcomes
  extras <- cleaned$extras
  processed <- blueprint$forge$process(blueprint = blueprint, 
                                       predictors = predictors, 
                                       outcomes = outcomes, 
                                       extras = extras)
Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
In addition: Warning message:
In sweep(res[, names(object$sds)], 2, object$sds, "/") :
  STATS is longer than the extent of 'dim(x)[MARGIN]'

My original recipe contained a step_normalize(all_predictors). Removing this from the recipe means the predict() now works and the error does not appear.

Seem therefore to be an issue with forge() when you have a preprocess recipe step and only 1 row of new_data to predict on.

Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you!

If you've never heard of a reprex before, start by reading "What is a reprex", and follow the advice further down that page.

library(tidymodels)

data("car_prices")

cars_split <- car_prices %>%
  dplyr::select(Price, Mileage, Cylinder, Doors) %>%
  rsample::initial_time_split(prop = 1 - (1/NROW(car_prices)))

carsTrain <- rsample::training(cars_split)
carsTest <- rsample::testing(cars_split)

model_rf <-
  parsnip::rand_forest(
    mode = "regression",
    mtry = tune(),
    trees = tune(),
    min_n = tune()
  ) %>%
  parsnip::set_engine("ranger")

cars_folds <-
  rsample::vfold_cv(data = carsTrain, v = 5, repeats = 1)

cars_recipe <-  carsTrain %>%
  recipes::recipe(Price ~ .) %>% 
  recipes::step_normalize(all_predictors())

cars_wflow <-
  workflows::workflow() %>%
  workflows::add_recipe(cars_recipe) %>%
  workflows::add_model(model_rf)

set.seed(123)
cars_fit <-
  cars_wflow %>% tune::tune_grid(
    resamples = cars_folds,
    metrics = yardstick::metric_set(yardstick::mae),
    control = tune::control_grid(save_pred = TRUE)
  )
#> i Creating pre-processing data to finalize unknown parameter: mtry
cars_fitFinalParams <- 
  tune::select_best(cars_fit)

model_rfFinal <- 
  finalize_model(model_rf, cars_fitFinalParams)

cars_fitFinal <-
  fit(cars_wflow %>%
        update_model(model_rfFinal),
      carsTrain)

cars_fitFinalPred <-
  predict(cars_fitFinal, new_data = carsTest)
#> Warning in sweep(res[, names(object$sds)], 2, object$sds, "/"): STATS is longer
#> than the extent of 'dim(x)[MARGIN]'
#> Error in array(STATS, dims[perm]): 'dims' cannot be of length 0

Created on 2022-06-21 by the reprex package (v2.0.1)

Thanks for the reprex, @nealec!

I don't see this error when I run your reprex, but some recent changes we've made to our packages may have addressed this issue.

Could you update your installs of core tidymodels packages with install.packages(c("parsnip", "recipes", "tune", "workflows", "yardstick", "hardhat")), restart R, and try to re-run? If you continue to see this error, could you share your sessioninfo::session_info()?

EDIT: clarify install code

Hi Simon, still there I'm afraid. Regards, Chris

library(tidymodels)

data("car_prices")

cars_split <- car_prices %>%
  dplyr::select(Price, Mileage, Cylinder, Doors) %>%
  rsample::initial_time_split(prop = 1 - (1/NROW(car_prices)))

carsTrain <- rsample::training(cars_split)
carsTest <- rsample::testing(cars_split)

model_rf <-
  parsnip::rand_forest(
    mode = "regression",
    mtry = tune(),
    trees = tune(),
    min_n = tune()
  ) %>%
  parsnip::set_engine("ranger")

cars_folds <-
  rsample::vfold_cv(data = carsTrain, v = 5, repeats = 1)

cars_recipe <-  carsTrain %>%
  recipes::recipe(Price ~ .) %>% 
  recipes::step_normalize(all_predictors())

cars_wflow <-
  workflows::workflow() %>%
  workflows::add_recipe(cars_recipe) %>%
  workflows::add_model(model_rf)

set.seed(123)
cars_fit <-
  cars_wflow %>% tune::tune_grid(
    resamples = cars_folds,
    metrics = yardstick::metric_set(yardstick::mae),
    control = tune::control_grid(save_pred = TRUE)
  )
#> i Creating pre-processing data to finalize unknown parameter: mtry
cars_fitFinalParams <- 
  tune::select_best(cars_fit)

model_rfFinal <- 
  finalize_model(model_rf, cars_fitFinalParams)

cars_fitFinal <-
  fit(cars_wflow %>%
        update_model(model_rfFinal),
      carsTrain)

cars_fitFinalPred <-
  predict(cars_fitFinal, new_data = carsTest)
#> Warning in sweep(res[, names(object$sds)], 2, object$sds, "/"): STATS is longer
#> than the extent of 'dim(x)[MARGIN]'
#> Error in array(STATS, dims[perm]): 'dims' cannot be of length 0
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.5 (2021-03-31)
#>  os       macOS Big Sur 10.16         
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_GB.UTF-8                 
#>  ctype    en_GB.UTF-8                 
#>  tz       Europe/London               
#>  date     2022-06-21                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date       lib source                               
#>  assertthat     0.2.1      2019-03-21 [1] CRAN (R 4.0.2)                       
#>  backports      1.2.1      2020-12-09 [1] CRAN (R 4.0.2)                       
#>  broom        * 0.8.0      2022-04-13 [1] CRAN (R 4.0.5)                       
#>  class          7.3-19     2021-05-03 [1] CRAN (R 4.0.2)                       
#>  cli            3.3.0      2022-04-25 [1] standard (@3.3.0)                    
#>  codetools      0.2-18     2020-11-04 [1] CRAN (R 4.0.5)                       
#>  colorspace     2.0-2      2021-06-24 [1] CRAN (R 4.0.2)                       
#>  crayon         1.4.1      2021-02-08 [1] CRAN (R 4.0.2)                       
#>  DBI            1.1.1      2021-01-15 [1] CRAN (R 4.0.2)                       
#>  dials        * 1.0.0      2022-06-14 [1] CRAN (R 4.0.5)                       
#>  DiceDesign     1.9        2021-02-13 [1] CRAN (R 4.0.2)                       
#>  digest         0.6.28     2021-09-23 [1] CRAN (R 4.0.2)                       
#>  dplyr        * 1.0.8      2022-02-08 [1] standard (@1.0.8)                    
#>  ellipsis       0.3.2      2021-04-29 [1] CRAN (R 4.0.2)                       
#>  evaluate       0.14       2019-05-28 [1] CRAN (R 4.0.1)                       
#>  fansi          0.5.0      2021-05-25 [1] CRAN (R 4.0.2)                       
#>  fastmap        1.1.0      2021-01-25 [1] CRAN (R 4.0.2)                       
#>  foreach        1.5.1      2020-10-15 [1] CRAN (R 4.0.2)                       
#>  fs             1.5.0      2020-07-31 [1] CRAN (R 4.0.2)                       
#>  furrr          0.2.3      2021-06-25 [1] CRAN (R 4.0.2)                       
#>  future         1.22.1     2021-08-25 [1] CRAN (R 4.0.2)                       
#>  future.apply   1.8.1      2021-08-10 [1] CRAN (R 4.0.2)                       
#>  generics       0.1.2      2022-01-31 [1] standard (@0.1.2)                    
#>  ggplot2      * 3.3.5      2021-06-25 [1] CRAN (R 4.0.2)                       
#>  globals        0.14.0     2020-11-22 [1] CRAN (R 4.0.2)                       
#>  glue           1.6.2      2022-02-24 [1] standard (@1.6.2)                    
#>  gower          0.2.2      2020-06-23 [1] CRAN (R 4.0.2)                       
#>  GPfit          1.0-8      2019-02-08 [1] CRAN (R 4.0.2)                       
#>  gtable         0.3.0      2019-03-25 [1] CRAN (R 4.0.2)                       
#>  hardhat        1.1.0      2022-06-10 [1] CRAN (R 4.0.5)                       
#>  highr          0.9        2021-04-16 [1] CRAN (R 4.0.2)                       
#>  htmltools      0.5.2      2021-08-25 [1] CRAN (R 4.0.2)                       
#>  infer        * 1.0.0      2021-08-13 [1] CRAN (R 4.0.2)                       
#>  ipred          0.9-12     2021-09-15 [1] CRAN (R 4.0.2)                       
#>  iterators      1.0.13     2020-10-15 [1] CRAN (R 4.0.2)                       
#>  knitr          1.34       2021-09-09 [1] CRAN (R 4.0.2)                       
#>  lattice        0.20-44    2021-05-02 [1] CRAN (R 4.0.2)                       
#>  lava           1.6.10     2021-09-02 [1] CRAN (R 4.0.2)                       
#>  lhs            1.1.3      2021-09-08 [1] CRAN (R 4.0.2)                       
#>  lifecycle      1.0.1      2021-09-24 [1] standard (@1.0.1)                    
#>  listenv        0.8.0      2019-12-05 [1] CRAN (R 4.0.2)                       
#>  lubridate      1.7.10     2021-02-26 [1] CRAN (R 4.0.2)                       
#>  magrittr       2.0.1      2020-11-17 [1] CRAN (R 4.0.2)                       
#>  MASS           7.3-54     2021-05-03 [1] CRAN (R 4.0.2)                       
#>  Matrix         1.3-4      2021-06-01 [1] CRAN (R 4.0.2)                       
#>  modeldata    * 0.1.1.9000 2022-05-09 [1] Github (tidymodels/modeldata@78d1b89)
#>  munsell        0.5.0      2018-06-12 [1] CRAN (R 4.0.2)                       
#>  nnet           7.3-16     2021-05-03 [1] CRAN (R 4.0.2)                       
#>  parallelly     1.28.1     2021-09-09 [1] CRAN (R 4.0.2)                       
#>  parsnip      * 0.2.1.9001 2022-05-09 [1] Github (tidymodels/parsnip@284252b)  
#>  pillar         1.7.0      2022-02-01 [1] CRAN (R 4.0.5)                       
#>  pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.0.2)                       
#>  prodlim        2019.11.13 2019-11-17 [1] CRAN (R 4.0.2)                       
#>  purrr        * 0.3.4      2020-04-17 [1] CRAN (R 4.0.2)                       
#>  R6             2.5.1      2021-08-19 [1] CRAN (R 4.0.2)                       
#>  ranger       * 0.13.1     2021-07-14 [1] CRAN (R 4.0.2)                       
#>  Rcpp           1.0.7      2021-07-07 [1] CRAN (R 4.0.2)                       
#>  recipes      * 0.2.0.9001 2022-05-09 [1] Github (tidymodels/recipes@ce1c657)  
#>  reprex         2.0.1      2021-08-05 [1] CRAN (R 4.0.2)                       
#>  rlang          1.0.2      2022-03-04 [1] standard (@1.0.2)                    
#>  rmarkdown      2.11       2021-09-14 [1] CRAN (R 4.0.2)                       
#>  rpart          4.1-15     2019-04-12 [1] CRAN (R 4.0.5)                       
#>  rsample      * 0.1.1      2021-11-08 [1] CRAN (R 4.0.2)                       
#>  rstudioapi     0.13       2020-11-12 [1] CRAN (R 4.0.2)                       
#>  scales       * 1.1.1      2020-05-11 [1] CRAN (R 4.0.2)                       
#>  sessioninfo    1.1.1      2018-11-05 [1] CRAN (R 4.0.2)                       
#>  stringi        1.7.4      2021-08-25 [1] CRAN (R 4.0.2)                       
#>  stringr        1.4.0      2019-02-10 [1] CRAN (R 4.0.2)                       
#>  styler         1.6.1      2021-09-17 [1] CRAN (R 4.0.2)                       
#>  survival       3.2-13     2021-08-24 [1] CRAN (R 4.0.2)                       
#>  tibble       * 3.1.7      2022-05-03 [1] CRAN (R 4.0.5)                       
#>  tidymodels   * 0.2.0      2022-03-19 [1] CRAN (R 4.0.5)                       
#>  tidyr        * 1.2.0      2022-02-01 [1] standard (@1.2.0)                    
#>  tidyselect     1.1.2      2022-02-21 [1] standard (@1.1.2)                    
#>  timeDate       3043.102   2018-02-21 [1] CRAN (R 4.0.2)                       
#>  tune         * 0.2.0.9001 2022-05-09 [1] Github (tidymodels/tune@1856421)     
#>  utf8           1.2.2      2021-07-24 [1] CRAN (R 4.0.2)                       
#>  vctrs          0.4.1      2022-04-13 [1] standard (@0.4.1)                    
#>  withr          2.4.2      2021-04-18 [1] CRAN (R 4.0.2)                       
#>  workflows    * 0.2.6.9001 2022-05-09 [1] Github (tidymodels/workflows@9a9e231)
#>  workflowsets * 0.2.1      2022-03-15 [1] CRAN (R 4.0.5)                       
#>  xfun           0.26       2021-09-14 [1] CRAN (R 4.0.2)                       
#>  yaml           2.2.1      2020-02-01 [1] CRAN (R 4.0.2)                       
#>  yardstick    * 0.0.9.9000 2022-05-09 [1] Github (tidymodels/yardstick@e56b69f)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

Created on 2022-06-21 by the reprex package (v2.0.1)

Thanks for the quick response, @nealec.

When I try to install the package versions listed in your session info, I see a few conflicts:

> pak::pak(c("tidymodels/parsnip@284252b", "tidymodels/recipes@ce1c657", 
+            "tidymodels/tune@1856421", "tidymodels/workflows@9a9e231",
+            "tidymodels/yardstick@e56b69f"))
Error: Cannot install packages:                                                  
* tidymodels/tune@1856421:
  * Can't install dependency tidymodels/parsnip@feature/case-weights
  * Can't install dependency tidymodels/workflows
  * Can't install dependency tidymodels/yardstick
* tidymodels/workflows: Conflicts with tidymodels/workflows@9a9e231
* tidymodels/yardstick: Conflicts with tidymodels/yardstick@e56b69f
* tidymodels/parsnip@feature/case-weights: Conflicts with tidymodels/parsnip@284252b

You'll indeed need to update your tidymodels package installs.:+1:

If you'd like to continue using developmental versions, the following code will update the key players:

pak::pak(c("tidymodels/parsnip", "tidymodels/recipes", 
           "tidymodels/tune", "tidymodels/workflows",
           "tidymodels/yardstick", "tidymodels/hardhat"))

I had loaded those development versions to test cases weights - if the issue is related to those you may want to let @Max know. Either way everything is now updated and still seeing the error unfortunately .

library(tidymodels)

data("car_prices")

cars_split <- car_prices %>%
  dplyr::select(Price, Mileage, Cylinder, Doors) %>%
  rsample::initial_time_split(prop = 1 - (1/NROW(car_prices)))

carsTrain <- rsample::training(cars_split)
carsTest <- rsample::testing(cars_split)

model_rf <-
  parsnip::rand_forest(
    mode = "regression",
    mtry = tune(),
    trees = tune(),
    min_n = tune()
  ) %>%
  parsnip::set_engine("ranger")

cars_folds <-
  rsample::vfold_cv(data = carsTrain, v = 5, repeats = 1)

cars_recipe <-  carsTrain %>%
  recipes::recipe(Price ~ .) %>% 
  recipes::step_normalize(all_predictors())

cars_wflow <-
  workflows::workflow() %>%
  workflows::add_recipe(cars_recipe) %>%
  workflows::add_model(model_rf)

set.seed(123)
cars_fit <-
  cars_wflow %>% tune::tune_grid(
    resamples = cars_folds,
    metrics = yardstick::metric_set(yardstick::mae),
    control = tune::control_grid(save_pred = TRUE)
  )
#> i Creating pre-processing data to finalize unknown parameter: mtry

cars_fitFinalParams <- 
  tune::select_best(cars_fit)

model_rfFinal <- 
  finalize_model(model_rf, cars_fitFinalParams)

cars_fitFinal <-
  fit(cars_wflow %>%
        update_model(model_rfFinal),
      carsTrain)

cars_fitFinalPred <-
  predict(cars_fitFinal, new_data = carsTest)
#> Warning in sweep(res[, names(object$sds)], 2, object$sds, "/"): STATS is longer
#> than the extent of 'dim(x)[MARGIN]'
#> Error in array(STATS, dims[perm]): 'dims' cannot be of length 0

tidymodels_update()
#> All packages up-to-date

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.0.5 (2021-03-31)
#>  os       macOS Big Sur 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_GB.UTF-8
#>  ctype    en_GB.UTF-8
#>  tz       Europe/London
#>  date     2022-06-21
#>  pandoc   2.17.1.1 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date (UTC) lib source
#>  assertthat     0.2.1      2019-03-21 [1] CRAN (R 4.0.2)
#>  backports      1.4.1      2021-12-13 [1] CRAN (R 4.0.2)
#>  broom        * 0.8.0      2022-04-13 [1] CRAN (R 4.0.5)
#>  class          7.3-20     2022-01-13 [1] CRAN (R 4.0.5)
#>  cli            3.3.0      2022-04-25 [1] CRAN (R 4.0.5)
#>  codetools      0.2-18     2020-11-04 [1] CRAN (R 4.0.5)
#>  colorspace     2.0-3      2022-02-21 [1] CRAN (R 4.0.5)
#>  crayon         1.5.1      2022-03-26 [1] CRAN (R 4.0.5)
#>  DBI            1.1.3      2022-06-18 [1] CRAN (R 4.0.5)
#>  dials        * 1.0.0      2022-06-14 [1] CRAN (R 4.0.5)
#>  DiceDesign     1.9        2021-02-13 [1] CRAN (R 4.0.2)
#>  digest         0.6.29     2021-12-01 [1] CRAN (R 4.0.2)
#>  dplyr        * 1.0.9      2022-04-28 [1] CRAN (R 4.0.5)
#>  ellipsis       0.3.2      2021-04-29 [1] CRAN (R 4.0.2)
#>  evaluate       0.15       2022-02-18 [1] CRAN (R 4.0.5)
#>  fansi          1.0.3      2022-03-24 [1] CRAN (R 4.0.5)
#>  fastmap        1.1.0      2021-01-25 [1] CRAN (R 4.0.2)
#>  foreach        1.5.2      2022-02-02 [1] CRAN (R 4.0.5)
#>  fs             1.5.2      2021-12-08 [1] CRAN (R 4.0.2)
#>  furrr          0.3.0      2022-05-04 [1] CRAN (R 4.0.5)
#>  future         1.26.1     2022-05-27 [1] CRAN (R 4.0.5)
#>  future.apply   1.9.0      2022-04-25 [1] CRAN (R 4.0.5)
#>  generics       0.1.2      2022-01-31 [1] CRAN (R 4.0.5)
#>  ggplot2      * 3.3.6      2022-05-03 [1] CRAN (R 4.0.5)
#>  globals        0.15.0     2022-05-09 [1] CRAN (R 4.0.5)
#>  glue           1.6.2      2022-02-24 [1] CRAN (R 4.0.5)
#>  gower          1.0.0      2022-02-03 [1] CRAN (R 4.0.5)
#>  GPfit          1.0-8      2019-02-08 [1] CRAN (R 4.0.2)
#>  gtable         0.3.0      2019-03-25 [1] CRAN (R 4.0.2)
#>  hardhat        1.1.0      2022-06-10 [1] CRAN (R 4.0.5)
#>  highr          0.9        2021-04-16 [1] CRAN (R 4.0.2)
#>  htmltools      0.5.2      2021-08-25 [1] CRAN (R 4.0.2)
#>  infer        * 1.0.2      2022-06-10 [1] CRAN (R 4.0.5)
#>  ipred          0.9-13     2022-06-02 [1] CRAN (R 4.0.5)
#>  iterators      1.0.14     2022-02-05 [1] CRAN (R 4.0.5)
#>  knitr          1.39       2022-04-26 [1] CRAN (R 4.0.5)
#>  lattice        0.20-45    2021-09-22 [1] CRAN (R 4.0.2)
#>  lava           1.6.10     2021-09-02 [1] CRAN (R 4.0.2)
#>  lhs            1.1.5      2022-03-22 [1] CRAN (R 4.0.5)
#>  lifecycle      1.0.1      2021-09-24 [1] CRAN (R 4.0.2)
#>  listenv        0.8.0      2019-12-05 [1] CRAN (R 4.0.2)
#>  lubridate      1.8.0      2021-10-07 [1] CRAN (R 4.0.2)
#>  magrittr       2.0.3      2022-03-30 [1] CRAN (R 4.0.5)
#>  MASS           7.3-57     2022-04-22 [1] CRAN (R 4.0.5)
#>  Matrix         1.4-1      2022-03-23 [1] CRAN (R 4.0.5)
#>  modeldata    * 0.1.1.9000 2022-05-09 [1] Github (tidymodels/modeldata@78d1b89)
#>  munsell        0.5.0      2018-06-12 [1] CRAN (R 4.0.2)
#>  nnet           7.3-17     2022-01-13 [1] CRAN (R 4.0.5)
#>  parallelly     1.32.0     2022-06-07 [1] CRAN (R 4.0.5)
#>  parsnip      * 1.0.0      2022-06-16 [1] CRAN (R 4.0.5)
#>  pillar         1.7.0      2022-02-01 [1] CRAN (R 4.0.5)
#>  pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.0.2)
#>  prodlim        2019.11.13 2019-11-17 [1] CRAN (R 4.0.2)
#>  purrr        * 0.3.4      2020-04-17 [1] CRAN (R 4.0.2)
#>  R.cache        0.15.0     2021-04-30 [1] CRAN (R 4.0.2)
#>  R.methodsS3    1.8.2      2022-06-13 [1] CRAN (R 4.0.5)
#>  R.oo           1.25.0     2022-06-12 [1] CRAN (R 4.0.5)
#>  R.utils        2.11.0     2021-09-26 [1] CRAN (R 4.0.2)
#>  R6             2.5.1      2021-08-19 [1] CRAN (R 4.0.2)
#>  ranger       * 0.14.1     2022-06-18 [1] CRAN (R 4.0.5)
#>  Rcpp           1.0.8.3    2022-03-17 [1] CRAN (R 4.0.5)
#>  recipes      * 0.2.0.9001 2022-05-09 [1] Github (tidymodels/recipes@ce1c657)
#>  reprex         2.0.1      2021-08-05 [1] CRAN (R 4.0.2)
#>  rlang          1.0.2      2022-03-04 [1] CRAN (R 4.0.5)
#>  rmarkdown      2.14       2022-04-25 [1] CRAN (R 4.0.5)
#>  rpart          4.1.16     2022-01-24 [1] CRAN (R 4.0.5)
#>  rsample      * 0.1.1      2021-11-08 [1] CRAN (R 4.0.2)
#>  rstudioapi     0.13       2020-11-12 [1] CRAN (R 4.0.2)
#>  scales       * 1.2.0      2022-04-13 [1] CRAN (R 4.0.5)
#>  sessioninfo    1.2.2      2021-12-06 [1] CRAN (R 4.0.2)
#>  stringi        1.7.6      2021-11-29 [1] CRAN (R 4.0.2)
#>  stringr        1.4.0      2019-02-10 [1] CRAN (R 4.0.2)
#>  styler         1.7.0      2022-03-13 [1] CRAN (R 4.0.5)
#>  survival       3.3-1      2022-03-03 [1] CRAN (R 4.0.5)
#>  tibble       * 3.1.7      2022-05-03 [1] CRAN (R 4.0.5)
#>  tidymodels   * 0.2.0      2022-03-19 [1] CRAN (R 4.0.5)
#>  tidyr        * 1.2.0      2022-02-01 [1] CRAN (R 4.0.5)
#>  tidyselect     1.1.2      2022-02-21 [1] CRAN (R 4.0.5)
#>  timeDate       3043.102   2018-02-21 [1] CRAN (R 4.0.2)
#>  tune         * 0.2.0.9001 2022-05-09 [1] Github (tidymodels/tune@1856421)
#>  utf8           1.2.2      2021-07-24 [1] CRAN (R 4.0.2)
#>  vctrs          0.4.1      2022-04-13 [1] CRAN (R 4.0.5)
#>  withr          2.5.0      2022-03-03 [1] CRAN (R 4.0.5)
#>  workflows    * 0.2.6.9001 2022-05-09 [1] Github (tidymodels/workflows@9a9e231)
#>  workflowsets * 0.2.1      2022-03-15 [1] CRAN (R 4.0.5)
#>  xfun           0.31       2022-05-10 [1] CRAN (R 4.0.5)
#>  yaml           2.3.5      2022-02-21 [1] CRAN (R 4.0.5)
#>  yardstick    * 1.0.0      2022-06-06 [1] CRAN (R 4.0.5)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Created on 2022-06-21 by the reprex package (v2.0.1)

Ignore above. Despite updating tidymodels I was still running some of the case weights development versions (as seen in session info). Fully removing and reinstalling to prod versions has cleared the error.

Please let @Max know the link to the case weight versions.

library(tidymodels)

data("car_prices")

cars_split <- car_prices %>%
  dplyr::select(Price, Mileage, Cylinder, Doors) %>%
  rsample::initial_time_split(prop = 1 - (1/NROW(car_prices)))

carsTrain <- rsample::training(cars_split)
carsTest <- rsample::testing(cars_split)

model_rf <-
  parsnip::rand_forest(
    mode = "regression",
    mtry = tune(),
    trees = tune(),
    min_n = tune()
  ) %>%
  parsnip::set_engine("ranger")

cars_folds <-
  rsample::vfold_cv(data = carsTrain, v = 5, repeats = 1)

cars_recipe <-  carsTrain %>%
  recipes::recipe(Price ~ .) %>% 
  recipes::step_normalize(all_predictors())

cars_wflow <-
  workflows::workflow() %>%
  workflows::add_recipe(cars_recipe) %>%
  workflows::add_model(model_rf)

set.seed(123)
cars_fit <-
  cars_wflow %>% tune::tune_grid(
    resamples = cars_folds,
    metrics = yardstick::metric_set(yardstick::mae),
    control = tune::control_grid(save_pred = TRUE)
  )
#> i Creating pre-processing data to finalize unknown parameter: mtry

cars_fitFinalParams <- 
  tune::select_best(cars_fit)

model_rfFinal <- 
  finalize_model(model_rf, cars_fitFinalParams)

cars_fitFinal <-
  fit(cars_wflow %>%
        update_model(model_rfFinal),
      carsTrain)

cars_fitFinalPred <-
  predict(cars_fitFinal, new_data = carsTest)

tidymodels_update()
#> All packages up-to-date

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.0.5 (2021-03-31)
#>  os       macOS Big Sur 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_GB.UTF-8
#>  ctype    en_GB.UTF-8
#>  tz       Europe/London
#>  date     2022-06-21
#>  pandoc   2.17.1.1 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date (UTC) lib source
#>  assertthat     0.2.1      2019-03-21 [1] CRAN (R 4.0.2)
#>  backports      1.4.1      2021-12-13 [1] CRAN (R 4.0.2)
#>  broom        * 0.8.0      2022-04-13 [1] CRAN (R 4.0.5)
#>  class          7.3-20     2022-01-13 [1] CRAN (R 4.0.5)
#>  cli            3.3.0      2022-04-25 [1] CRAN (R 4.0.5)
#>  codetools      0.2-18     2020-11-04 [1] CRAN (R 4.0.5)
#>  colorspace     2.0-3      2022-02-21 [1] CRAN (R 4.0.5)
#>  crayon         1.5.1      2022-03-26 [1] CRAN (R 4.0.5)
#>  DBI            1.1.3      2022-06-18 [1] CRAN (R 4.0.5)
#>  dials        * 1.0.0      2022-06-14 [1] CRAN (R 4.0.5)
#>  DiceDesign     1.9        2021-02-13 [1] CRAN (R 4.0.2)
#>  digest         0.6.29     2021-12-01 [1] CRAN (R 4.0.2)
#>  dplyr        * 1.0.9      2022-04-28 [1] CRAN (R 4.0.5)
#>  ellipsis       0.3.2      2021-04-29 [1] CRAN (R 4.0.2)
#>  evaluate       0.15       2022-02-18 [1] CRAN (R 4.0.5)
#>  fansi          1.0.3      2022-03-24 [1] CRAN (R 4.0.5)
#>  fastmap        1.1.0      2021-01-25 [1] CRAN (R 4.0.2)
#>  foreach        1.5.2      2022-02-02 [1] CRAN (R 4.0.5)
#>  fs             1.5.2      2021-12-08 [1] CRAN (R 4.0.2)
#>  furrr          0.3.0      2022-05-04 [1] CRAN (R 4.0.5)
#>  future         1.26.1     2022-05-27 [1] CRAN (R 4.0.5)
#>  future.apply   1.9.0      2022-04-25 [1] CRAN (R 4.0.5)
#>  generics       0.1.2      2022-01-31 [1] CRAN (R 4.0.5)
#>  ggplot2      * 3.3.6      2022-05-03 [1] CRAN (R 4.0.5)
#>  globals        0.15.0     2022-05-09 [1] CRAN (R 4.0.5)
#>  glue           1.6.2      2022-02-24 [1] CRAN (R 4.0.5)
#>  gower          1.0.0      2022-02-03 [1] CRAN (R 4.0.5)
#>  GPfit          1.0-8      2019-02-08 [1] CRAN (R 4.0.2)
#>  gtable         0.3.0      2019-03-25 [1] CRAN (R 4.0.2)
#>  hardhat        1.1.0      2022-06-10 [1] CRAN (R 4.0.5)
#>  highr          0.9        2021-04-16 [1] CRAN (R 4.0.2)
#>  htmltools      0.5.2      2021-08-25 [1] CRAN (R 4.0.2)
#>  infer        * 1.0.2      2022-06-10 [1] CRAN (R 4.0.5)
#>  ipred          0.9-13     2022-06-02 [1] CRAN (R 4.0.5)
#>  iterators      1.0.14     2022-02-05 [1] CRAN (R 4.0.5)
#>  knitr          1.39       2022-04-26 [1] CRAN (R 4.0.5)
#>  lattice        0.20-45    2021-09-22 [1] CRAN (R 4.0.2)
#>  lava           1.6.10     2021-09-02 [1] CRAN (R 4.0.2)
#>  lhs            1.1.5      2022-03-22 [1] CRAN (R 4.0.5)
#>  lifecycle      1.0.1      2021-09-24 [1] CRAN (R 4.0.2)
#>  listenv        0.8.0      2019-12-05 [1] CRAN (R 4.0.2)
#>  lubridate      1.8.0      2021-10-07 [1] CRAN (R 4.0.2)
#>  magrittr       2.0.3      2022-03-30 [1] CRAN (R 4.0.5)
#>  MASS           7.3-57     2022-04-22 [1] CRAN (R 4.0.5)
#>  Matrix         1.4-1      2022-03-23 [1] CRAN (R 4.0.5)
#>  modeldata    * 0.1.1.9000 2022-05-09 [1] Github (tidymodels/modeldata@78d1b89)
#>  munsell        0.5.0      2018-06-12 [1] CRAN (R 4.0.2)
#>  nnet           7.3-17     2022-01-13 [1] CRAN (R 4.0.5)
#>  parallelly     1.32.0     2022-06-07 [1] CRAN (R 4.0.5)
#>  parsnip      * 1.0.0      2022-06-16 [1] CRAN (R 4.0.5)
#>  pillar         1.7.0      2022-02-01 [1] CRAN (R 4.0.5)
#>  pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.0.2)
#>  prodlim        2019.11.13 2019-11-17 [1] CRAN (R 4.0.2)
#>  purrr        * 0.3.4      2020-04-17 [1] CRAN (R 4.0.2)
#>  R.cache        0.15.0     2021-04-30 [1] CRAN (R 4.0.2)
#>  R.methodsS3    1.8.2      2022-06-13 [1] CRAN (R 4.0.5)
#>  R.oo           1.25.0     2022-06-12 [1] CRAN (R 4.0.5)
#>  R.utils        2.11.0     2021-09-26 [1] CRAN (R 4.0.2)
#>  R6             2.5.1      2021-08-19 [1] CRAN (R 4.0.2)
#>  ranger       * 0.14.1     2022-06-18 [1] CRAN (R 4.0.5)
#>  Rcpp           1.0.8.3    2022-03-17 [1] CRAN (R 4.0.5)
#>  recipes      * 0.2.0      2022-02-18 [1] CRAN (R 4.0.5)
#>  reprex         2.0.1      2021-08-05 [1] CRAN (R 4.0.2)
#>  rlang          1.0.2      2022-03-04 [1] CRAN (R 4.0.5)
#>  rmarkdown      2.14       2022-04-25 [1] CRAN (R 4.0.5)
#>  rpart          4.1.16     2022-01-24 [1] CRAN (R 4.0.5)
#>  rsample      * 0.1.1      2021-11-08 [1] CRAN (R 4.0.2)
#>  rstudioapi     0.13       2020-11-12 [1] CRAN (R 4.0.2)
#>  scales       * 1.2.0      2022-04-13 [1] CRAN (R 4.0.5)
#>  sessioninfo    1.2.2      2021-12-06 [1] CRAN (R 4.0.2)
#>  stringi        1.7.6      2021-11-29 [1] CRAN (R 4.0.2)
#>  stringr        1.4.0      2019-02-10 [1] CRAN (R 4.0.2)
#>  styler         1.7.0      2022-03-13 [1] CRAN (R 4.0.5)
#>  survival       3.3-1      2022-03-03 [1] CRAN (R 4.0.5)
#>  tibble       * 3.1.7      2022-05-03 [1] CRAN (R 4.0.5)
#>  tidymodels   * 0.2.0      2022-03-19 [1] CRAN (R 4.0.5)
#>  tidyr        * 1.2.0      2022-02-01 [1] CRAN (R 4.0.5)
#>  tidyselect     1.1.2      2022-02-21 [1] CRAN (R 4.0.5)
#>  timeDate       3043.102   2018-02-21 [1] CRAN (R 4.0.2)
#>  tune         * 0.2.0      2022-03-19 [1] CRAN (R 4.0.5)
#>  utf8           1.2.2      2021-07-24 [1] CRAN (R 4.0.2)
#>  vctrs          0.4.1      2022-04-13 [1] CRAN (R 4.0.5)
#>  withr          2.5.0      2022-03-03 [1] CRAN (R 4.0.5)
#>  workflows    * 0.2.6      2022-03-18 [1] CRAN (R 4.0.5)
#>  workflowsets * 0.2.1      2022-03-15 [1] CRAN (R 4.0.5)
#>  xfun           0.31       2022-05-10 [1] CRAN (R 4.0.5)
#>  yaml           2.3.5      2022-02-21 [1] CRAN (R 4.0.5)
#>  yardstick    * 1.0.0      2022-06-06 [1] CRAN (R 4.0.5)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Created on 2022-06-21 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.