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.

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.

See also

Author

Edward Lavender

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)
#> List of 5 #> $ fit : 'AsIs' Named num [1:1000] -39.6680.... -34.1110.... -23.6484.... -6.16898.... -42.9948.... ... #> ..- attr(*, "names")= chr [1:1000] "1" "2" "3" "4" ... #> $ lowerCI: 'AsIs' Named num [1:1000] -47.9250.... -41.1190.... -30.2602.... -17.2848.... -52.2126.... ... #> ..- attr(*, "names")= chr [1:1000] "1" "2" "3" "4" ... #> $ upperCI: 'AsIs' Named num [1:1000] -31.4110.... -27.1029.... -17.0367.... 4.946843.... -33.7770.... ... #> ..- attr(*, "names")= chr [1:1000] "1" "2" "3" "4" ... #> $ yat : num [1:6] -80 -60 -40 -20 0 20 #> $ ylim : num [1:2] -80 20