Learn R Programming

ciTools (version 0.1.0)

add_ci.glm: Confidence Intervals for Generalized Linear Model Predictions

Description

This function is one of the methods for add_ci, and is called automatically when add_ci is used on a fit of class glm. Confidence Intervals are determined by making an interval on the scale of the linear predictor, then applying the inverse link function from the model fit to transform the linear level confidence intervals to the response level.

Usage

# S3 method for glm
add_ci(tb, fit, alpha = 0.05, names = NULL,
  yhatName = "pred", response = TRUE, type = "parametric", ...)

Arguments

tb

A tibble or data frame of new data.

fit

An object of class glm.

alpha

A real number between 0 and 1. Controls the confidence level of the interval estimates.

names

NULL or character vector of length two. If NULL, confidence bounds automatically will be named by add_ci, otherwise, the lower confidence bound will be named names[1] and the upper confidence bound will be named names[2].

yhatName

A string. Name of the vector of predictions made for each observation in tb

response

A logical. The default is TRUE. If TRUE, the confidence intervals will be determined for the expected response; if FALSE, confidence intervals will be made on the scale of the linear predictor.

type

A string. Currently type = "parametric" is the only supported method.

...

Additional arguments.

Value

A tibble, tb, with predicted values, upper and lower confidence bounds attached.

See Also

add_pi.glm for prediction intervals for glm objects, add_probs.glm for conditional probabilities of glm objects, and add_quantile.glm for response quantiles of glm objects.

Examples

Run this code
# NOT RUN {
# Poisson regression
fit <- glm(dist ~ speed, data = cars, family = "poisson")
add_ci(cars, fit)
# Try a different confidence level
add_ci(cars, fit, alpha = 0.5)
# Add custom names to the confidence bounds (may be useful for plotting)
add_ci(cars, fit, alpha = 0.5, names = c("lwr", "upr"))

# Logistic regression
fit2 <- glm(I(dist > 30) ~ speed, data = cars, family = "binomial")
dat <- cbind(cars, I(cars$dist > 30))
add_ci(dat, fit)
add_ci(dat, fit, alpha = 0.5)
# Make confidence intervals on the scale of the linear predictor
add_ci(dat, fit, alpha = 0.5, response = FALSE)
# Add custom names to the confidence bounds
add_ci(dat, fit, alpha = 0.5, names = c("lwr", "upr"))


# }

Run the code above in your browser using DataLab