hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
321 stars 28 forks source link

Can't properly exponentiate Cox Regression coefficients using huxreg to get Hazard Ratios #163

Closed mabilang closed 4 years ago

mabilang commented 4 years ago

Describe the bug When using huxreg to report Cox Regression model output, it is useful to exponentiate the model coefficients to report the HR and 95% CI. This is done using the error_format() argument. However, the "estimate" is not able to be parsed correctly and returns errors. First, it will return a "non-numeric argument to mathematical function" error when attempting to include {exp(estimate)} in the error_format() argument. When I attempt to coerce the"estimate" to numeric, it then returns NA introduced by coercion because it cannot parse the character string (I believe this is due to statistically significant estimates being reported with an asterisk at the end e.g. "0.019 *", where as non-significant estimates are able to be parsed due to the absence of the asterisk symbol e.g. "0.019".

Note that {exp(conf.low)} and {exp(conf.high)} both work correctly.

Minimal Reproducible Example

library(survival)
data("lung") #NCCTG Lung Cancer Data from survival package
str(lung)
cox.age <- coxph(Surv(time, status) ~ age, data=lung) 
summary(cox.age) # I want to report the exp(coef) of 1.019 and 95% CI of (1.001, 1.037) using huxreg

huxreg('Age model' = cox.age,
       ci_level = 0.95, bold_signif = 0.05, error_pos = 'same',
       error_format = c('p={p.value}, HR: {exp(estimate)}, CI: ({exp(conf.low)},{exp(conf.high)})')) -> test
test #error because estimate is read as non-numeric argument
tidy(cox.age) #confirm that estimate is numeric: <dbl>
huxreg('Age model' = cox.age,
       ci_level = 0.95, bold_signif = 0.05, error_pos = 'same',
       error_format = c('p={p.value}, HR: {exp(as.numeric(estimate))}, CI: ({exp(conf.low)},{exp(conf.high)})')) -> test
test #error because estimate cannot be coerced into numeric due to the * in the character string

huxreg('Age model' = cox.age,
       ci_level = 0.95, bold_signif = 0.05, error_pos = 'same',
       error_format = c('p={p.value}, HR: {glimpse(estimate)}, CI: ({exp(conf.low)},{exp(conf.high)})')) #you can see the estimate is being read as chr "0.0187201792045515 *"

Expected behavior I expected that the argument error_format = c('{exp(estimate)}, CI: ({exp(conf.low)},{exp(conf.high)})') would be able to produce the HR and 95% CI in the output.

System details Please report the output of packageVersion("huxtable"), as well as your R version and Rstudio version if applicable. huxtable: v5.0.0 R: v3.6.3 RStudio: v1.2.5019

NOTE I initially posted on stackoverflow for help thinking I was doing something wrong, but I've posted here as well thinking it may be a bug. Thank you for your time.

hughjonesd commented 4 years ago

Use tidy_override or tidy_replace to pre-exponentiate your data; or just do

huxreg('Age model' = cox.age, ci_level = 0.95, bold_signif = 0.05, error_pos = 'same', error_format = c('p={p.value}, HR: {exp(as.numeric(gsub(" .*", "", estimate)))}, CI: ({exp(conf.low)},{exp(conf.high)})'))

The former seems a better idea, since there's little reason to put the estimate in the error cell.

hughjonesd commented 4 years ago

That said, I think this is a legitimate bug since it violates expectations that you can use the raw estimate column from tidy(mod) in error_format. Please try the latest github master to see if it works for you.

mabilang commented 4 years ago

Hi David,

Thank you so much, the latest github master works. I use huxreg to output cox model tables into Word frequently, so being able to exponentiate estimates to see the HR is very helpful (and I couldn't figure out how to do it without using error_format()). One minor effect of the update I noticed is that variables in the huxtable but absent in the cox model now have a cell value of "NA". I think before the update it was blank or a dash (I could be wrong, but I'm fairly certain it wasn't NA). Here's example code and a screenshot:

data("lung") #NCCTG Lung Cancer Data from survival package

str(lung) cox.age <- coxph(Surv(time, status) ~ age, data=lung) cox.meal <- coxph(Surv(time, status) ~ meal.cal, data=lung) cox.agemealwt <- coxph(Surv(time, status) ~ age + meal.cal + wt.loss, data=lung) huxreg('Age ' = cox.age, 'Meal' = cox.meal, 'Multivariate' = cox.agemealwt, ci_level = 0.95, bold_signif = 0.05, error_pos = 'same', error_format = c('p={p.value}, HR: {exp(estimate)}, CI: ({exp(conf.low)},{exp(conf.high)})')) -> test test

[image: image.png]

It's not a big deal, but thought I should let you know. Thanks again for your help (and your great work on huxtable)

Best,

Curtis

On Tue, Jun 30, 2020 at 1:42 AM David Hugh-Jones notifications@github.com wrote:

That said, I think this is a legitimate bug since it violates expectations that you can use the raw estimate column from tidy(mod) in error_format. Please try the latest github master to see if it works for you.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hughjonesd/huxtable/issues/163#issuecomment-651611474, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJ2CCLIJU37IEN2BFFYCGLRZGJNVANCNFSM4OLVHNTQ .