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

print(include_reference = TRUE) doesn't work with pipes #923

Closed
snhansen opened this issue Dec 1, 2023 · 1 comment · Fixed by #924
Closed

print(include_reference = TRUE) doesn't work with pipes #923

snhansen opened this issue Dec 1, 2023 · 1 comment · Fixed by #924
Assignees

Comments

@snhansen
Copy link
Contributor

snhansen commented Dec 1, 2023

The following example shows that the include_reference = TRUE option of print() doesn't work with pipes.

library(parameters)
lm(Sepal.Length ~ Petal.Length + Species, data = iris) |> parameters() |> print(include_reference = TRUE)

# Parameter            | Coefficient |   SE |         95% CI | t(146) |      p
# ----------------------------------------------------------------------------
# (Intercept)          |        3.68 | 0.11 | [ 3.47,  3.89] |  34.72 | < .001
# Petal Length         |        0.90 | 0.06 | [ 0.78,  1.03] |  13.96 | < .001
# Species [versicolor] |       -1.60 | 0.19 | [-1.98, -1.22] |  -8.28 | < .001
# Species [virginica]  |       -2.12 | 0.27 | [-2.66, -1.58] |  -7.74 | < .001

model_name <- lm(Sepal.Length ~ Petal.Length + Species, data = iris)
model_name |> parameters() |> print(include_reference = TRUE)

# Parameter            | Coefficient |   SE |         95% CI | t(146) |      p
# ----------------------------------------------------------------------------
# (Intercept)          |        3.68 | 0.11 | [ 3.47,  3.89] |  34.72 | < .001
# Petal Length         |        0.90 | 0.06 | [ 0.78,  1.03] |  13.96 | < .001
# Species [setosa]     |        0.00 |      |                |        |       
# Species [versicolor] |       -1.60 | 0.19 | [-1.98, -1.22] |  -8.28 | < .001
# Species [virginica]  |       -2.12 | 0.27 | [-2.66, -1.58] |  -7.74 | < .001

I believe the issue stems from the first lines of the .add_reference_level() function. Here we have

.add_reference_level <- function(params) {
  # check if we have a model object, else return parameter table
  model <- .get_object(params)
  if (is.null(model)) {
    params
  }
...
}

and if we look at .get_object() we can see that NULL is returned if the model object doesn't have an object_name attribute which it won't have if it's part of a pipe.

.get_object <- function(x, attribute_name = "object_name") {
  obj_name <- attr(x, attribute_name, exact = TRUE)
  model <- NULL
  if (!is.null(obj_name)) {
    model <- .safe(get(obj_name, envir = parent.frame()))
    # prevent self reference
    if (is.null(model) || inherits(model, "parameters_model")) {
      model <- .safe(get(obj_name, envir = globalenv()))
    }
  }
  model
}
@strengejacke
Copy link
Member

Thanks, should be fixed in #924:

library(parameters)
lm(Sepal.Length ~ Petal.Length + Species, data = iris) |>
  parameters() |>
  print(include_reference = TRUE)
#> Parameter            | Coefficient |   SE |         95% CI | t(146) |      p
#> ----------------------------------------------------------------------------
#> (Intercept)          |        3.68 | 0.11 | [ 3.47,  3.89] |  34.72 | < .001
#> Petal Length         |        0.90 | 0.06 | [ 0.78,  1.03] |  13.96 | < .001
#> Species [setosa]     |        0.00 |      |                |        |       
#> Species [versicolor] |       -1.60 | 0.19 | [-1.98, -1.22] |  -8.28 | < .001
#> Species [virginica]  |       -2.12 | 0.27 | [-2.66, -1.58] |  -7.74 | < .001
#> 
#> Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
#>   using a Wald t-distribution approximation.

Created on 2023-12-01 with reprex v2.0.2

I guess pipe-workflow will also fail in other situations, maybe we can in general improve stability when we rely on the call if no object name is available.

strengejacke added a commit that referenced this issue Dec 3, 2023
* `print(include_reference = TRUE)` doesn't work with pipes
Fixes #923

* style

* lintr

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

Successfully merging a pull request may close this issue.

2 participants