add_quantile {ciTools}R Documentation

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 df. Quantiles are then appended to df and returned to the user as a data frame.

Usage

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

Arguments

df

A 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 data frame.

yhatName

A string. Name of the vector of predictions.

...

Additional arguments

Details

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

Note: Except in add_ci.survreg, 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.

Value

A dataframe, df, with predicted values and level-p quantiles attached.

See Also

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

Examples


# 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")



[Package ciTools version 0.6.1 Index]