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")