Interaction Tips Summary

Author

Andrei Pyko

Published

March 19, 2025

How to extract est and CIs from r object

# Example R code to extract estimates and confidence intervals from a model object
# install.packages("broom")
library(broom)

# Assuming you have a model object called 'model'
model <- lm(mpg ~ hp * cyl, data = mtcars)

# Extracting estimates and confidence intervals
tidy_model <- tidy(model, conf.int = TRUE)

# Display the results
tidy_model
# A tibble: 4 × 7
  term        estimate std.error statistic      p.value conf.low conf.high
  <chr>          <dbl>     <dbl>     <dbl>        <dbl>    <dbl>     <dbl>
1 (Intercept)  50.8      6.51         7.79 0.0000000172 37.4       64.1   
2 hp           -0.171    0.0691      -2.47 0.0199       -0.312     -0.0291
3 cyl          -4.12     0.988       -4.17 0.000267     -6.14      -2.09  
4 hp:cyl        0.0197   0.00881      2.24 0.0332        0.00169    0.0378
# Assuming you have a model object called 'model'
model <- lm(mpg ~ hp * cyl, data = mtcars)

# Extracting estimates and confidence intervals
tidy_model <- tidy(model, conf.int = TRUE)

# Display the results
tidy_model
# A tibble: 4 × 7
  term        estimate std.error statistic      p.value conf.low conf.high
  <chr>          <dbl>     <dbl>     <dbl>        <dbl>    <dbl>     <dbl>
1 (Intercept)  50.8      6.51         7.79 0.0000000172 37.4       64.1   
2 hp           -0.171    0.0691      -2.47 0.0199       -0.312     -0.0291
3 cyl          -4.12     0.988       -4.17 0.000267     -6.14      -2.09  
4 hp:cyl        0.0197   0.00881      2.24 0.0332        0.00169    0.0378

Extraction of data with gtsummary

Now we will use the gtsummary package to extract the estimates and confidence intervals from a model object.

# Example R code to extract estimates and confidence intervals from a model object using gtsummary
# install.packages("gtsummary", dependencies = TRUE)
library(gtsummary)

# Assuming you have a model object called 'model'
model <- lm(mpg ~ hp * cyl, data = mtcars)

# Extracting estimates and confidence intervals
tidy_model <- tidy(model, conf.int = TRUE)

# Display the results
tidy_model
# A tibble: 4 × 7
  term        estimate std.error statistic      p.value conf.low conf.high
  <chr>          <dbl>     <dbl>     <dbl>        <dbl>    <dbl>     <dbl>
1 (Intercept)  50.8      6.51         7.79 0.0000000172 37.4       64.1   
2 hp           -0.171    0.0691      -2.47 0.0199       -0.312     -0.0291
3 cyl          -4.12     0.988       -4.17 0.000267     -6.14      -2.09  
4 hp:cyl        0.0197   0.00881      2.24 0.0332        0.00169    0.0378
# Extracting estimates and confidence intervals
tbl_regression(model)
Characteristic Beta 95% CI p-value
hp -0.17 -0.31, -0.03 0.020
cyl -4.1 -6.1, -2.1 <0.001
hp * cyl 0.02 0.00, 0.04 0.033
Abbreviation: CI = Confidence Interval

Options of gtsummary

The gtsummary package provides several options for customizing the output of tbl_regression. Here are a few examples:

Exponentiating coefficients (eform)

To exponentiate the coefficients (useful for logistic regression models), you can use the exponentiate = TRUE argument:

logistic_model <- glm(am ~ hp + cyl, data = mtcars, family = binomial)

# Exponentiating coefficients
tbl_regression(logistic_model, exponentiate = TRUE)
Characteristic OR 95% CI p-value
hp 1.03 1.00, 1.06 0.042
cyl 0.18 0.04, 0.50 0.005
Abbreviations: CI = Confidence Interval, OR = Odds Ratio

Inlind estimate of gtsummary

To extract inline estimates and 95% confidence intervals for the first term (hp) using gtsummary, you can use the inline_text function. Here is how it looks:

# Example R code to extract inline estimates and 95% confidence intervals using gtsummary
# install.packages("gtsummary", dependencies = TRUE)
library(gtsummary)

# Assuming you have a model object called 'model'
model <- lm(mpg ~ hp * cyl, data = mtcars)

# Extracting inline estimates and confidence intervals for the first term (hp)
inline_text(tbl_regression(model), variable = "hp")
[1] "-0.17 (95% CI -0.31, -0.03; p=0.020)"
# or have only est and CI in without pvalue
inline_text(tbl_regression(model), variable = "hp", pattern = "{estimate} [{conf.low}, {conf.high}]")
[1] "-0.17 [-0.31, -0.03]"

Now let’s see how that could be used in Quarto as an inline command within formatted text, so the estimates of -0.17 (95% CI -0.31, -0.03; p=0.020) can be displayed directly in the document. To do so we are writing text as follows:

Now let's see how that could be used in Quarto as an inline 
command within formatted text, so the estimates of 
-0.17 (95% CI -0.31, -0.03; p=0.020) 
can be displayed directly in the document.