| autoforest | R Documentation |
A convenience wrapper function that automatically detects the input type and routes to the appropriate specialized forest plot function. This eliminates the need to remember which forest function to call for different model types or analysis objects, making it ideal for exploratory analysis and rapid prototyping.
autoforest(x, data = NULL, title = NULL, ...)
x |
One of the following:
|
data |
Data frame or data.table containing the original data. Required
when |
title |
Character string for plot title. If
|
... |
Additional arguments passed to the specific forest plot function. Common arguments include:
See the documentation for the specific forest function for all available options. |
This function provides a convenient wrapper around the specialized forest plot functions, automatically routing to the appropriate function based on the model class or result type. All parameters are passed through to the underlying function, so the full range of options remains available.
For model-specific advanced features, individual forest functions may be called directly.
Automatic Detection Logic:
The function uses the following priority order for detection:
uniscreen results: Detected by class "uniscreen_result" or
presence of attributes outcome, predictors, model_type,
and model_scope = "Univariable". Routes to uniforest().
multifit results: Detected by presence of attributes
predictor, outcomes, model_type, and raw_data.
Routes to multiforest().
Cox models: Classes coxph or clogit. Routes to
coxforest().
GLM models: Class glm. Routes to glmforest().
Linear models: Class lm (but not glm). Routes to
lmforest().
A ggplot object containing the complete forest plot. The plot
can be:
Displayed directly: print(plot)
Saved to file: ggsave("forest.pdf", plot, width = 12, height = 8)
Further customized with ggplot2 functions
The returned object includes an attribute "rec_dims"
accessible via attr(plot, "rec_dims"), which is a list
containing:
Numeric. Recommended plot width in specified units
Numeric. Recommended plot height in specified units
These recommendations are automatically calculated based on the number of
variables, text sizes, and layout parameters, and are printed to console
if plot_width or plot_height are not specified.
glmforest for GLM forest plots,
coxforest for Cox model forest plots,
lmforest for linear model forest plots,
uniforest for univariable screening forest plots,
multiforest for multi-outcome forest plots,
fit for single-model regression,
fullfit for combined univariable/multivariable regression,
uniscreen for univariable screening,
multifit for multi-outcome analysis
Other visualization functions:
coxforest(),
glmforest(),
lmforest(),
multiforest(),
uniforest()
data(clintrial)
data(clintrial_labels)
library(survival)
# Create example model
glm_model <- glm(surgery ~ age + sex + bmi + smoking,
family = binomial, data = clintrial)
# Example 1: Logistic regression model
p <- autoforest(glm_model, data = clintrial)
# Automatically detects GLM and routes to glmforest()
# Example 2: Cox proportional hazards model
cox_model <- coxph(Surv(os_months, os_status) ~ age + sex + treatment + stage,
data = clintrial)
plot2 <- autoforest(cox_model, data = clintrial)
# Automatically detects coxph and routes to coxforest()
# Example 3: Linear regression model
lm_model <- lm(biomarker_x ~ age + sex + bmi + treatment, data = clintrial)
plot3 <- autoforest(lm_model, data = clintrial)
# Automatically detects lm and routes to lmforest()
# Example 4: With custom labels and formatting options
plot4 <- autoforest(
cox_model,
data = clintrial,
labels = clintrial_labels,
title = "Prognostic Factors for Overall Survival",
zebra_stripes = TRUE,
indent_groups = TRUE
)
# Example 5: From fit() result - data and labels extracted automatically
fit_result <- fit(
data = clintrial,
outcome = "surgery",
predictors = c("age", "sex", "bmi", "treatment"),
labels = clintrial_labels
)
plot5 <- autoforest(fit_result)
# No need to pass data or labels - extracted from fit_result
# Save with recommended dimensions
dims <- attr(plot5, "rec_dims")
ggplot2::ggsave(file.path(tempdir(), "forest.pdf"),
plot5, width = dims$width, height = dims$height)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.