parsnip icon indicating copy to clipboard operation
parsnip copied to clipboard

Namespacing issue with MARS models

Open alexpghayes opened this issue 4 years ago • 1 comments

I shouldn't need to explicitly load earth but I do.

library(parsnip)

mars(
  mode = "classification",
  num_terms = 1,
  prod_degree = 1,
  prune_method = "backward"
) %>% 
  set_engine("earth") %>% 
  fit(Species ~ ., iris)
#> Error in get(ctr, mode = "function", envir = parent.frame()): object 'contr.earth.response' of mode 'function' was not found
#> Timing stopped at: 0 0 0

library(earth)
#> Loading required package: Formula
#> Loading required package: plotmo
#> Loading required package: plotrix
#> Loading required package: TeachingDemos

mars(
  mode = "classification",
  num_terms = 1,
  prod_degree = 1,
  prune_method = "backward"
) %>% 
  set_engine("earth") %>% 
  fit(Species ~ ., iris)
#> parsnip model object
#> 
#> Fit time:  50ms 
#> GLM (family binomial, link logit):
#>            nulldev  df       dev  df   devratio     AIC iters converged
#> setosa     190.954 149   190.954 149          0     193     4         1
#> versicolor 190.954 149   190.954 149          0     193     4         1
#> virginica  190.954 149   190.954 149          0     193     4         1
#> 
#> Earth selected 1 of 15 terms, and 0 of 4 predictors
#> Termination condition: Reached nk 21
#> Importance: Sepal.Length-unused, Sepal.Width-unused, Petal.Length-unused, ...
#> Number of terms at each degree of interaction: 1 (intercept only model)
#> 
#> Earth
#>                  GCV       RSS GRSq RSq
#> setosa     0.2252151  33.33333    0   0
#> versicolor 0.2252151  33.33333    0   0
#> virginica  0.2252151  33.33333    0   0
#> All        0.6756452 100.00000    0   0

Created on 2020-01-05 by the reprex package (v0.3.0)

alexpghayes avatar Jan 05 '20 23:01 alexpghayes

I investigated why we are getting this issue. earth.default() is calling expand.arg.modvars()

https://github.com/cran/earth/blob/213f668f255d3b80cdb82821cd04fe53b379828f/R/earth.R#L101-L105

which temporary sets the options() to contrasts=c("contr.earth.response", "contr.earth.response")

https://github.com/cran/earth/blob/213f668f255d3b80cdb82821cd04fe53b379828f/R/expand.arg.R#L60-L62

which is a unexported function

https://github.com/cran/earth/blob/213f668f255d3b80cdb82821cd04fe53b379828f/R/expand.arg.R#L97-L111

So the issues is actually, that you can't use earth() without loading the namespace.

earth::earth(formula = Species ~ ., data = iris)
#> Error in get(ctr, mode = "function", envir = parent.frame()): object 'contr.earth.response' of mode 'function' was not found

library(earth)
#> Loading required package: Formula
#> Loading required package: plotmo
#> Loading required package: plotrix
#> Loading required package: TeachingDemos

earth::earth(formula = Species ~ ., data = iris)
#> Selected 9 of 15 terms, and 4 of 4 predictors
#> Termination condition: Reached nk 21
#> Importance: Petal.Length, Petal.Width, Sepal.Width, Sepal.Length
#> Number of terms at each degree of interaction: 1 8 (additive model)
#> 
#>                   GCV       RSS      GRSq       RSq
#> setosa     0.00089390 0.1054146 0.9960309 0.9968376
#> versicolor 0.02859205 3.3717648 0.8730456 0.8988471
#> virginica  0.02743849 3.2357302 0.8781676 0.9029281
#> All        0.05692444 6.7129096 0.9157480 0.9328709

Created on 2022-01-05 by the reprex package (v2.0.1)

Another note. Since the way contrasts work, contr.earth.response() isn't being pulled from earth but just the one it can find.

contr.earth.response <- function(x, base, contrasts) {
  contr <- array(0, c(length(x), length(x)), list(x, x))
  diag(contr) <- 1
  contr
}

earth::earth(formula = Species ~ ., data = iris)
#> Selected 9 of 15 terms, and 4 of 4 predictors
#> Termination condition: Reached nk 21
#> Importance: Petal.Length, Petal.Width, Sepal.Width, Sepal.Length
#> Number of terms at each degree of interaction: 1 8 (additive model)
#> 
#>                   GCV       RSS      GRSq       RSq
#> setosa     0.00089390 0.1054146 0.9960309 0.9968376
#> versicolor 0.02859205 3.3717648 0.8730456 0.8988471
#> virginica  0.02743849 3.2357302 0.8781676 0.9029281
#> All        0.05692444 6.7129096 0.9157480 0.9328709

Created on 2022-01-05 by the reprex package (v2.0.1)

EmilHvitfeldt avatar Jan 06 '22 05:01 EmilHvitfeldt

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[bot] avatar Mar 25 '23 00:03 github-actions[bot]