调整后的预测 Tidymodels
有谁知道如何将 marginaleffects()
包中的 predictions()
与 tidymodels
一起使用?在这个玩具示例中,我想要获取变量 state
的预测值,同时将所有其他变量保持在其基本水平或平均值。
library(liver)
library(tidymodels)
library(marginaleffects)
df_churn <- data.frame(churn)
# Create data split object
churn_split <- initial_split(df_churn, prop = 0.75,
strata = churn)
# Create the training data
churn_train <- churn_split %>%
training()
# Create the test data
churn_test <- churn_split %>%
testing()
lr_mod <-
logistic_reg(penalty = tune(), mixture = 1) %>% # penalty = lambda. mixture = alpha
set_engine("glmnet") %>%
set_mode("classification")
# pre-process recipe
churn_recipe <- recipe(churn ~ .,
data = churn_train) %>%
step_corr(all_numeric(), threshold = 0.9) %>%
step_normalize(all_numeric()) %>%
step_dummy(all_nominal(), -all_outcomes())
# model + recipe = workflow
churn_wkfl <- workflow() %>%
add_model(lr_mod) %>%
add_recipe(churn_recipe)
# cv
set.seed(1)
churn_folds <- vfold_cv(churn_train,
v = 10,
strata = churn)
# grid
set.seed(1)
glmnet_tuning <- churn_wkfl %>%
tune_grid(resamples = churn_folds,
grid = 25, # let the model find the best hyperparameters
metrics = metric_set(roc_auc))
# select the best model
best_glmnet_model <- glmnet_tuning %>%
select_best(metric = 'roc_auc')
# finalize the workflow and try to get adjusted predictions
# This does not work
final_churn_wkfl <- churn_wkfl %>%
finalize_workflow(best_glmnet_model) %>%
fit(churn_train) %>%
tidy() %>%
predictions(variables = c("state"))
Does anyone know how to use predictions()
in the marginaleffects()
package with tidymodels
? In this toy example, I want to get the predicted values of the variable state
while holding all other variables at their base levels or mean values.
library(liver)
library(tidymodels)
library(marginaleffects)
df_churn <- data.frame(churn)
# Create data split object
churn_split <- initial_split(df_churn, prop = 0.75,
strata = churn)
# Create the training data
churn_train <- churn_split %>%
training()
# Create the test data
churn_test <- churn_split %>%
testing()
lr_mod <-
logistic_reg(penalty = tune(), mixture = 1) %>% # penalty = lambda. mixture = alpha
set_engine("glmnet") %>%
set_mode("classification")
# pre-process recipe
churn_recipe <- recipe(churn ~ .,
data = churn_train) %>%
step_corr(all_numeric(), threshold = 0.9) %>%
step_normalize(all_numeric()) %>%
step_dummy(all_nominal(), -all_outcomes())
# model + recipe = workflow
churn_wkfl <- workflow() %>%
add_model(lr_mod) %>%
add_recipe(churn_recipe)
# cv
set.seed(1)
churn_folds <- vfold_cv(churn_train,
v = 10,
strata = churn)
# grid
set.seed(1)
glmnet_tuning <- churn_wkfl %>%
tune_grid(resamples = churn_folds,
grid = 25, # let the model find the best hyperparameters
metrics = metric_set(roc_auc))
# select the best model
best_glmnet_model <- glmnet_tuning %>%
select_best(metric = 'roc_auc')
# finalize the workflow and try to get adjusted predictions
# This does not work
final_churn_wkfl <- churn_wkfl %>%
finalize_workflow(best_glmnet_model) %>%
fit(churn_train) %>%
tidy() %>%
predictions(variables = c("state"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,glmnet 不是受支持的边际效应模型之一。
您可以将其切换为受支持的模型之一(例如常规
glm()
)和此 将使用extract_fit_engine()
工作。由 reprex 包 (v2.0.1) 于 2022 年 3 月 25 日创建
我没有使用
variables = c("state")
并替换了连续的数字预测变量之一。Unfortunately, glmnet is not one of the supported models for marginaleffects.
You can switch this to one of the supported models (like regular
glm()
) and this will work usingextract_fit_engine()
.Created on 2022-03-25 by the reprex package (v2.0.1)
Notice that I did not use
variables = c("state")
and substituted one of the continuous, numeric predictors.