从 tidymodel 对象中提取普通模型

Extract plain model from tidymodel object

是否可以从使用 recipelogistic_reg() %>% set_engine("glm") 构建的 tidymodel 中提取 class glm 的模型?

我想使用 easystats 项目中的包,它需要“正常”、不整齐的模型。工作流提取器函数 (pull_workflow_fit()) returns class `"_glm" "model_fit" 的对象,似乎不兼容。

我知道我可以使用 glm() 和与 recipe 中相同的公式生成模型,但在我看来拟合参数不同。 谢谢!

您可以使用 $fit.

从欧洲防风草对象中提取底层模型对象(无论是由 glm 或 ranger 或 keras 或任何东西创建的)
library(tidymodels)

data(two_class_dat)

glm_spec <- logistic_reg() %>%
  set_engine("glm") 

norm_rec <- recipe(Class ~ A + B, data = two_class_dat) %>%
  step_normalize(all_predictors())

glm_fit <- workflow() %>%
  add_recipe(norm_rec) %>%
  add_model(glm_spec) %>%
  fit(two_class_dat) %>%
  pull_workflow_fit()

那个合适的物体里有什么?

## this is a parsnip object
glm_fit
#> parsnip model object
#> 
#> Fit time:  5ms 
#> 
#> Call:  stats::glm(formula = ..y ~ ., family = stats::binomial, data = data)
#> 
#> Coefficients:
#> (Intercept)            A            B  
#>     -0.3491      -1.1063       2.7966  
#> 
#> Degrees of Freedom: 790 Total (i.e. Null);  788 Residual
#> Null Deviance:       1088 
#> Residual Deviance: 673.9     AIC: 679.9

## this is a glm object
glm_fit$fit
#> 
#> Call:  stats::glm(formula = ..y ~ ., family = stats::binomial, data = data)
#> 
#> Coefficients:
#> (Intercept)            A            B  
#>     -0.3491      -1.1063       2.7966  
#> 
#> Degrees of Freedom: 790 Total (i.e. Null);  788 Residual
#> Null Deviance:       1088 
#> Residual Deviance: 673.9     AIC: 679.9

reprex package (v1.0.0)

于 2021-02-04 创建

拟合出来的参数肯定和直接调用模型没有区别。如果您认为您找到了不同的拟合参数,那么您调用模型的方式可能会出错。

自上次更新后 easystats package suite 支持 tidymodels:

library(tidymodels)

data(two_class_dat)

glm_spec <- logistic_reg() %>%
  set_engine("glm") 

norm_rec <- recipe(Class ~ A + B, data = two_class_dat) %>%
  step_normalize(all_predictors())

workflow() %>%
  add_recipe(norm_rec) %>%
  add_model(glm_spec) %>%
  fit(two_class_dat) %>%
  pull_workflow_fit() %>% 
  parameters::model_parameters()
#> Parameter   | Log-Odds |   SE |         95% CI |     z |      p
#> ---------------------------------------------------------------
#> (Intercept) |    -0.35 | 0.10 | [-0.54, -0.16] | -3.55 | < .001
#> A           |    -1.11 | 0.17 | [-1.44, -0.79] | -6.64 | < .001
#> B           |     2.80 | 0.21 | [ 2.40,  3.22] | 13.33 | < .001


workflow() %>%
  add_recipe(norm_rec) %>%
  add_model(glm_spec) %>%
  fit(two_class_dat) %>%
  pull_workflow_fit() %>% 
  parameters::model_parameters() %>% 
  plot()



workflow() %>%
  add_recipe(norm_rec) %>%
  add_model(glm_spec) %>%
  fit(two_class_dat) %>%
  pull_workflow_fit() %>% 
  parameters::model_parameters() %>% 
  parameters::print_md()
Parameter Log-Odds SE 95% CI z p
(Intercept) -0.35 0.10 (-0.54, -0.16) -3.55 < .001
A -1.11 0.17 (-1.44, -0.79) -6.64 < .001
B 2.80 0.21 (2.40, 3.22) 13.33 < .001


workflow() %>%
  add_recipe(norm_rec) %>%
  add_model(glm_spec) %>%
  fit(two_class_dat) %>%
  pull_workflow_fit() %>% 
  performance::model_performance()
#> # Indices of model performance
#> 
#> AIC     |     BIC | Tjur's R2 |  RMSE | Sigma | Log_loss | Score_log | Score_spherical |   PCP
#> ----------------------------------------------------------------------------------------------
#> 679.950 | 693.970 |     0.460 | 0.362 | 0.925 |    0.426 |      -Inf |           0.003 | 0.733

reprex package (v2.0.0)

于 2021-04-25 创建