Learn R Programming

ciTools (version 0.1.0)

add_probs.glm: Response Probabilities for Generalized Linear Models

Description

This is the method add_probs uses if the model fit is an object of class glm. Probabilities are determined through simulation, using the same method as add_pi.glm. Currently, only logistic and Poisson models are supported.

Usage

# S3 method for glm
add_probs(tb, fit, q, name = NULL, yhatName = "pred",
  comparison = "

Arguments

tb

A tibble or data frame of new data.

fit

An object of class glm. Predictions are made with this object.

q

A double. A quantile of the response distribution.

name

NULL or a string. If NULL, probabilities automatically will be named by add_probs(), otherwise, the probabilities will be named name in the returned tibble

yhatName

A string. Name of the vector of predictions.

comparison

A character vector of length one. If comparison = "<", then \(Pr(Y|X < q)\) is calculated. Any comparison is allowed in Poisson regression, but only certain comparisons may be made in Logistic regression. See the Details section.

nSims

A positive integer. Controls the number of simulated draws to make if the model is Poisson.

...

Additional arguments.

Value

A tibble, tb, with predicted values and probabilities attached.

Details

Any of the five comparisons may be made for a Poisson model: comparison = "<", ">", "=", "<=", or ">=". For logistic regression, the comparison statement must be equivalent to \(Pr(Y|x = 0)\) or \(Pr(Y|x = 1)\).

If add_probs is called on a Poisson model, a simulation is preformed using arm::sim.

If add_probs is called on a logistic model, the fitted probabilities are used directly (no simulation is required).

See Also

add_ci.glm for confidence intervals for glm objects, add_pi.glm for prediction intervals of glm objects, and add_quantile.glm for response quantiles of glm objects.

Examples

Run this code
# NOT RUN {
# Fit a Poisson model
fit <- glm(dist ~ speed, data = cars, family = "poisson")

# Determine the probability that a new dist is less than 20, given
# the Poisson model.
add_probs(cars, fit, q = 20)

# Determine the probability that a new dist is greater than 20,
# given the Poisson model.
add_probs(cars, fit, q = 30, comparison = ">")

# Determine the probability that a new dist is greater than or
# equal to 20, given the Poisson model.
add_probs(cars, fit, q = 30, comparison = ">=")

# Fit a logistic model
fit2 <- glm(I(dist > 30) ~ speed, data = cars, family = "binomial")
add_probs(cars, fit2, q = 0, comparison = "=")
add_probs(cars, fit2, q = 1, comparison = "=")

# }

Run the code above in your browser using DataLab