Search code examples
rbroomtidymodels

Error: No tidy method for objects of class function :: broom.mixed


I am trying to perform a linear regression fit using tidymodels,parsnip but encounters the following error:

Error: No tidy method for objects of class function

routine:

library(tidymodels)
library(parsnip)
library(broom.mixed)

linear_reg() %>% 
  set_engine("lm") %>% 
  fit(formula = cnt ~ temp_raw, data = bikeshare)
fit %>% tidy()
fit %>% glance()

Having read this post Tidy function gives this error: No tidy method for objects of class lmerMod. It will not work on my computer, but works in pdf with same code

and I tried broom.mixed but still the error persists.


Solution

  • The main issue is that you need to assign the fitted model to an object; in your case it would also be fit.

    There are two other points to consider:

    • it's confusing/not best practice to assign variables with the same name as R functions (i.e. you might want to call your fit fit0 or my_fit or something rather than fit); usually you can get away with it but it breaks, confusingly, in some contexts
    • broom.mixed is a red herring. The broom package is actually used for lm fits (and you don't need to load it, apparently tidymodels loads it (and parsnip) automatically ...)
    library(tidymodels)
    fit <- linear_reg()  %>%  
        set_engine("lm") %>%  
        fit(formula = mpg ~ cyl, data = mtcars)
    fit %>% tidy()
    fit %>% glance()