Search code examples
rtidymodels

Extract plain model from tidymodel object


Is it possible to extract, say, a model of class glm from a tidymodel built with recipe and logistic_reg() %>% set_engine("glm")?

I'd like to use packages from the easystats project, which require "normal", non-tidy models. The workflow extractor function (pull_workflow_fit()) returns an object of class `"_glm" "model_fit", which doesn't seem to be compatible.

I understand I can generate a model using glm() and the same formula as in the recipe, but it seems to me the fitted parameters differ. Thanks!


Solution

  • The easystats package suite supports tidymodels since the last updates:

    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
    

    Created on 2021-04-25 by the reprex package (v2.0.0)