trying the code in the book of "tidymodeling with R" at the page 486 feedback error

Hello !
I am reading the book "Tidy modeling with R". Many thanks to the authors,It is awsome.

When I trying the follwing code at page 486.

set.seed(1805)
 pdp_age <- model_profile(explainer_rf, N = 500, variables = "Year_Built")

It shows error like this:

Error in `scream()`:
! Can't convert from `data$Year_Built` <double> to `Year_Built` <integer> due to loss of precision.
• Locations: 3, 5, 7, 16, 26, 67, 78, 80, 82, 91, 101, 142, 153, 155, 157, 166,...
Traceback:

1. model_profile(explainer_rf, N = 500, variables = "Year_Built")
2. ingredients::ceteris_paribus(explainer, new_observation = ndata, 
 .     variables = variables, ...) 

I dont know what is wrong? I am Looking forward to some help. The whole code as follow:

suppressMessages({
    library(tidymodels)
    tidymodels_prefer()
    library(DALEXtra)
    theme_set(theme_bw())
})

data(ames, package = "modeldata")
set.seed(501)
ames_split <- initial_split(ames, prop = 0.8)
ames_train <- training(ames_split)
ames_test <- testing(ames_split)


lm_model <- linear_reg() %>% 
    set_engine("lm")

ames_rec <- 
  recipe(Sale_Price ~ Neighborhood + Gr_Liv_Area + Year_Built + Bldg_Type + 
           Latitude + Longitude, data = ames_train) %>%
  step_log(Gr_Liv_Area, base = 10) %>% 
  step_other(Neighborhood, threshold = 0.01, id = "my_id") %>% 
  step_dummy(all_nominal_predictors()) %>% 
  step_interact( ~ Gr_Liv_Area:starts_with("Bldg_Type_") ) %>% 
  step_ns(Latitude, Longitude, deg_free = 20)

lm_wflow <- 
  workflow() %>% 
     add_model(lm_model) %>%
  add_recipe(ames_rec)
  

lm_fit <- fit(lm_wflow, ames_train)


rf_model <- 
  rand_forest(trees = 1000) %>% 
  set_engine("ranger") %>% 
  set_mode("regression")

rf_wflow <- 
  workflow() %>% 
  add_formula(
    Sale_Price ~ Neighborhood + Gr_Liv_Area + Year_Built + Bldg_Type + 
      Latitude + Longitude) %>% 
  add_model(rf_model) 

rf_fit <- rf_wflow %>% fit(data = ames_train)

vip_features <- c("Neighborhood", "Gr_Liv_Area", "Year_Built", 
                  "Bldg_Type", "Latitude", "Longitude")
vip_train <- 
  ames_train %>% 
  select(all_of(vip_features))

explainer_lm <- 
  explain_tidymodels(
    lm_fit, 
    data = vip_train, 
    y = ames_train$Sale_Price,
    label = "lm + interactions",
    verbose = FALSE
  )

explainer_rf <- 
  explain_tidymodels(
    rf_fit, 
    data = vip_train, 
    y = ames_train$Sale_Price,
    label = "random forest",
    verbose = FALSE
  )
set.seed(1805)
pdp_age <- model_profile(explainer_rf, N = 500, variables = "Year_Built")

before you split ames; change any integers to numeric

ames<- mutate(ames,
               across(.cols = where(is.integer),
                      .fns = as.numeric))
1 Like

Thanks for pointing this out, @jchpan!

@nirgrahamuk's recommendation will indeed do the trick for now. This error is due to a bug in the ingredients package, and I've filed an issue on their repo. Hopefully, this will once again "just work" in due time. :slight_smile:

This topic was automatically closed 7 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.