# 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 intervalstidy_model <-tidy(model, conf.int =TRUE)# Display the resultstidy_model
# Assuming you have a model object called 'model'model <-lm(mpg ~ hp * cyl, data = mtcars)# Extracting estimates and confidence intervalstidy_model <-tidy(model, conf.int =TRUE)# Display the resultstidy_model
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 intervalstidy_model <-tidy(model, conf.int =TRUE)# Display the resultstidy_model
# Extracting estimates and confidence intervalstbl_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 coefficientstbl_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 pvalueinline_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.