Terryb
Terryb

Reputation: 51

I am getting an error with R tidymodels ranger example. How do I resolve this?

Hello I am getting an error in tidymodels ranger. I've provided a reproducible example below using reprex as suggested.

Can anyone identify what might be the issue here?

Many thanks.

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.5.3
#> Warning: package 'readr' was built under R version 3.5.3
#> Warning: package 'purrr' was built under R version 3.5.3
#> Warning: package 'stringr' was built under R version 3.5.3
#> Warning: package 'forcats' was built under R version 3.5.3
library(tidymodels)
#> -- Attaching packages -------------------------------------- tidymodels 0.1.2 --
#> v broom     0.7.5      v recipes   0.1.15
#> v dials     0.0.9      v rsample   0.0.9 
#> v infer     0.5.4      v tune      0.1.3 
#> v modeldata 0.1.0      v workflows 0.2.2 
#> v parsnip   0.1.5      v yardstick 0.0.7
#> Warning: package 'scales' was built under R version 3.5.3
#> -- Conflicts ----------------------------------------- tidymodels_conflicts() --
#> x scales::discard() masks purrr::discard()
#> x dplyr::filter()   masks stats::filter()
#> x recipes::fixed()  masks stringr::fixed()
#> x dplyr::lag()      masks stats::lag()
#> x yardstick::spec() masks readr::spec()
#> x recipes::step()   masks stats::step()

# get and clean data
voters <- read_csv("https://raw.githubusercontent.com/juliasilge/supervised-ML-case-studies-course/master/data/voters.csv")
#> Parsed with column specification:
#> cols(
#>   .default = col_double(),
#>   turnout16_2016 = col_character()
#> )
#> See spec(...) for full column specifications.
voters_select <- voters %>% 
  mutate(turnout16_2016 = factor(turnout16_2016)) %>% 
  select(-case_identifier)

# recipe
vote_rec <- voters_select %>% 
  recipe(data = ., turnout16_2016 ~ .) 

# rf model spec
rf_spec <- rand_forest() %>% 
  set_engine("ranger") %>% 
  set_mode("classification")

# fit 
rf_fit <- workflow() %>% 
  add_recipe(vote_rec) %>% 
  add_model(rf_spec) %>% 
  fit(data = voters_select)
#> Error in ranger::ranger(x = maybe_data_frame(x), y = y, num.threads = 1, : unused arguments (x = maybe_data_frame(x), y = y)
#> Timing stopped at: 0 0 0

Created on 2021-07-21 by the reprex package (v0.3.0)

Upvotes: 1

Views: 929

Answers (1)

EmilHvitfeldt
EmilHvitfeldt

Reputation: 3185

I'm not able to reproduce your error. You need to make sure all the necessary previous objects have been created as well before you fit the model specification.

Below is a reproducible example copied from the article

library(tidymodels)
data(cells, package = "modeldata")

set.seed(123)
cell_split <- initial_split(cells %>% select(-case), 
                            strata = class)

cell_train <- training(cell_split)
cell_test  <- testing(cell_split)

rf_mod <- 
  rand_forest(trees = 1000) %>% 
  set_engine("ranger") %>% 
  set_mode("classification")

set.seed(234)
rf_fit <- 
  rf_mod %>% 
  fit(class ~ ., data = cell_train)
rf_fit
#> parsnip model object
#> 
#> Fit time:  2.7s 
#> Ranger result
#> 
#> Call:
#>  ranger::ranger(x = maybe_data_frame(x), y = y, num.trees = ~1000,      num.threads = 1, verbose = FALSE, seed = sample.int(10^5,          1), probability = TRUE) 
#> 
#> Type:                             Probability estimation 
#> Number of trees:                  1000 
#> Sample size:                      1515 
#> Number of independent variables:  56 
#> Mtry:                             7 
#> Target node size:                 10 
#> Variable importance mode:         none 
#> Splitrule:                        gini 
#> OOB prediction error (Brier s.):  0.1218873

Created on 2021-04-23 by the reprex package (v1.0.0)

Upvotes: 1

Related Questions