Accessing linear model statistics inside of tidymodel

Is there a tidymodels way to extract all the statistics from a parsnip model of linear regression?

What I want is the same results as obtained with

summary(lm(mpg ~ wt, mtcars))

I could do it this way:

library(tidymodels)

fit_tidy <- linear_reg() %>%
  set_engine("lm") %>%
  fit(mpg ~ wt, mtcars)

summary(fit_tidy$fit)

But I wonder if there is a function from tidymodels to do that.

All help very much appreciated!

tidymodels incorporates the broom library, which contains the tidy() function

tidy(fit_tidy)
1 Like

Thank you! This is very helpful! But, still, I need the other metrics too, such as Residual Standard error, Multiple and Adjusted R squared, and so on.

Maybe the glance() method from broom is probably what you want?

library(parsnip)
library(broom)

fit_tidy <- linear_reg() %>%
  set_engine("lm") %>%
  fit(mpg ~ wt, mtcars)

glance(fit_tidy)
#> # A tibble: 1 × 12
#>   r.squared adj.r.squared sigma statistic  p.value    df logLik   AIC   BIC
#>       <dbl>         <dbl> <dbl>     <dbl>    <dbl> <dbl>  <dbl> <dbl> <dbl>
#> 1     0.753         0.745  3.05      91.4 1.29e-10     1  -80.0  166.  170.
#> # … with 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>

Created on 2022-10-11 by the reprex package (v2.0.1)

1 Like

What an honor to have an answer from you, Max! I attended ML with Tidymodels workshop in Rstudio conference, and also got your book - signed!

Actually, this is not what I wanted, but it is also interesting, because it contains some metrics that the base R way do not deliver from the summary(fit) command (AIC, BIC, etc.).

I wanted the exact same output from summary(fit) because I am working on explaining one by one in a blog post.

Maybe I will add the tidymodels version and explain the other metrics as well.

Thank you very much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.