tidymodels / parsnip

A tidy unified interface to models
https://parsnip.tidymodels.org
Other
586 stars 88 forks source link

Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "c('elnet', 'glmnet')" #249

Closed konradsemsch closed 4 years ago

konradsemsch commented 4 years ago

I'm attaching a zipped rds object that contains all the information to make this example reproducible. I'm essentially trying to make a prediction on a fitted workflow, but that gives me the following error. Could you please take a look?

library(recipes)
library(parsnip)

df_input <- diamonds[1, ]
pipeline <- readRDS("/pipeline.rds")

predict(pipeline$workflow, df_input)

Giving the following error:

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "c('elnet', 'glmnet')"

pipeline.rds.zip

EmilHvitfeldt commented 4 years ago

Since you have a workflows object with a fitted glmnet you need to load both workflows and glmnet to predict so it knows what methods to dispatch.

library(recipes)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> 
#> Attaching package: 'recipes'
#> The following object is masked from 'package:stats':
#> 
#>     step
library(parsnip)
library(workflows)
library(glmnet)
#> Loading required package: Matrix
#> Loaded glmnet 3.0-2
library(zeallot)
library(ggplot2)

df_input <- diamonds[1, ]
pipeline <- readRDS("~/Downloads/pipeline")
#> Warning: namespace 'rmlpackage' is not available and has been replaced
#> by .GlobalEnv when processing object ''

predict(pipeline$workflow, df_input)
#> # A tibble: 1 x 1
#>   .pred
#>   <dbl>
#> 1  4.91

Created on 2019-12-20 by the reprex package (v0.3.0)

konradsemsch commented 4 years ago

Thanks, that helped!

jgaeb commented 4 years ago

Why is it that it's not necessary to load glmnet to actually train the underlying model, but it is necessary to load it to make predictions? For instance in this model notebook, ranger is never attached to the global namespace.

topepo commented 4 years ago

We do load glmnet to fit the model but you might not see that since it is not attached. This SO link does a good job at explaining the difference.

We don't attach so that there are not conflicts in function names.

The currrent reason that you need to load a package is because we are not doing the same thing for predictions (but #308 will fix that).

In the future, it would be better to open a new issue rather than append to a closed one.

jgaeb commented 4 years ago

Great, thanks for the explanation! I'll be sure to open a new issue or post in the RStudio community as appropriate in the future.

github-actions[bot] commented 3 years ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.