list_CIs: Define a list containing fitted values and confidence...

View source: R/list_CIs.R

list_CIsR Documentation

Define a list containing fitted values and confidence intervals for the linear predictor or response from a model

Description

This function creates a list of fitted values and lower/upper confidence intervals (CIs) that can be easily plotted from a list of fitted values and standard errors (SEs). For predictions on the scale of the response, the user is advised the supply the function with predictions on the scale of the link function (e.g. from predict(..., type = "link")) and an inverse link function. If an inverse link is supplied, the function first computes lower/upper CIs and then applies the inverse link function (this order of operations is generally preferable to the computation of SEs on the scale of the response (e.g. from predict(..., type = "response")) and then using these to compute CIs). Predictions can be adjusted with user-defined functions and the function can also return plot suggestions (e.g. y limits of a plot) based on predictions.

Usage

list_CIs(
  pred,
  inv_link = I,
  fadj = I,
  centre = FALSE,
  plot_suggestions = TRUE,
  pretty_param = list(n = 5)
)

Arguments

pred

A list of fitted values and corresponding SEs (usually from predict).

inv_link

A function which defines the inverse link function. The default is I which leaves predictions unchanged. If supplied, the function applies inv_link after computing confidence intervals.

fadj

A function by which to modify predictions (after the inverse link function has been applied, if applicable). For example, in models of animal depth time series, models are often implemented by considering depth as a positive number, but plotted with depth as a negative number, for visualisation purposes.

centre

A logical input defining whether or not to centre predictions. If TRUE, the mean fitted value is minused from predictions.

plot_suggestions

A logical input defining whether or not to make plot suggestions (e.g. y limits) when can help make prettier plots.

pretty_param

A list of parameters, passed to pretty, if plot suggestions are requested. (pretty_axis is more flexible.)

Value

A list with (usually) three elements: (1) 'fit', fitted values; (2) 'lowerCI', lower CIs (0.025 percentile); and (3) 'upperCI', upper CIs (97.5 percentile). If plot_suggetions = TRUE, 'yat' is a vector of suggested positions for y axis positions based on model predictions and 'ylim' is a vector of suggested y axis limits.

Author(s)

Edward Lavender

See Also

pretty, pretty_axis, predict

Examples


#### Example (1): A simple linear model
# Define some data for a model
set.seed(1)
x <- runif(1000, 0, 100)
y <- rnorm(1000, 0.5*x - 50, 100)
# Define model
m1 <- lm(y ~ x)
# Define predictions
p <- predict(m1, se.fit = TRUE)
# list CIs
CIs <- list_CIs(pred = p)
str(CIs)



edwardlavender/prettyGraphics documentation built on Jan. 19, 2025, 2:47 p.m.