Learn R Programming

ciTools (version 0.1.0)

add_quantile: Add Regression Quantiles to Data Frames

Description

This is a generic function to append regression quantiles to a data frame. A regression quantile q is a point such that \(Pr(Response | Covariates < q) = p\). These quantiles are generated for the fitted value of each observation in tb. Quantiles are then appended to tb and returned to the user as a tibble.

Usage

add_quantile(tb, fit, p, name = NULL, yhatName = "pred", ...)

Arguments

tb

A tibble or data frame of new data.

fit

An object of class lm, glm, or lmerMod. Predictions are made with this object.

p

A double. A probability that determines the quantile. Must be between 0 and 1.

name

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

yhatName

A string. Name of the vector of predictions.

...

Additional arguments

Value

A tibble, tb, with predicted values and level-p quantiles attached.

Details

For more specific information about the arguments that are applicable for each type of model, consult:

Note: the quantiles that add_quantile calculates are on the distribution of \(Y|x\), not \(E[Y|x]\). That is, they use the same distribution that determines a prediction interval, not the distribution that determines a confidence interval.

See Also

add_ci for confidence intervals, add_probs for response level probabilities, and add_pi for prediction intervals

Examples

Run this code
# NOT RUN {
# Fit a linear model
fit <- lm(dist ~ speed, data = cars)

# Find the 0.4 quantile (or 40th percentile) of new distances for
# each observations in cars, conditioned on the linear model.
add_quantile(cars, fit, p = 0.4)

# Fit a Poisson model
fit2 <- glm(dist ~ speed, family = "poisson", data = cars)
# Find the 0.4 quantile (or 40th percentile) of new distances for
# each observation in cars, conditioned on the Poisson model.
add_quantile(cars, fit2, p = 0.4)

# Fit a random intercept linear mixed model
fit3 <- lme4::lmer(Reaction ~ Days + (1|Subject), data = lme4::sleepstudy)
# Find the 0.4 quantile (or 40 percentile) of reaction times for
# each observation in the sleepstudy data. Condition on the model and random effects.
add_quantile(lme4::sleepstudy, fit3, p = 0.4, type = "parametric")


# }

Run the code above in your browser using DataLab