I've been fitting a series of models using case weights. The aim is to down-weight the influence of a large number of background points relative to observed presence points. I have been using logistic regression and ML: fitting a GLM, GAM, RF and BRT - all of which can accept case-weights.

The models are fit via spatial cross-validation using the excellent `spatialsample`

package and I have had some help applying the weights to the folds

From the series of fitted models I would like to generate partial dependence plots. However, when a fitted model is passed to DALEX::explain_tidymodels and DALEX::model_profile I get the following error:

```
`Error in `quantile()`:
! `quantile.hardhat_importance_weights()` not implemented.`
```

Typically, although a model is fitted with weights, the weights are not needed when generating predictions to new data. Can anyone offer advice on how to generate predictions from a tidymodel workflow using case weights, either using DALEX or using an alternative?

Thanks in advance for any help or suggestions, here's a reprex to illustrate the issue.

James

```
set.seed(1107)
# packages
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(tidymodels)
library(spatialsample)
library(DALEXtra)
#> Loading required package: DALEX
#> Welcome to DALEX (version: 2.4.3).
#> Find examples and detailed introduction at: http://ema.drwhy.ai/
#> Additional features will be available after installation of: ggpubr.
#> Use 'install_dependencies()' to get all suggested dependencies
#>
#> Attaching package: 'DALEX'
#> The following object is masked from 'package:dplyr':
#>
#> explain
#> Anaconda not found on your computer. Conda related functionality such as create_env.R and condaenv and yml parameters from explain_scikitlearn will not be available
## Data prep:
# pak::pkg_install("Nowosad/spDataLarge")
data("lsl", "study_mask", package = "spDataLarge")
ta <- terra::rast(system.file("raster/ta.tif", package = "spDataLarge"))
lsl <- lsl |>
st_as_sf(coords = c("x", "y"), crs = "EPSG:32717")
# convert to 0, 1 as is typical in species distribution modelling
lsl <- lsl |>
mutate(lslpts = factor(as.numeric(lslpts)-1)) |>
# Creating a dummy case weights column, to get past initial verification by recipe
mutate(cwts = hardhat::importance_weights(NA))
# set up case weights as a recipe step
lsl_recipe <- recipes::recipe(
lslpts ~ slope + cplan + cprof + elev + log10_carea,
data = sf::st_drop_geometry(lsl)
) |>
recipes::step_mutate(
cwts = hardhat::importance_weights(
ifelse(lslpts == 1, 1, sum(lslpts == 1) / sum(lslpts == 0))
),
# Need to set the "case_weights" role explicitly:
role = "case_weights"
)
# split into folds
lsl_folds <- spatial_block_cv(lsl, method = "random", v = 10)
# try GLM
glm_model <- logistic_reg() |>
set_engine("glm") |>
set_mode("classification")
# Using weights instead: no add_formula, because the formula is in our recipe
glm_wflow_wts <- workflow(preprocessor = lsl_recipe) |>
add_model(glm_model) |>
add_case_weights(cwts) |>
fit_resamples(lsl_folds)
# generate partial dependence profile for model
# ideally want to generate profile for each fold to verify model fit
explain_tidymodels(glm_wflow_wts,
data = lsl_folds$splits[[1]] |> analysis() |> st_drop_geometry(),
y = lsl_folds$splits[[1]] |> analysis() |> st_drop_geometry() |> pull(lslpts)) |>
model_profile(N = 1000, type = "partial")
#> Warning: Unknown or uninitialised column: `spec`.
#> Preparation of a new explainer is initiated
#> -> model label : data.frame ( default )
#> -> data : 308 rows 7 cols
#> -> target variable : 308 values
#> -> predict function : yhat.default will be used ( default )
#> -> predicted values : No value for predict function target column. ( default )
#> -> model_info : package Model of class: resample_results package unrecognized , ver. Unknown , task regression ( default )
#> -> model_info : Model info detected regression task but 'y' is a factor . ( WARNING )
#> -> model_info : By deafult regressions tasks supports only numercical 'y' parameter.
#> -> model_info : Consider changing to numerical vector.
#> -> model_info : Otherwise I will not be able to calculate residuals or loss function.
#> -> predicted values : the predict_function returns an error when executed ( WARNING )
#> -> residual function : difference between y and yhat ( default )
#> -> residuals : the residual_function returns an error when executed ( WARNING )
#> A new explainer has been created!
#> Error in `quantile()`:
#> ! `quantile.hardhat_importance_weights()` not implemented.
#> Backtrace:
#> â–†
#> 1. â””â”€DALEX::model_profile(...)
#> 2. â”śâ”€ingredients::ceteris_paribus(...)
#> 3. â””â”€ingredients:::ceteris_paribus.explainer(...)
#> 4. â””â”€ingredients:::ceteris_paribus.default(...)
#> 5. â”śâ”€ingredients:::calculate_variable_split(...)
#> 6. â””â”€ingredients:::calculate_variable_split.default(...)
#> 7. â””â”€base::lapply(...)
#> 8. â””â”€ingredients (local) FUN(X[[i]], ...)
#> 9. â”śâ”€base::unique(quantile(selected_column, probs = probs))
#> 10. â”śâ”€stats::quantile(selected_column, probs = probs)
#> 11. â””â”€vctrs:::quantile.vctrs_vctr(selected_column, probs = probs)
#> 12. â””â”€vctrs:::stop_unimplemented(x, "quantile")
#> 13. â””â”€vctrs:::stop_vctrs(...)
#> 14. â””â”€rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
```

^{Created on 2023-05-29 with reprex v2.0.2}