View source: R/binned_residuals.R
binned_residuals  R Documentation 
Check model quality of binomial logistic regression models.
binned_residuals(
model,
term = NULL,
n_bins = NULL,
show_dots = NULL,
ci = 0.95,
ci_type = c("exact", "gaussian", "boot"),
residuals = c("deviance", "pearson", "response"),
iterations = 1000,
verbose = TRUE,
...
)
model 
A 
term 
Name of independent variable from 
n_bins 
Numeric, the number of bins to divide the data. If

show_dots 
Logical, if 
ci 
Numeric, the confidence level for the error bounds. 
ci_type 
Character, the type of error bounds to calculate. Can be

residuals 
Character, the type of residuals to calculate. Can be

iterations 
Integer, the number of iterations to use for the
bootstrap method. Only used if 
verbose 
Toggle warnings and messages. 
... 
Currently not used. 
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 xaxis is a signal to consider taking the logarithm
of the predictor (cf. Gelman and Hill 2007, pp. 9798).
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 greyshaded area), blue points indicate residuals that are OK, while red points indicate model under or overfitting for the relevant range of estimated probabilities.
binned_residuals()
returns a data frame, however, the print()
method only returns a short summary of the result. The data frame itself
is used for plotting. The plot()
method, in turn, creates a ggplotobject.
Gelman, A., and Hill, J. (2007). Data analysis using regression and multilevel/hierarchical models. Cambridge; New York: Cambridge University Press.
model < glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
result < binned_residuals(model)
result
# look at the data frame
as.data.frame(result)
# plot
if (require("see")) {
plot(result, show_dots = TRUE)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.