tidymodels / broom

Convert statistical analysis objects from R into tidy format
https://broom.tidymodels.org
Other
1.45k stars 304 forks source link

Feature request: report valid parameters for `tidy` and `glance` #208

Closed hughjonesd closed 6 years ago

hughjonesd commented 7 years ago

Hi,

I'm writing a regression table creator for https://hughjonesd.github.io/huxtable . It uses broom as a backend: very nice.

I would like to know, for a given object x, whether I can call tidy(x, conf.int = TRUE). At the moment, I look at the formals of the relevant object:

has_builtin_ci <- function (x) {
  objs <- sapply(class(x), function (y) try(getS3method('tidy', y), silent = TRUE))
  obj <- Find(function(x) class(x) == 'function', objs)
  if (is.null(obj)) return(FALSE)
  argnames <- names(formals(obj))
  all(c('conf.int', 'conf.level') %in% argnames)
}

This is a bit imperfect (did you know R has no built in way of telling you what function will be called on a given object?)

It would be nice if I could find out, for a given object or class, (a) what parameters are available for tidy and glance and perhaps also (b) what columns they will return.

I think this fits with broom's idea of providing more guarantees of "what to expect" from model output.

If you're open to a pull request, tell me and I will see what I can do.

alexpghayes commented 6 years ago

I'm a fan. What about tidy_arguments(object), glance_arguments(object) and augment_arguments(object) or something along those lines, each return a character vector of arguments available for object?

dgrtwo commented 6 years ago

Generally I think this is better handled by documentation, such as doing ?tidy.lm or ?tidy.glmnet.

I appreciate that you want to dig into this programmatically for the table function, but I don't think there's any reason for it to live in broom any more than other packages would have methods for listing their arguments. I would approach it programmatically similarly to your code approach:

names(as.list(args(getS3method("tidy", "lm"))))

I think handling it beyond this level of programming would be challenging to maintain for broom maintainers.

hughjonesd commented 6 years ago

TBH I now find @dgrtwo's arguments convincing re my original point (a) - what arguments a function takes. There's no reason function/method introspection should belong in broom, it's a more general problem. (For rlang maybe?)

There might be places where tidy and glance could provide information about (b) - what they return. That is harder to discover by automatic code introspection. The alternative is to pass in an object of class whatever and see what you get back... but that can easily become unwieldy.

I am thinking of something like:

tidy_doc(obj_or_class)

returning a data frame of "column names" and "descriptions". This is info that ?tidy.whatever ought to contain anyway, so perhaps it could be autogenerated to/from roxygen. Again, say if you're open to a pull request.

alexpghayes commented 6 years ago

Sorry for the delay getting back to you. I think this is an interesting idea although am not entirely sure how I feel about it yet.

I'm interested in strictly enforcing a set of allowable column names in tidy output, but I don't think that solves your problem.

I suspect that autogenerating from roxygen will be rather unwieldy because we've been pretty lenient when accepting pull requests and the documentation is of varied quality at the moment.

At some point soon I'm going to need to go through the documentation to clean it up. At the moment, I'm planning to follow up after that.

alexpghayes commented 6 years ago

Actually, this is going to happen. Closing in favor of #419, where I'm posting updates.

github-actions[bot] commented 3 years ago

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.