pred_with_ci: Make a prediction and put a confidence interval around it

View source: R/pred_with_ci.R

pred_with_ciR Documentation

Make a prediction and put a confidence interval around it

Description

Small utility function to append a data.frame with predictions from a GAM and include a confidence interval. Various things are customizable.

Usage

pred_with_ci(
  model,
  data,
  alpha = 0.05,
  label = NULL,
  label_label = NULL,
  pred_label = "pred",
  ci_label = "ci",
  ci_types = c("u", "l"),
  sep = "_"
)

Arguments

model

a fitted GAM

data

the data we wish to predict to

alpha

the level we want our confidence intervals at

label

label for these models (e.g., "model_1")

label_label

the column name for the model labels

pred_label

the column name for the predictions

ci_label

start of the column name for the confidence intervals

ci_types

a 2-vector with the suffixes for the confidence intervals (upper then lower)

sep

separator between ci_label and ci_types in the confidence intervals column name

Details

By the data argument (a data.frame) will be returned with 3 extra columns, one for the prediction (column name as in pred_label), then upper CI (named paste(ci_label, ci_types[1], sep="_")) and finally the lower CI (named paste(ci_label, ci_types[2], sep="_")). If label and label_label are non-empty, a column will be added (with name label_label) and contents as in label.

Author(s)

David L Miller

Examples

library(mgcvUtils)

# fit something to look at
set.seed(2)
dat <- gamSim(1,n=400,dist="normal",scale=2)
b <- gam(y~s(x0),data=dat)

# adding prediction/ci columns
dat2 <- pred_with_ci(b, dat)
head(dat2)

# add labels, so you can concatenate predictions later for
# plotting with ggplot2
dat3 <- pred_with_ci(b, dat, label="model1", label_label="model_id")
head(dat3)

dill/mgcvUtils documentation built on Oct. 14, 2024, 7:30 p.m.