Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
konradsemsch opened this issue Dec 18, 2019 · 6 comments

Comments

@konradsemsch
Copy link

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
Copy link
Member

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
Copy link
Author

Thanks, that helped!

@jgaeb
Copy link

jgaeb commented May 27, 2020

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
Copy link
Member

topepo commented May 27, 2020

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
Copy link

jgaeb commented May 27, 2020

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
Copy link

github-actions bot commented Mar 7, 2021

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.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants