Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

binned_residuals

Binned residuals for binomial logistic regression


Description

Check model quality of binomial logistic regression models.

Usage

binned_residuals(model, term = NULL, n_bins = NULL, ...)

Arguments

model

A glm-object with binomial-family.

term

Name of independent variable from x. If not NULL, average residuals for the categories of term are plotted; else, average residuals for the estimated probabilities of the response are plotted.

n_bins

Numeric, the number of bins to divide the data. If n_bins = NULL, the square root of the number of observations is taken.

...

Further argument like size (for point-size) or color (for point-colors).

Details

Binned residual plots are achieved by “dividing the data into categories (bins) based on their fitted values, and then plotting the average residual versus the average fitted value for each bin.” (Gelman, Hill 2007: 97). If the model were true, one would expect about 95% of the residuals to fall inside the error bounds.

If term is not NULL, one can compare the residuals in relation to a specific model predictor. This may be helpful to check if a term would fit better when transformed, e.g. a rising and falling pattern of residuals along the x-axis is a signal to consider taking the logarithm of the predictor (cf. Gelman and Hill 2007, pp. 97-98).

Value

A data frame representing the data that is mapped in the accompanying plot. In case all residuals are inside the error bounds, points are black. If some of the residuals are outside the error bounds (indicated by the grey-shaded area), blue points indicate residuals that are OK, while red points indicate model under- or over-fitting for the relevant range of estimated probabilities.

Note

Since binned_residuals() returns a data frame, the default action for the result is printing. However, the 'print()'-method for binned_residuals() actually creates a plot. For further modifications of the plot, use 'print()' and add ggplot-layers to the return values, e.g. plot(binned_residuals(model)) + see::scale_color_pizza().

References

Gelman, A., & Hill, J. (2007). Data analysis using regression and multilevel/hierarchical models. Cambridge; New York: Cambridge University Press.

Examples

if (require("see")) {
  # creating a model
  model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")

  # this will automatically plot the results
  (result <- binned_residuals(model))

  # if you assign results to an object, you can also look at the dataframe
  as.data.frame(result)
}

performance

Assessment of Regression Models Performance

v0.7.1
GPL-3
Authors
Daniel Lüdecke [aut, cre] (<https://orcid.org/0000-0002-8895-3206>), Dominique Makowski [aut, ctb] (<https://orcid.org/0000-0001-5375-9967>), Mattan S. Ben-Shachar [aut, ctb] (<https://orcid.org/0000-0002-4287-4801>), Indrajeet Patil [aut, ctb] (<https://orcid.org/0000-0003-1995-6531>), Philip Waggoner [aut, ctb] (<https://orcid.org/0000-0002-7825-7573>), Vincent Arel-Bundock [ctb] (<https://orcid.org/0000-0003-2042-7063>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.