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

Add arguments to rename polynomials with coef_rename() #778

Merged

Conversation

mccarthy-m-g
Copy link
Contributor

This PR updates coef_rename() to handle polynomial coefficients created by the poly() function, similar to how the function currently handles factors. Here's some reprex code to check the new functionality out:

x <- list(
    lm(mpg ~ poly(cyl, 2) + drat + disp, data = mtcars),
    lm(hp ~ poly(cyl, 2, raw = TRUE) + drat + disp, data = mtcars)
)

# Results in polynomial names of the form '^1', '^2', etc.
modelsummary(dvnames(x), coef_rename = coef_rename)

# Results in polynomial names of the form 'Cyl^1', 'Cyl^2', etc.
modelsummary(dvnames(x), coef_rename = \(.x) coef_rename(.x, poly_name = FALSE))

Note that I didn't update NEWS or add any tests yet (I'm unfamiliar with tinytest, so I'm unsure how to go about it).

@vincentarelbundock
Copy link
Owner

Oh thanks, this is a really great idea! Awesome.

Two questions:

  1. Could we combine the two arguments? Maybe poly doesn't have to be TRUE/FALSE and can immediately say what "style" we want.
  2. Will the default style work in all output formats? For example, I believe Cyl^2 would break LaTeX compilation, and is not valid markdown either.

@mccarthy-m-g
Copy link
Contributor Author

Welcome! Regarding your questions:

Will the default style work in all output formats? For example, I believe Cyl^2 would break LaTeX compilation, and is not valid markdown either.

This approach just lets us clean up poly() coefficients to get the same result we would get if we cleaned up polynomials that were constructed "by hand" with I():

x <- list(
    lm(mpg ~ I(cyl^1) + I(cyl^2) + drat + disp, data = mtcars),
    lm(mpg ~ poly(cyl, 2, raw = TRUE) + drat + disp, data = mtcars)
)
modelsummary(x, coef_rename = \(.x) coef_rename(.x, poly_name = FALSE))

So the intention isn't to print the exponents in superscript---just as is. I don't believe this should break any output formats unless you attempt to apply some post-processing to the string that results in something breaking. But if that's the case you'd need special handling for I() statements too.

Could we combine the two arguments? Maybe poly doesn't have to be TRUE/FALSE and can immediately say what "style" we want.

I think it makes sense to stick with a boolean argument for poly, since every other argument for coef_rename() is also a boolean.

What we could do though is reduce it to a single boolean argument for the Cyl^2 style. I only included the ^2 style based on the options you currently give for factors---but personally I don't think this style is particularly useful, so I'd just drop it unless you can imagine a use case.

@vincentarelbundock
Copy link
Owner

Got it.

Yes, let's simplify to a single bool with the style you prefer.

The ^ character should really be tested in a latex document though (or Quarto/Rmd PDF), because I think it's a special character.

If that's the case and it breaks compilation, we either need some more complicated logic to escape conditionally on output format (would be a pain to code), or we need to come up with a different default style.

@vincentarelbundock
Copy link
Owner

Oh I see your point about I(x^2).

I think that works because modelsummary escapes this character by default.

So we're fine on that front.

@mccarthy-m-g
Copy link
Contributor Author

Awesome. I simplified the code to do the Cyl^2 style when poly = TRUE, so this gives nice output by default now:

x <- list(
    lm(mpg ~ I(cyl^1) + I(cyl^2) + drat + disp, data = mtcars),
    lm(mpg ~ poly(cyl, 2, raw = TRUE) + drat + disp, data = mtcars)
)
modelsummary(x, coef_rename = coef_rename)

Since ^ is escaped by default, we don't need to worry about testing in a latex document now, correct?

@vincentarelbundock vincentarelbundock merged commit 32848c0 into vincentarelbundock:main Jun 16, 2024
4 checks passed
@vincentarelbundock
Copy link
Owner

Thanks a ton for this! Looks great.

I merged.

The latest release was only a few days ago, so it'll take a little bit before this gets to CRAN, but it should be available on Github and R-Universe in the next few minutes.

@mccarthy-m-g mccarthy-m-g deleted the coef_rename-poly branch June 16, 2024 23:11
@mccarthy-m-g
Copy link
Contributor Author

Happy to contribute! I've been using modelsummary frequently for the example articles of a data package I'm working on and it's been super useful.

Thanks for merging!

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 this pull request may close these issues.

2 participants