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

variable transformed with I() seen as string, and variable not found #291

Open
F041 opened this issue Nov 4, 2024 · 1 comment
Open

Comments

@F041
Copy link

F041 commented Nov 4, 2024

I have a model like that

lmc= lm(yc~I(x1)^2+log(x2), data=data)
So not built with a recipe like specified here

When I run the API, locally, I see

[
  {
    "I(x1)": "**string**",
    "log(x2)": 0
  }
]

but of course I have it as numeric.

For the second variable, I get

{
  "error": "500 - Internal server error",
  "message": "Error in eval(predvars, data, env): object 'x2' not found\n"
}

I don't feel sure what I posted belongs to an issue, since I stated I deviated from the recipe good practice. Maybe it can just lead to a specification in the documentation.

@juliasilge
Copy link
Member

I think this would work if you used the tidymodels add_formula() interface:

library(tidymodels)
#> Warning: package 'broom' was built under R version 4.4.1
library(vetiver)
#> 
#> Attaching package: 'vetiver'
#> The following object is masked from 'package:tune':
#> 
#>     load_pkgs

wf <- 
    workflow() |> 
    add_model(linear_reg()) |> 
    add_formula(mpg ~ I(cyl) + log(disp)) |> 
    fit(data = mtcars)

vetiver_ptype(wf)
#> # A tibble: 0 × 2
#> # ℹ 2 variables: cyl <dbl>, disp <dbl>

Created on 2024-11-08 with reprex v2.1.1

You can definitely use lm() directly with a simple formula, like this example, but if you start using more complex formula syntax then lm() doesn't give you a way to get out the true inputs. For example:

library(vetiver)
mod <- lm(mpg ~ I(cyl) + log(disp), data = mtcars)
vetiver_ptype(mod)
#> # A tibble: 0 × 2
#> # ℹ 2 variables: I(cyl) <I<dbl>>, log(disp) <dbl>

Created on 2024-11-08 with reprex v2.1.1

I will look for a way to clarify in the documentation these limitations of the formula interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants