Skip to content

lo() in formula fails in lapply-type implementation #23

@mkoohafkan

Description

@mkoohafkan

It looks like there is an eval/substitute bug somewhere when using lo() with gamlss() inside a tidyverse/purrr:map() framework.

Reprex:

library(dplyr)
library(tidyr)
library(gamlss)


nested_data = abdom |>
  mutate(group = "A") |>
  group_nest(group, .key = "data")

nested_data |>
  mutate(fit = map(data, function(d) {
    gamlss(y~lo(~x), data=d)
  }))
#> Error in `mutate()`:
#> ℹ In argument: `fit = map(...)`.
#> Caused by error in `map()`:
#> ℹ In index: 1.
#> Caused by error:
#> ! object 'd' not found
#> Run `rlang::last_trace()` to see where the error occurred.

Even more troubling: if d is defined in the parent environment, gamlss will try to use that dataset instead.

d = abdom

# uses d from environment instead of function environment
nested_data |>
  mutate(fit = map(data, function(d) {
    gamlss(y~lo(~x), data=d)
  }))
#> GAMLSS-RS iteration 1: Global Deviance = 4936.64 
#> GAMLSS-RS iteration 2: Global Deviance = 4936.64 
#> # A tibble: 1 × 3
#>   group               data fit
#>   <chr> <list<tibble[,2]>> <list>
#> 1 A              [610 × 2] <gamlss>
#> Warning message:
#> There were 3 warnings in `mutate()`.
#> The first warning was:
#> ℹ In argument: `fit = map(...)`.
#> Caused by warning in `additive.fit()`:
#> ! additive.fit convergence not obtained in  30  iterations
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 2 remaining warnings.

# remove d from parent environment and try again
rm(d)
nested_data |>
  mutate(fit = map(data, function(d) {
    gamlss(y~lo(~x), data=d)
  }))
#> Error in `mutate()`:
#> ℹ In argument: `fit = map(...)`.
#> Caused by error in `map()`:
#> ℹ In index: 1.
#> Caused by error:
#> ! object 'd' not found
#> Run `rlang::last_trace()` to see where the error occurred.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions