tidymodels / parsnip

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

check_args() doesn't handle expression that well #1099

Open EmilHvitfeldt opened 3 months ago

EmilHvitfeldt commented 3 months ago

From https://github.com/tidymodels/parsnip/blob/dc9a66e2424d2aed3d5c6663f1a997fdb5fac0ec/R/README.md?plain=1#L58

In the following example if you jump into check_args(), it passes but for the wrong reasons.

check_args() doesn't eval in the environment. so floor(nrow(data)/3) becomes nuemric(0) because nrow(data) is NULL because data is a function...

Ideally we should handle it better. There might be reasons why we don't, but we should be sure. it also means that the checking right now needs to be looser than I would like, as checking "1 or 0" length isn't as good as checking "1" length.

library(parsnip)
library(discrim)

discrim_linear(penalty = floor(nrow(data)/3)) |>
  fit(Species ~ ., data = iris)
#> parsnip model object
#> 
#> Call:
#> lda(Species ~ ., data = data)
#> 
#> Prior probabilities of groups:
#>     setosa versicolor  virginica 
#>  0.3333333  0.3333333  0.3333333 
#> 
#> Group means:
#>            Sepal.Length Sepal.Width Petal.Length Petal.Width
#> setosa            5.006       3.428        1.462       0.246
#> versicolor        5.936       2.770        4.260       1.326
#> virginica         6.588       2.974        5.552       2.026
#> 
#> Coefficients of linear discriminants:
#>                     LD1         LD2
#> Sepal.Length  0.8293776 -0.02410215
#> Sepal.Width   1.5344731 -2.16452123
#> Petal.Length -2.2012117  0.93192121
#> Petal.Width  -2.8104603 -2.83918785
#> 
#> Proportion of trace:
#>    LD1    LD2 
#> 0.9912 0.0088

Created on 2024-04-02 with reprex v2.1.0