building parsnipe model for nlme::gls function, issue with formula/model argument

Hi,

I'm trying to build a parsnip model for the nlme::gls function following steps available at Learn - How to build a parsnip model. When I test the built model again Ovary dataset (associated to nlme package) I got the error message 'unused formula argument' (see reproducible code below). My understanding is that nlme::gls argument for formula is 'model' (see ?gls) and not formula. I tried to modify using set_model_arg(parsnip = "formula", original = "model"), but it didn't help.
Happy and grateful for any support!

Kevin

library(tidymodels)
#> Warning: package 'tidymodels' was built under R version 4.0.5
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
#> Warning: package 'broom' was built under R version 4.0.5
#> Warning: package 'dials' was built under R version 4.0.5
#> Warning: package 'dplyr' was built under R version 4.0.5
#> Warning: package 'ggplot2' was built under R version 4.0.5
#> Warning: package 'infer' was built under R version 4.0.5
#> Warning: package 'parsnip' was built under R version 4.0.5
#> Warning: package 'recipes' was built under R version 4.0.5
#> Warning: package 'rsample' was built under R version 4.0.5
#> Warning: package 'tibble' was built under R version 4.0.4
#> Warning: package 'tidyr' was built under R version 4.0.5
#> Warning: package 'tune' was built under R version 4.0.5
#> Warning: package 'workflows' was built under R version 4.0.5
#> Warning: package 'workflowsets' was built under R version 4.0.5
#> Warning: package 'yardstick' was built under R version 4.0.5
library(nlme)
#> 
#> Attaching package: 'nlme'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse

set_new_model("gls_tidy")
set_model_mode(model = "gls_tidy", mode = "regression")
set_model_engine(
  "gls_tidy", 
  mode = "regression", 
  eng = "gls"
)

set_dependency("gls_tidy", eng = "gls", pkg = "nlme")

set_model_arg(
  model = "gls_tidy",
  eng = "gls",
  parsnip = "correlation",
  original = "correlation",
  func = list(pkg = "nlme", fun = "bar"),
  has_submodel = FALSE
)
set_model_arg(
  model = "gls_tidy",
  eng = "gls",
  parsnip = "formula",
  original = "model",
  func = list(pkg = "nlme", fun = "gls"),
  has_submodel = FALSE
)
show_model_info("gls_tidy")
#> Information for `gls_tidy`
#>  modes: unknown, regression 
#> 
#>  engines: 
#>    regression: gls
#> 
#>  arguments: 
#>    gls: 
#>       correlation --> correlation
#>       formula     --> model
#> 
#>  no registered fit modules.
#> 
#>  no registered prediction modules.

gls_tidy <-
  function(mode = "regression", model=NULL, correlation = NULL) {
    # Check for correct mode
    if (mode  != "regression") {
      rlang::abort("`mode` should be 'regression'")
    }
    
    # Capture the arguments in quosures
    args <- list(correlation = rlang::enquo(correlation))
    
    # Save some empty slots for future parts of the specification
    new_model_spec(
      "gls_tidy",
      args = args,
      eng_args = NULL,
      mode = mode,
      method = NULL,
      engine = NULL
    )
  }
set_fit(
  model = "gls_tidy",
  eng = "gls",
  mode = "regression",
  value = list(
    interface = "formula",
    protect = c("formula", "data"),
    func = c(pkg = "nlme", fun = "gls"),
    defaults = list()
  )
)
set_encoding(
  model = "gls_tidy",
  eng = "gls",
  mode = "regression",
  options = list(
    predictor_indicators = "traditional",
    compute_intercept = TRUE,
    remove_intercept = TRUE,
    allow_sparse_x = FALSE
  )
)

show_model_info("gls_tidy")
#> Information for `gls_tidy`
#>  modes: unknown, regression 
#> 
#>  engines: 
#>    regression: gls
#> 
#>  arguments: 
#>    gls: 
#>       correlation --> correlation
#>       formula     --> model
#> 
#>  fit modules:
#>  engine       mode
#>     gls regression
#> 
#>  no registered prediction modules.

gls_tidy(model=NULL,correlation = corGaus(1, form = ~ x + y)) %>%
  translate(engine = "gls")
#> Model Specification (regression)
#> 
#> Main Arguments:
#>   correlation = corGaus(1, form = ~x + y)
#> 
#> Computational engine: gls 
#> 
#> Model fit template:
#> nlme::gls(formula = missing_arg(), data = missing_arg(), correlation = corGaus(1, 
#>     form = ~x + y))

gls_spec <- gls_tidy(correlation = corGaus(1, form = ~ x + y)) %>%
  set_engine("gls")

gls_fit <- gls_spec %>%
  fit(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), data = Ovary, engine = "gls")
#> Error in nlme::gls(formula = follicles ~ sin(2 * pi * Time) + cos(2 * : unused argument (formula = follicles ~ sin(2 * pi * Time) + cos(2 * pi * Time))
#> Timing stopped at: 0 0 0

Created on 2021-08-26 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.