This guide covers fitted-model inputs, effect scales, model comparisons, and derived subgroup effects. For appearance and layout, see Customize Forest Plots and Tables.
Explicit conversion methods are available for the following classes. Their tidiers must supply estimates and confidence limits.
| Model input | Required extraction packages | Output and scope |
|---|---|---|
stats::lm() (lm) |
broom |
Additive coefficients (Beta). |
stats::glm() (glm) |
broom |
Scale follows family and link, as described below. |
survival::coxph()
(coxph) |
broom, survival |
Hazard ratios (HR). |
lme4 models (merMod) |
broom, broom.mixed,
lme4 |
Fixed effects; scale follows family and link. |
nlme::lme() (lme) |
broom, broom.mixed,
nlme |
Fixed-effect additive coefficients. |
glmmTMB::glmmTMB()
(glmmTMB) |
broom, broom.mixed,
glmmTMB |
Conditional-model fixed effects; excludes zero-inflation and dispersion coefficients. |
Coefficient extraction and derived subgroup effects have different
requirements. Subgroup inference additionally uses
marginaleffects and has the restrictions described below. A
broom tidier alone does not imply direct support for every
model class. Post-estimation results and regression tables from
marginaleffects, emmeans,
gtsummary, and parameters are covered below.
For other inputs, use a coefficient table as described in Preparing Forest Data.
fit <- lm(mpg ~ wt + hp + qsec, data = mtcars)
ggforestplot(fit, term_labels = c("wt" = "Weight")) +
add_forest_table()If broom is available, the model-specific
as_forest_data() method pulls coefficient estimates and
confidence limits from a fitted model. The method also assigns model
semantics such as Beta, OR, or
HR.
The returned object can be passed directly into
ggforestplot().
tidy_forest_model() remains available as a compatibility
wrapper and returns the same forest_data object.
With exponentiate = NULL, binomial and quasibinomial
logit models use odds ratios, Cox models use hazard ratios, and log-link
Poisson models use incidence rate ratios. Binomial log-link models use
risk ratios; identity-link binomial models use risk differences. Other
log-link families use a ratio label. Linear models retain additive
coefficients.
For fitted models, exponentiate = FALSE retains
unexponentiated coefficients; TRUE transforms coefficients
and bounds. For data frames, TRUE instead declares values
that are already ratios and does not transform them.
Ratio plots default to a log10 axis with reference value 1.
Use as_forest_data(fit, conf.level = 0.90) to request a
different confidence level. Confidence intervals are required.
Intercepts are omitted by default; use
as_forest_data(fit, intercept = TRUE) to retain them.
logit_fit <- glm(am ~ mpg + cyl, data = mtcars, family = binomial())
ggforestplot(logit_fit, striped_rows = TRUE) + add_forest_table()bind_forest_models() checks compatible effect scales,
axis transformations, reference values, and confidence levels. Labels
follow the supplied model order. Also check that outcomes, predictor
units, and populations make the comparison meaningful.
fit1 <- lm(mpg ~ cyl, data = mtcars)
fit2 <- lm(mpg ~ cyl + qsec, data = mtcars)
fit3 <- lm(mpg ~ cyl + qsec + wt, data = mtcars)
bound_models <- bind_forest_models(
list(fit1, fit2, fit3),
model_labels = c("Unadjusted", "Adjusted", "Fully Adjusted")
)
ggforestplot(bound_models, striped_rows = TRUE, p.value = "p.value") +
scale_x_continuous(limits = c(-6, 2)) +
theme(legend.position = "top") +
scale_color_manual(values = c("#1F968BFF", "#453781FF", "#10A53dFF")) +
add_forest_table(
columns = c("term", "model", "estimate", "p.value"),
p_digits = 4
)For a fitted model with one continuous-by-factor interaction,
subgroup = "auto" identifies the factor from model metadata
and estimates the focal variable’s average slope within each observed
factor level.
interaction_fit <- lm(wt ~ hp + mpg * factor(cyl) + qsec, data = mtcars)
subgroup_ready <- tidy_forest_model(
interaction_fit,
subgroup = "auto",
focal = "mpg"
)
as.data.frame(subgroup_ready)[, c(
"term", "subgroup", "estimate", "conf.low", "conf.high", "p.value"
)]
#> term subgroup estimate conf.low conf.high p.value
#> 1 hp <NA> 0.002211858 -0.003019553 0.007443269 0.391515516
#> 2 4 cyl -0.063790596 -0.121760403 -0.005820789 0.133731900
#> 3 6 cyl -0.045622115 -0.273096846 0.181852616 0.133731900
#> 4 8 cyl -0.168851554 -0.258484920 -0.079218188 0.133731900
#> 5 qsec <NA> 0.237491332 0.105815640 0.369167024 0.001059097
ggforestplot(subgroup_ready, striped_rows = TRUE) +
add_forest_table(columns = c("term", "estimate", "p"))These rows are average mpg slopes within the
cyl levels. They come from
marginaleffects::avg_slopes() using the original model and
its covariance matrix. No subgroup-specific models are refitted. The
focal main effect, factor main-effect coefficients, and raw interaction
coefficients are replaced by the derived block; unrelated coefficients
remain standalone in formula order.
The same interaction can be selected explicitly:
tidy_forest_model(
interaction_fit,
subgroup = "cyl",
focal = "mpg"
)
#> <forest_data> Beta; scale: identity; reference: 0
#> term estimate conf.low conf.high label row_type group subgroup
#> 1 hp 0.002211858 -0.003019553 0.007443269 hp estimate <NA> <NA>
#> 2 4 -0.063790596 -0.121760403 -0.005820789 4 estimate <NA> cyl
#> 3 6 -0.045622115 -0.273096846 0.181852616 6 estimate <NA> cyl
#> 4 8 -0.168851554 -0.258484920 -0.079218188 8 estimate <NA> cyl
#> 5 qsec 0.237491332 0.105815640 0.369167024 qsec estimate <NA> <NA>
#> grouping separate_groups n events p.value std.error statistic df
#> 1 <NA> <NA> <NA> <NA> 0.391515516 0.002534723 0.8726232 NA
#> 2 <NA> <NA> <NA> <NA> 0.133731900 0.029576976 -2.1567653 NA
#> 3 <NA> <NA> <NA> <NA> 0.133731900 0.116060669 -0.3930885 NA
#> 4 <NA> <NA> <NA> <NA> 0.133731900 0.045732150 -3.6921849 NA
#> 5 <NA> <NA> <NA> <NA> 0.001059097 0.063799498 3.7224640 NA
#> subgroup_level focal model_term contrast estimand effect_scale
#> 1 <NA> <NA> <NA> <NA> <NA> <NA>
#> 2 4 mpg mpg:factor(cyl) dY/dX average_slope identity
#> 3 6 mpg mpg:factor(cyl) dY/dX average_slope identity
#> 4 8 mpg mpg:factor(cyl) dY/dX average_slope identity
#> 5 <NA> <NA> <NA> <NA> <NA> <NA>Automatic selection currently requires exactly one unambiguous
continuous-by-factor interaction. Factor-by-factor effects require
explicit predictor names and use
marginaleffects::avg_comparisons(). Continuous subgroups,
transformed focal terms, multiple selected interactions, and three-way
interactions are rejected. Logistic and Cox effects are calculated on
their model scale and shown as odds ratios and hazard ratios by default,
not as marginal probabilities or survival probabilities.
The canonical p.value combines two compatible display
roles: ordinary covariates retain their coefficient p-values, while
every derived subgroup block receives an omnibus Wald test of the
selected interaction. The display layer moves that interaction p-value
to the subgroup header and leaves the individual levels blank, so one
table column can present both kinds of tests.
Use p_method = "level" when the subgroup-specific slope
or comparison tests are the quantities that should appear beside the
individual estimates:
level_p_ready <- tidy_forest_model(
interaction_fit,
subgroup = "auto",
focal = "mpg",
p_method = "level"
)
ggforestplot(level_p_ready, striped_rows = TRUE) +
add_forest_table(columns = c("term", "estimate", "p"))Both methods use the same canonical p.value column.
"overall" changes the test and places it on the subgroup
header; "level" retains the tests returned for the derived
effects and places them on the subgroup rows.
fit2 <- lm(wt ~ as.factor(gear)*as.factor(cyl) + hp + am, data = mtcars)
fit2 |>
tidy_forest_model(subgroup = "gear", focal = "cyl", p_method = "level") |>
ggforestplot(striped_rows = TRUE) +
theme(legend.position = "top") +
add_forest_table(columns = c("term", "estimate", "p.value"))For mixed models, derived effects are population-level (random effects set to zero). Supported subgroup links are identity, logit, and log; Cox effects use the linear-predictor scale.
Results that already contain estimates and confidence intervals can
be passed to as_forest_data(), ggforestplot(),
and bind_forest_models() without reshaping. Their
estimates, intervals, p-values, and confidence levels are used exactly
as computed by the source package.
| Input | Source functions | Output |
|---|---|---|
marginaleffects |
comparisons(),
avg_comparisons(), predictions(),
avg_predictions(), slopes(),
avg_slopes(), hypotheses() |
Scale from the comparison, type, and transformation;
group, facet, and subgroup map
term, contrast, or by
columns. |
emmeans |
emmeans(), pairs(),
contrast(), summary(),
confint() |
Means without a reference line, or contrasts with a reference at 0 or 1. |
gtsummary |
tbl_regression(),
tbl_uvregression(), tbl_merge(),
tbl_stack() |
The table’s rows, labels, reference levels, counts, and p-values. |
parameters |
model_parameters() |
Fixed-effect coefficients with formatted names. |
logit_fit <- glm(am ~ hp + wt + factor(cyl), data = mtcars, family = binomial())
ratios <- marginaleffects::avg_comparisons(
logit_fit,
comparison = "lnratioavg",
transform = exp
)
ggforestplot(ratios,
subgroup = "term",
term_labels = c(hp = "Horsepower",
wt = "Weight", cyl = "Cylinders")) +
add_forest_table(columns = c("term", "estimate", "ci", "p"))Row labels combine the identifier columns that vary, such as
term and contrast. Map a by
variable to group or facet to compare effects
across subgroups.
interaction_lm <- lm(mpg ~ wt * factor(am) + hp, data = mtcars)
marginaleffects::avg_comparisons(interaction_lm, variables = c("wt", "hp"),
by = "am") |>
ggforestplot(group = "am") +
theme(legend.position = "top")warp_fit <- lm(breaks ~ wool * tension, data = warpbreaks)
tension_means <- emmeans::emmeans(warp_fit, ~ tension | wool)
ggforestplot(pairs(tension_means), facet = "wool") +
add_forest_table(columns = c("term", "estimate", "ci", "p"))Contrasts from logistic models are log odds ratios on the link scale;
use exponentiate = TRUE or type = "response"
to plot odds ratios.
Categorical variables keep their gtsummary structure:
the variable label is a subgroup header and reference levels are
reference rows. Merged tables become grouped estimates labelled by their
spanning headers.
trial <- gtsummary::trial
trial_vars <- trial[c("response", "age", "grade", "trt")]
crude <- gtsummary::tbl_uvregression(
trial_vars,
method = glm,
y = response,
method.args = list(family = binomial),
exponentiate = TRUE
)
adjusted <- glm(response ~ age + grade + trt,
data = trial, family = binomial()) |>
gtsummary::tbl_regression(exponentiate = TRUE)
gtsummary::tbl_merge(list(crude, adjusted),
tab_spanner = c("**Crude**", "**Adjusted**")) |>
ggforestplot(striped_rows = TRUE) +
theme(legend.position = "top") +
add_forest_table(columns = c("term", "group", "estimate", "ci"))