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

How to specify the expected returned statistics for cases when there are errors? #316

Closed
ddsjoberg opened this issue Sep 9, 2024 · 1 comment · Fixed by #361
Closed
Assignees

Comments

@ddsjoberg
Copy link
Collaborator

ddsjoberg commented Sep 9, 2024

Related to insightsengineering/cardx#203

If a user were to pass a function to ard_continuous() that returns a tibble or named list, then what we get back is far less informative. We should have some capability of also passing what is expected to be returned, so in the case of an error, we can returned an ARD of the correct structure.

library(cards)

# great when there are no errors
ard_continuous(
  ADSL,
  variables = AGE, 
  statistic = everything() ~ list(ttest = \(x) t.test(x) |> broom::tidy())
)
#> {cards} data frame: 8 x 8
#>   variable   context   stat_name stat_label      stat fmt_fn
#> 1      AGE continuo…    estimate   estimate    75.087      1
#> 2      AGE continuo…   statistic  statistic   145.119      1
#> 3      AGE continuo…     p.value    p.value         0      1
#> 4      AGE continuo…   parameter  parameter       253      1
#> 5      AGE continuo…    conf.low   conf.low    74.068      1
#> 6      AGE continuo…   conf.high  conf.high    76.106      1
#> 7      AGE continuo…      method     method One Samp…   <fn>
#> 8      AGE continuo… alternative  alternat… two.sided   <fn>
#> ℹ 2 more variables: warning, error

# but when there is an error, it's unclear 
ard_continuous(
  ADSL,
  variables = AGEGR1, 
  statistic = everything() ~ list(ttest = \(x) t.test(x) |> broom::tidy())
) |> 
  as.data.frame()
#>   variable    context stat_name stat_label stat                     fmt_fn
#> 1   AGEGR1 continuous     ttest      ttest NULL .Primitive("as.character")
#>                                                                        warning
#> 1 argument is not numeric or logical: returning NA, NAs introduced by coercion
#>                                   error
#> 1 missing value where TRUE/FALSE needed

Created on 2024-09-08 with reprex v2.1.0

The first thing that comes to mind is assigning an attribute to the function itself of the expected statistics to be returned. Then if there is an error, we can inspect the function itself for the expected statistics. It's not great....but the only idea I have for now

@ddsjoberg ddsjoberg added this to the v0.3.0 milestone Sep 9, 2024
@ddsjoberg ddsjoberg removed this from the v0.3.0 milestone Oct 2, 2024
@ddsjoberg ddsjoberg added this to the Release v0.4.0 milestone Nov 11, 2024
@ddsjoberg
Copy link
Collaborator Author

ddsjoberg commented Nov 19, 2024

IDEA: Export a function that makes it easy to add an attribute with the expected return. The function takes in a function and a vector of stat_names. In the case of an error, the expected stat_names would be sued to construct an empty ARD (with the error column populated).

The function could also specify other items like the the fmt_fn.

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.

1 participant