binred_plot: Generate a Binned-Residual Plot from a Fitted Generalized...

View source: R/binred_plot.R

binred_plotR Documentation

Generate a Binned-Residual Plot from a Fitted Generalized Linear Model

Description

binred_plot() provides a diagnostic of the fit of the generalized linear model by "binning" the fitted and residual values from the model and showing where they may fall outside 95% error bounds.

Usage

binred_plot(model, nbins, plot = TRUE)

Arguments

model

a fitted GLM model, assuming link is "logit"

nbins

number of "bins" for the calculation. Defaults to the rounded square root of the number of observations in the model in the absence of a user-specified override here.

plot

logical, defaults to TRUE. If TRUE, the function plots the binned residuals. If FALSE, the function returns a data frame of the binned residuals.

Details

The number of bins the user wants is arbitrary. Gelman and Hill (2007) say that, for larger data sets (n >= 100), the number of bins should be the rounded-down square root of the number of observations from the model. For models with a number of observations between 10 and 100, the number of bins should be 10. For models with fewer than 10 observations, the number of bins should be the rounded-down number of observations (divided by 2). The default is the rounded square root of the number of observations in the model. Be smart about what you want here.

Value

bindred_plot() returns a plot as a ggplot2 object, as a default. The y-axis is the mean residuals of the particular bin. The x-axis is the mean fitted values from the bin. Error bounds are 95%. A LOESS smoother is overlaid as a solid blue line.

If plot = FALSE, the function returns a data frame of the binned residuals and a summary about whether the residuals are in the error bounds.

Author(s)

Steven V. Miller

Examples


M1 <- glm(vs ~ mpg + cyl + drat, data=mtcars, family=binomial(link="logit"))

binred_plot(M1)

stevemisc documentation built on April 12, 2022, 5:06 p.m.