Reputation: 13
I ran a linear regression with a natural log-transformed outcome which I wanted to un-transform in tbl_regression() by setting exponentiate = T. Instead, an error was produced. The regression equation without exponentiating,
ln(y) = B0 + B1x,
is not easy to interpret for a 1 unit change in x. Exponentiating would result in a more interpretable form where a 1 unit change in x would result in a percent change in y:
exp(ln(y)) = exp(B0 + B1x) y = exp(B0)*exp(B1x)
Is there a way to force tbl_regression() to provide exponentiated betas to a linear regression? If not, is there an alternative?
lm_cmv <-
lm(
log(cmv) ~ edu + covariates,
data = analytic_sample
) %>%
tbl_regression(
exponentiate = T
)
Error:
Error in `tidy_and_attach()`:
! `exponentiate = TRUE` is not valid for this type of model.
Upvotes: 1
Views: 396
Reputation: 11754
The way you're approaching the problem would usually work. But the lm()
tidier specifically includes this error for the exponentiate argument.
You can still get the table you're after with a bit of work.
library(gtsummary)
library(dplyr, warn.conflicts = FALSE)
tbl <-
lm(log(mpg) ~ am, mtcars) |>
tbl_regression()
# run this function to see the underlying column names in `tbl$table_body`
# show_header_names(tbl)
tbl |>
# remove character version of 95% CI
modify_column_hide(ci) |>
# exponentiate the regression estimates
modify_table_body(
\(x) x |> mutate(across(c(estimate, conf.low, conf.high), exp))
) |>
# merge numeric LB and UB together to display in table
modify_column_merge(pattern = "{conf.low}, {conf.high}", rows = !is.na(estimate)) |>
modify_header(conf.low = "**95% CI**") |>
as_kable() # convert to kable to display on stackoverflow
Characteristic | Beta | 95% CI | p-value |
---|---|---|---|
am | 1.4 | 1.2, 1.7 | <0.001 |
Created on 2023-11-06 with reprex v2.0.2
Upvotes: 0