Something is happening in how categorical data in the tidymodels glmnet model is being tidied by gtsummary. The tidymodels team suggested that it seems like gtsummary doesn't account for the transformation from the original variables via preprocessing from the recipe, or maybe the tidying doesn't work that well at all? A reprex of the error is below. And any help is much appreciated!
library(tidymodels)
data(penguins)
my_split <- penguins %>%
na.omit() %>%
initial_split()
rec <- recipe(sex ~ species + bill_length_mm + bill_depth_mm,
data = penguins
) %>%
step_dummy(species)
glmnet_spec <- logistic_reg(penalty = 0.1, mixture = 1) %>%
set_engine("glmnet")
glmnet_fit <-
workflow(rec, glmnet_spec) %>%
last_fit(my_split)
extract_workflow(glmnet_fit) %>%
gtsummary::tbl_regression(exponentiate = TRUE) %>%
gtsummary::as_kable()
#> Extracting {workflows} model fit with `workflows::extract_fit_parsnip(x) %>% tbl_regression(...)`
#> Extracting {parsnip} model fit with `tbl_regression(x = x$fit, ...)`
#> x Unable to identify the list of variables.
#>
#> This is usually due to an error calling `stats::model.frame(x)`or `stats::model.matrix(x)`.
#> It could be the case if that type of model does not implement these methods.
#> Rarely, this error may occur if the model object was created within
#> a functional programming framework (e.g. using `lappy()`, `purrr::map()`, etc.).
```
| **Characteristic** | **exp(Beta)** |
|:-------------------|:-------------:|
| (Intercept) | |
| (Intercept) | 0.07 |
| (Intercept) | -1.11 |
| (Intercept) | -2.38 |
| (Intercept) | -3.56 |
| (Intercept) | -4.67 |
| (Intercept) | -5.73 |
| (Intercept) | -6.75 |
| (Intercept) | -7.71 |
| (Intercept) | -9.41 |
| (Intercept) | -11.2 |
| (Intercept) | -12.9 |
| (Intercept) | -14.6 |
| (Intercept) | -16.3 |
| (Intercept) | -18.0 |
| (Intercept) | -19.6 |
| (Intercept) | -21.3 |
| (Intercept) | -22.9 |
| (Intercept) | -24.6 |
| (Intercept) | -26.2 |
| (Intercept) | -27.8 |
| (Intercept) | -29.4 |
| (Intercept) | -30.9 |
| (Intercept) | -32.5 |
| (Intercept) | -34.0 |
| (Intercept) | -35.5 |
| (Intercept) | -37.0 |
| (Intercept) | -38.4 |
| (Intercept) | -39.9 |
| (Intercept) | -41.2 |
| (Intercept) | -42.6 |
| (Intercept) | -43.9 |
| (Intercept) | -45.2 |
| (Intercept) | -46.4 |
| (Intercept) | -47.6 |
| (Intercept) | -48.8 |
| (Intercept) | -49.9 |
| (Intercept) | -51.0 |
| (Intercept) | -52.0 |
| (Intercept) | -53.0 |
| (Intercept) | -53.9 |
| (Intercept) | -54.8 |
| (Intercept) | -55.6 |
| (Intercept) | -56.4 |
| (Intercept) | -57.1 |
| (Intercept) | -57.8 |
| (Intercept) | -58.4 |
| (Intercept) | -59.0 |
| (Intercept) | -59.6 |
| (Intercept) | -60.1 |
| (Intercept) | -60.6 |
| (Intercept) | -61.0 |
| (Intercept) | -61.5 |
| (Intercept) | -61.8 |
| (Intercept) | -62.2 |
| (Intercept) | -62.5 |
| (Intercept) | -62.8 |
| (Intercept) | -63.1 |
| (Intercept) | -63.4 |
| (Intercept) | -63.6 |
| (Intercept) | -63.8 |
| (Intercept) | -64.0 |
| (Intercept) | -64.2 |
| (Intercept) | -64.4 |
| (Intercept) | -64.5 |
| (Intercept) | -64.7 |
| (Intercept) | -64.8 |
| (Intercept) | -64.9 |
| (Intercept) | -65.0 |
| (Intercept) | -65.1 |
| bill_length_mm | |
| bill_length_mm | 0.01 |
| bill_length_mm | 0.03 |
| bill_length_mm | 0.04 |
| bill_length_mm | 0.05 |
| bill_length_mm | 0.06 |
| bill_length_mm | 0.08 |
| bill_length_mm | 0.09 |
| bill_length_mm | 0.11 |
| bill_length_mm | 0.13 |
| bill_length_mm | 0.15 |
| bill_length_mm | 0.17 |
| bill_length_mm | 0.19 |
| bill_length_mm | 0.21 |
| bill_length_mm | 0.23 |
| bill_length_mm | 0.25 |
| bill_length_mm | 0.27 |
| bill_length_mm | 0.28 |
| bill_length_mm | 0.30 |
| bill_length_mm | 0.32 |
| bill_length_mm | 0.34 |
| bill_length_mm | 0.36 |
| bill_length_mm | 0.38 |
| bill_length_mm | 0.40 |
| bill_length_mm | 0.41 |
| bill_length_mm | 0.43 |
| bill_length_mm | 0.45 |
| bill_length_mm | 0.47 |
| bill_length_mm | 0.48 |
| bill_length_mm | 0.50 |
| bill_length_mm | 0.51 |
| bill_length_mm | 0.53 |
| bill_length_mm | 0.54 |
| bill_length_mm | 0.55 |
| bill_length_mm | 0.56 |
| bill_length_mm | 0.57 |
| bill_length_mm | 0.58 |
| bill_length_mm | 0.59 |
| bill_length_mm | 0.59 |
| bill_length_mm | 0.60 |
| bill_length_mm | 0.61 |
| bill_length_mm | 0.62 |
| bill_length_mm | 0.62 |
| bill_length_mm | 0.63 |
| bill_length_mm | 0.64 |
| bill_length_mm | 0.64 |
| bill_length_mm | 0.65 |
| bill_length_mm | 0.65 |
| bill_length_mm | 0.66 |
| bill_length_mm | 0.66 |
| bill_length_mm | 0.66 |
| bill_length_mm | 0.67 |
| bill_length_mm | 0.67 |
| bill_length_mm | 0.67 |
| bill_length_mm | 0.68 |
| bill_length_mm | 0.68 |
| bill_length_mm | 0.68 |
| bill_length_mm | 0.68 |
| bill_length_mm | 0.69 |
| bill_length_mm | 0.69 |
| bill_length_mm | 0.69 |
| bill_length_mm | 0.69 |
| bill_length_mm | 0.69 |
| bill_length_mm | 0.69 |
| bill_length_mm | 0.70 |
| bill_length_mm | 0.70 |
| bill_length_mm | 0.70 |
| bill_length_mm | 0.70 |
| bill_length_mm | 0.70 |
| bill_depth_mm | |
| bill_depth_mm | 0.03 |
| bill_depth_mm | 0.07 |
| bill_depth_mm | 0.11 |
| bill_depth_mm | 0.14 |
| bill_depth_mm | 0.17 |
| bill_depth_mm | 0.21 |
| bill_depth_mm | 0.24 |
| bill_depth_mm | 0.29 |
| bill_depth_mm | 0.34 |
| bill_depth_mm | 0.39 |
| bill_depth_mm | 0.44 |
| bill_depth_mm | 0.49 |
| bill_depth_mm | 0.54 |
| bill_depth_mm | 0.59 |
| bill_depth_mm | 0.64 |
| bill_depth_mm | 0.69 |
| bill_depth_mm | 0.74 |
| bill_depth_mm | 0.79 |
| bill_depth_mm | 0.83 |
| bill_depth_mm | 0.88 |
| bill_depth_mm | 0.93 |
| bill_depth_mm | 0.97 |
| bill_depth_mm | 1.02 |
| bill_depth_mm | 1.06 |
| bill_depth_mm | 1.11 |
| bill_depth_mm | 1.15 |
| bill_depth_mm | 1.19 |
| bill_depth_mm | 1.23 |
| bill_depth_mm | 1.27 |
| bill_depth_mm | 1.31 |
| bill_depth_mm | 1.35 |
| bill_depth_mm | 1.40 |
| bill_depth_mm | 1.44 |
| bill_depth_mm | 1.49 |
| bill_depth_mm | 1.53 |
| bill_depth_mm | 1.57 |
| bill_depth_mm | 1.61 |
| bill_depth_mm | 1.64 |
| bill_depth_mm | 1.68 |
| bill_depth_mm | 1.71 |
| bill_depth_mm | 1.74 |
| bill_depth_mm | 1.77 |
| bill_depth_mm | 1.79 |
| bill_depth_mm | 1.82 |
| bill_depth_mm | 1.84 |
| bill_depth_mm | 1.86 |
| bill_depth_mm | 1.88 |
| bill_depth_mm | 1.90 |
| bill_depth_mm | 1.92 |
| bill_depth_mm | 1.93 |
| bill_depth_mm | 1.95 |
| bill_depth_mm | 1.96 |
| bill_depth_mm | 1.98 |
| bill_depth_mm | 1.99 |
| bill_depth_mm | 2.00 |
| bill_depth_mm | 2.01 |
| bill_depth_mm | 2.02 |
| bill_depth_mm | 2.03 |
| bill_depth_mm | 2.04 |
| bill_depth_mm | 2.04 |
| bill_depth_mm | 2.05 |
| bill_depth_mm | 2.05 |
| bill_depth_mm | 2.06 |
| bill_depth_mm | 2.07 |
| bill_depth_mm | 2.07 |
| bill_depth_mm | 2.07 |
| bill_depth_mm | 2.08 |
| bill_depth_mm | 2.08 |
| species_Chinstrap | |
| species_Chinstrap | -0.25 |
| species_Chinstrap | -0.53 |
| species_Chinstrap | -0.80 |
| species_Chinstrap | -1.06 |
| species_Chinstrap | -1.31 |
| species_Chinstrap | -1.56 |
| species_Chinstrap | -1.81 |
| species_Chinstrap | -2.05 |
| species_Chinstrap | -2.29 |
| species_Chinstrap | -2.53 |
| species_Chinstrap | -2.76 |
| species_Chinstrap | -3.00 |
| species_Chinstrap | -3.22 |
| species_Chinstrap | -3.45 |
| species_Chinstrap | -3.67 |
| species_Chinstrap | -3.89 |
| species_Chinstrap | -4.11 |
| species_Chinstrap | -4.32 |
| species_Chinstrap | -4.53 |
| species_Chinstrap | -4.73 |
| species_Chinstrap | -4.93 |
| species_Chinstrap | -5.12 |
| species_Chinstrap | -5.31 |
| species_Chinstrap | -5.47 |
| species_Chinstrap | -5.61 |
| species_Chinstrap | -5.73 |
| species_Chinstrap | -5.86 |
| species_Chinstrap | -5.98 |
| species_Chinstrap | -6.09 |
| species_Chinstrap | -6.20 |
| species_Chinstrap | -6.30 |
| species_Chinstrap | -6.40 |
| species_Chinstrap | -6.50 |
| species_Chinstrap | -6.58 |
| species_Chinstrap | -6.67 |
| species_Chinstrap | -6.75 |
| species_Chinstrap | -6.82 |
| species_Chinstrap | -6.89 |
| species_Chinstrap | -6.95 |
| species_Chinstrap | -7.01 |
| species_Chinstrap | -7.07 |
| species_Chinstrap | -7.12 |
| species_Chinstrap | -7.17 |
| species_Chinstrap | -7.22 |
| species_Chinstrap | -7.26 |
| species_Chinstrap | -7.30 |
| species_Chinstrap | -7.33 |
| species_Chinstrap | -7.37 |
| species_Chinstrap | -7.40 |
| species_Chinstrap | -7.42 |
| species_Chinstrap | -7.45 |
| species_Chinstrap | -7.47 |
| species_Chinstrap | -7.49 |
| species_Chinstrap | -7.52 |
| species_Chinstrap | -7.53 |
| species_Chinstrap | -7.55 |
| species_Chinstrap | -7.57 |
| species_Chinstrap | -7.58 |
| species_Chinstrap | -7.59 |
| species_Chinstrap | -7.60 |
| species_Chinstrap | -7.61 |
| species_Gentoo | |
| species_Gentoo | 0.03 |
| species_Gentoo | 0.10 |
| species_Gentoo | 0.17 |
| species_Gentoo | 0.24 |
| species_Gentoo | 0.31 |
| species_Gentoo | 0.37 |
| species_Gentoo | 0.43 |
| species_Gentoo | 0.49 |
| species_Gentoo | 0.54 |
| species_Gentoo | 0.59 |
| species_Gentoo | 0.63 |
| species_Gentoo | 0.68 |
| species_Gentoo | 0.72 |
| species_Gentoo | 0.76 |
| species_Gentoo | 0.79 |
| species_Gentoo | 0.82 |
| species_Gentoo | 0.85 |
| species_Gentoo | 0.88 |
| species_Gentoo | 0.91 |
| species_Gentoo | 0.93 |
| species_Gentoo | 0.95 |
| species_Gentoo | 0.97 |
| species_Gentoo | 0.99 |
| species_Gentoo | 1.01 |
| species_Gentoo | 1.03 |
| species_Gentoo | 1.04 |
| species_Gentoo | 1.06 |
| species_Gentoo | 1.07 |
| species_Gentoo | 1.08 |
| species_Gentoo | 1.09 |
| species_Gentoo | 1.10 |
| species_Gentoo | 1.11 |
| species_Gentoo | 1.12 |
| species_Gentoo | 1.12 |
| species_Gentoo | 1.13 |
| species_Gentoo | 1.14 |
| species_Gentoo | 1.14 |
| species_Gentoo | 1.15 |
<sup>Created on 2022-06-18 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)</sup>
<details style="margin-bottom:10px;">
<summary>
Session info
</summary>
```r
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#> setting value
#> version R version 4.1.3 (2022-03-10)
#> os Windows 10 x64 (build 22000)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United Kingdom.1252
#> ctype English_United Kingdom.1252
#> tz Europe/London
#> date 2022-06-18
#> pandoc 2.18 @ C:/PROGRA~3/chocolatey/bin/ (via rmarkdown)
#>
#> - Packages -------------------------------------------------------------------
#> package * version date (UTC) lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.2)
#> backports 1.4.1 2021-12-13 [1] CRAN (R 4.1.2)
#> broom * 0.8.0 2022-04-13 [1] CRAN (R 4.1.3)
#> broom.helpers 1.6.0 2022-01-12 [1] CRAN (R 4.1.2)
#> class 7.3-20 2022-01-13 [1] CRAN (R 4.1.2)
#> cli 3.2.0 2022-02-14 [1] CRAN (R 4.1.2)
#> codetools 0.2-18 2020-11-04 [2] CRAN (R 4.1.3)
#> colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.1.3)
#> crayon 1.5.1 2022-03-26 [1] CRAN (R 4.1.3)
#> DBI 1.1.2 2021-12-20 [1] CRAN (R 4.1.2)
#> dials * 0.1.0 2022-01-31 [1] CRAN (R 4.1.2)
#> DiceDesign 1.9 2021-02-13 [1] CRAN (R 4.1.2)
#> digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.2)
#> dplyr * 1.0.8 2022-02-08 [1] CRAN (R 4.1.2)
#> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.2)
#> evaluate 0.15 2022-02-18 [1] CRAN (R 4.1.2)
#> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.1.3)
#> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.2)
#> foreach 1.5.2 2022-02-02 [1] CRAN (R 4.1.2)
#> fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.2)
#> furrr 0.2.3 2021-06-25 [1] CRAN (R 4.1.2)
#> future 1.24.0 2022-02-19 [1] CRAN (R 4.1.3)
#> future.apply 1.8.1 2021-08-10 [1] CRAN (R 4.1.2)
#> generics 0.1.2 2022-01-31 [1] CRAN (R 4.1.2)
#> ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.2)
#> glmnet * 4.1-4 2022-04-15 [1] CRAN (R 4.1.3)
#> globals 0.14.0 2020-11-22 [1] CRAN (R 4.1.1)
#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.1.3)
#> gower 1.0.0 2022-02-03 [1] CRAN (R 4.1.2)
#> GPfit 1.0-8 2019-02-08 [1] CRAN (R 4.1.2)
#> gt 0.6.0 2022-05-24 [1] CRAN (R 4.1.3)
#> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.2)
#> gtsummary 1.5.2 2022-01-29 [1] CRAN (R 4.1.2)
#> hardhat 0.2.0 2022-01-24 [1] CRAN (R 4.1.2)
#> highr 0.9 2021-04-16 [1] CRAN (R 4.1.2)
#> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.2)
#> infer * 1.0.0 2021-08-13 [1] CRAN (R 4.1.2)
#> ipred 0.9-12 2021-09-15 [1] CRAN (R 4.1.2)
#> iterators 1.0.14 2022-02-05 [1] CRAN (R 4.1.2)
#> knitr 1.39 2022-04-26 [1] CRAN (R 4.1.3)
#> lattice 0.20-45 2021-09-22 [2] CRAN (R 4.1.3)
#> lava 1.6.10 2021-09-02 [1] CRAN (R 4.1.2)
#> lhs 1.1.5 2022-03-22 [1] CRAN (R 4.1.3)
#> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.2)
#> listenv 0.8.0 2019-12-05 [1] CRAN (R 4.1.2)
#> lubridate 1.8.0 2021-10-07 [1] CRAN (R 4.1.2)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.1.3)
#> MASS 7.3-55 2022-01-13 [1] CRAN (R 4.1.2)
#> Matrix * 1.4-0 2021-12-08 [2] CRAN (R 4.1.3)
#> modeldata * 0.1.1 2021-07-14 [1] CRAN (R 4.1.2)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.2)
#> nnet 7.3-17 2022-01-13 [1] CRAN (R 4.1.2)
#> parallelly 1.30.0 2021-12-17 [1] CRAN (R 4.1.2)
#> parsnip * 0.2.1.9000 2022-03-29 [1] Github (tidymodels/parsnip@9ce41c8)
#> pillar 1.7.0 2022-02-01 [1] CRAN (R 4.1.2)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.2)
#> plyr 1.8.7 2022-03-24 [1] CRAN (R 4.1.3)
#> pROC 1.18.0 2021-09-03 [1] CRAN (R 4.1.2)
#> prodlim 2019.11.13 2019-11-17 [1] CRAN (R 4.1.2)
#> purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.1.2)
#> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.1.2)
#> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.1.1)
#> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.1.1)
#> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.1.2)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.2)
#> Rcpp 1.0.8.3 2022-03-17 [1] CRAN (R 4.1.3)
#> recipes * 0.2.0 2022-02-18 [1] CRAN (R 4.1.2)
#> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.2)
#> rlang 1.0.2 2022-03-04 [1] CRAN (R 4.1.3)
#> rmarkdown 2.14 2022-04-25 [1] CRAN (R 4.1.3)
#> rpart 4.1.16 2022-01-24 [1] CRAN (R 4.1.2)
#> rsample * 0.1.1 2021-11-08 [1] CRAN (R 4.1.2)
#> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.2)
#> scales * 1.2.0 2022-04-13 [1] CRAN (R 4.1.3)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.2)
#> shape 1.4.6 2021-05-19 [1] CRAN (R 4.1.1)
#> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.2)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.2)
#> styler 1.7.0 2022-03-13 [1] CRAN (R 4.1.3)
#> survival 3.2-13 2021-08-24 [2] CRAN (R 4.1.3)
#> tibble * 3.1.6 2021-11-07 [1] CRAN (R 4.1.2)
#> tidymodels * 0.2.0 2022-03-19 [1] CRAN (R 4.1.3)
#> tidyr * 1.2.0 2022-02-01 [1] CRAN (R 4.1.2)
#> tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.1.3)
#> timeDate 3043.102 2018-02-21 [1] CRAN (R 4.1.1)
#> tune * 0.2.0.9000 2022-03-29 [1] Github (tidymodels/tune@6ed30a4)
#> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.2)
#> vctrs 0.4.1 2022-04-13 [1] CRAN (R 4.1.3)
#> withr 2.5.0 2022-03-03 [1] CRAN (R 4.1.3)
#> workflows * 0.2.6 2022-03-18 [1] CRAN (R 4.1.3)
#> workflowsets * 0.2.1 2022-03-15 [1] CRAN (R 4.1.3)
#> xfun 0.30 2022-03-02 [1] CRAN (R 4.1.3)
#> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.1.2)
#> yardstick * 0.0.9 2021-11-22 [1] CRAN (R 4.1.2)
#>
#> [1] C:/Users/rmkja/Documents/R/win-library/4.1
#> [2] C:/Program Files/R/R-4.1.3/library
#>
#> ------------------------------------------------------------------------------
</details>
(small edit)
It looks like an issue in gtsummary:
library(tidymodels)
data(penguins)
penguins <- penguins %>% na.omit()
my_split <- penguins %>%
initial_split()
rec <- recipe(sex ~ species + bill_length_mm + bill_depth_mm,
data = penguins
) %>%
step_dummy(species)
x <- prep(rec) %>% bake(-sex, new_data = NULL, composition = "matrix")
y <- penguins$sex
glmn_fit <- glmnet::glmnet(x, y, alpha = 1, family = "binomial")
glmn_fit %>%
gtsummary::tbl_regression(exponentiate = TRUE)
#> ✖ Unable to identify the list of variables.
#>
#> This is usually due to an error calling `stats::model.frame(x)`or `stats::model.matrix(x)`.
#> It could be the case if that type of model does not implement these methods.
#> Rarely, this error may occur if the model object was created within
#> a functional programming framework (e.g. using `lappy()`, `purrr::map()`, etc.).
(removed identical html results)
They do not have a glmnet
method for tbl_regression()
so the default method is being called. That doesn't work well for this type of object.