forest_plot_comp: Produce a forest plot of model predictions

View source: R/forest_plot_examples.R

forest_plot_compR Documentation

Produce a forest plot of model predictions

Description

This function takes a named list of compositions, and plots model predictions for each composition. Please note that with the default terms = TRUE predictions are relative to at the (compositional) mean composition in the data used to create the model. Please note that if there is missing data in the original data (e.g. in covariates or outcomes) this may not be the same as the compositional mean in the original data.

Usage

forest_plot_comp(
  composition_list,
  model = NULL,
  models_list = NULL,
  comp_labels,
  x_label = NULL,
  xllimit = NULL,
  xulimit = NULL,
  plot_log = FALSE,
  text_settings = NULL,
  pred_name = NULL,
  boxsize = 0.05,
  terms = TRUE,
  units = "unitless",
  specified_units = NULL,
  part_1 = NULL,
  fixed_values = NULL,
  ...
)

Arguments

composition_list

Named list of compositions. Note each composition should be stored as a data frame. For example, use the output of change_composition.

model

Model to use in estimates/predictions.

models_list

If model is NULL (or not set), a named list of models for which to plot model predictions in the forest plot. All models should have the same type.

comp_labels

The labels of the compositional columns.

x_label

x-axis label.

xllimit

Minimum value for x-axis.

xulimit

Maximum value for x-axis.

plot_log

If this is TRUE, the x-axis will be log-transformed.

text_settings

An optional argument to specify text formatting. It should be an fpTxtGp object (forestplot package).

pred_name

Name for column of predictions in plot.

boxsize

Sets the size of boxes for estimates on the forest plot.

terms

Are estimates for differences in outcome associated with differences in compositional variables? If terms = TRUE all estimates and plots will be for difference in outcome associated with differences in the compositional variables. If terms = FALSE, fixed_values is used to set the values of the non-compositional covariates, and outputs are predictions for the outcome based on these values of the non-compositional covariates and the given value of the compositional variables (and confidence intervals include uncertainty due to all variables in the model, not just the compositional variables). Note that for logistic regression models with terms = TRUE estimates are odds ratios; for logistic regression models with terms = FALSE estimates are probabilities (i.e. predictions on the response scale).

units

What should the units of the compositional variables be in any output? Currently available are "unitless" (where working in terms of proportions), "hr/day", "hr/wk", "min/day", "min/wk" and "specified", in which case the specified_units argument should be set. Note that this doesn't specify the input units, as this is not relevant for any function.

specified_units

If units are being specified via the composition sum, this is where it is done. It should be a vector where the first argument is a string describing the units, and the second argument is the expected sum of a composition e.g. c("hr/day", 24)

part_1

Used with ilr-pivot coordinate transformation to specify which should be first part. Passed to alter_order_comp_labels unless NULL.

fixed_values

If terms = FALSE, this gives the fixed values of the non-compositional covariates at which to calculate the prediction. It is generated automatically if not set. It does not usually need setting, and makes no difference to the output if 'terms = TRUE'.

...

Arguments passed on to forestplot::forestplot

Value

Forest plot showing model predictions.

Examples

# Example using a list of models
  # First we set up composition list
df <- as.data.frame(comp_mean(data = simdata,
                             comp_labels = c("vigorous", "moderate",
                             "light", "sedentary", "sleep"),
                             units = "hr/day", rounded_zeroes = FALSE))
new_comp <- change_composition(composition = df, main_part = "moderate",
                             main_change = +0.5,
                             comp_labels = c("vigorous", "moderate",
                             "light", "sedentary", "sleep"))
new_comp2 <- change_composition(composition = df, main_part = "sedentary",
                             main_change = -3.5,
                             comp_labels = c("vigorous", "moderate",
                             "light", "sedentary", "sleep"))
list_for_plot <- list("Extra 0.5 hr/day moderate" = new_comp,
                      "3.5 hr/day less sedentary" = new_comp2)

 # Then calculate models
lm_BMI_unadjusted <- comp_model(type = "linear", outcome = "BMI",
                                data = simdata,
                                comp_labels = c("vigorous", "moderate",
                                 "light", "sedentary", "sleep"), rounded_zeroes = FALSE)
lm_BMI_age_group_only <- comp_model(type = "linear", outcome = "BMI",
                                    covariates = c("agegroup"), data = simdata,
                                    comp_labels = c("vigorous", "moderate",
                                     "light", "sedentary", "sleep"), rounded_zeroes = FALSE)

  # Finally, plot
forest_plot_comp(composition_list = list_for_plot,
                 models_list = list("Unadjusted" = lm_BMI_unadjusted,
                 "Age-adjusted" = lm_BMI_age_group_only),
                 comp_labels = c("vigorous", "moderate",
                 "light", "sedentary", "sleep"))

OxWearables/epicoda documentation built on Dec. 7, 2022, 9:07 p.m.