| Title: | Surrogate Residuals for Ordinal and General Regression Models |
|---|---|
| Description: | An implementation of the surrogate approach to residuals and diagnostics for ordinal and general regression models; for details, see Liu and Zhang (2017, <doi:https://doi.org/10.1080/01621459.2017.1292915>) and Greenwell et al. (2017, <https://journal.r-project.org/archive/2018/RJ-2018-004/index.html>). These residuals can be used to construct standard residual plots for model diagnostics (e.g., residual-vs-fitted value plots, residual-vs-covariate plots, Q-Q plots, etc.). The package also provides 'plot' methods for producing standard diagnostic plots using lightweight base R graphics (via 'tinyplot'). The package currently supports cumulative link models from packages 'MASS', 'ordinal', 'rms', and 'VGAM'. Support for binary regression models using the standard 'glm' function is also available. |
| Authors: | Brandon Greenwell [aut, cre], Andrew McCarthy [aut], Boehmke Bradley [aut], Liz Glaser [ctb] |
| Maintainer: | Brandon Greenwell <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 0.3.0 |
| Built: | 2026-07-07 04:53:42 UTC |
| Source: | https://github.com/bgreenwell/sure |
Calculate surrogate residuals for multinomial logit and general discrete choice models using a fast, exact conditional Gumbel simulation method.
calc_resid(y, proba)calc_resid(y, proba)
y |
Vector of observed classes (either a factor or a 1-based integer vector). |
proba |
Matrix of predicted probabilities, where rows correspond to observations and columns correspond to classes. |
A matrix of surrogate residuals of the same dimensions as proba.
Simulate p-values from a goodness-of-fit test.
gof(object, nsim = 10, test = c("ks", "ad", "cvm"), ...) ## Default S3 method: gof(object, nsim = 10, test = c("ks", "ad", "cvm"), ...) ## S3 method for class 'gof' plot(x, ...)gof(object, nsim = 10, test = c("ks", "ad", "cvm"), ...) ## Default S3 method: gof(object, nsim = 10, test = c("ks", "ad", "cvm"), ...) ## S3 method for class 'gof' plot(x, ...)
object |
An object of class |
nsim |
Integer specifying the number of bootstrap replicates to use. |
test |
Character string specifying which goodness-of-fit test to use.
Current options include: |
... |
Additional optional arguments. (Currently ignored.) |
x |
An object of class |
Under the null hypothesis, the distribution of the p-values should appear
uniformly distributed on the interval [0, 1]. This can be visually
investigated using the plot method. A 45 degree line is indicative of
a "good" fit.
A numeric vector of class "gof", "numeric" containing the
simulated p-values.
# See ?resids for an example ?resids# See ?resids for an example ?resids
Residual-based diagnostic plots for cumulative link and general regression
models using tinyplot::tinyplot() graphics.
## S3 method for class 'resid' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'clm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'glm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'lrm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'orm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'polr' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'vglm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... )## S3 method for class 'resid' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'clm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'glm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'lrm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'orm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'polr' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... ) ## S3 method for class 'vglm' plot( x, y = NULL, what = c("qq", "fitted", "covariate"), covariate = NULL, fit = NULL, distribution = qnorm, ncol = NULL, alpha = 1, xlab = NULL, ylab = NULL, main = NULL, smooth = TRUE, ... )
x |
An object of class |
y |
Ignored. |
what |
Character string specifying what to plot. Default is |
covariate |
A vector giving the covariate values to use for residual-by-
covariate plots (i.e., when |
fit |
The fitted model from which the residuals were extracted. (Only
required if |
distribution |
Function that computes the quantiles for the reference
distribution to use in the quantile-quantile plot. Default is |
ncol |
Integer specifying the number of columns to use for the plot
layout (if requesting multiple plots). Default is |
alpha |
A single value in the interval |
xlab |
Character string giving the text to use for the x-axis label.
Default is |
ylab |
Character string giving the text to use for the y-axis label.
Default is |
main |
Character string giving the plot title. Default is |
smooth |
Logical indicating whether or not to add a nonparametric
smooth to certain plots. Default is |
... |
Additional optional arguments to be passed onto
|
No return value, called for plotting.
For vglm (and vgam) objects, the VGAM package defines its own S4 plot
method which will override this S3 method if VGAM is loaded. In such cases,
we recommend extracting the residuals first using resids() and then plotting the
resulting resid object (e.g. plot(resids(fit))), which will correctly dispatch
to plot.resid().
Simulate surrogate response values for cumulative link regression models using the latent method described in Liu and Zhang (2017).
resids(object, ...) ## Default S3 method: resids( object, nsim = 1L, method = c("latent", "jitter"), jitter_scale = c("response", "probability"), ... ) ## S3 method for class 'multinom' resids(object, ...) ## S3 method for class 'matrix' resids(object, y, ...)resids(object, ...) ## Default S3 method: resids( object, nsim = 1L, method = c("latent", "jitter"), jitter_scale = c("response", "probability"), ... ) ## S3 method for class 'multinom' resids(object, ...) ## S3 method for class 'matrix' resids(object, y, ...)
object |
An object of class |
... |
Additional optional arguments. (Currently ignored.) |
nsim |
Integer specifying the number of bootstrap replicates to use.
Default is |
method |
Character string specifying which method to use to generate the
surrogate response values. Current options are |
jitter_scale |
Character string specifyint the scale on which to perform
the jittering whenever |
y |
A vector of the response variable (only used when |
A numeric vector of class c("numeric", "surrogate") containing
the simulated surrogate response values. Additionally, if nsim > 1,
then the result will contain the attributes:
boot_reps: A matrix with nsim columns, one for each bootstrap
replicate of the surrogate values. Note, these are random and do not
correspond to the original ordering of the data.
boot_id: A matrix with nsim columns. Each column contains the
observation number each surrogate value corresponds to in boot_reps.
(This is used for plotting purposes.)
Surrogate response values require sampling from a continuous distribution;
consequently, the result will be different with every call to
surrogate. The internal functions used for sampling from truncated
distributions are based on modified versions of
truncdist::rtrunc() and truncdist::qtrunc().
For "glm" objects, only the binomial() family is supported.
Liu, Dungang and Zhang, Heping. Residuals and Diagnostics for Ordinal Regression Models: A Surrogate Approach. Journal of the American Statistical Association (accepted). URL http://www.tandfonline.com/doi/abs/10.1080/01621459.2017.1292915?journalCode=uasa20
Nadarajah, Saralees and Kotz, Samuel. R Programs for Truncated Distributions. Journal of Statistical Software, Code Snippet, 16(2), 1-8, 2006. URL https://www.jstatsoft.org/v016/c02.
# Generate data from a quadratic probit model set.seed(101) n <- 2000 x <- runif(n, min = -3, max = 6) z <- 10 + 3 * x - 1 * x^2 + rnorm(n) y <- ifelse(z <= 0, yes = 0, no = 1) # Scatterplot matrix pairs(~ x + y + z) # Setup for side-by-side plots par(mfrow = c(1, 2)) # Misspecified mean structure fm1 <- glm(y ~ x, family = binomial(link = "probit")) scatter.smooth(x, y = resids(fm1), main = "Misspecified model", ylab = "Surrogate residual", lpars = list(lwd = 3, col = "red2")) abline(h = 0, lty = 2, col = "blue2") # Correctly specified mean structure fm2 <- glm(y ~ x + I(x ^ 2), family = binomial(link = "probit")) scatter.smooth(x, y = resids(fm2), main = "Correctly specified model", ylab = "Surrogate residual", lpars = list(lwd = 3, col = "red2")) abline(h = 0, lty = 2, col = "blue2")# Generate data from a quadratic probit model set.seed(101) n <- 2000 x <- runif(n, min = -3, max = 6) z <- 10 + 3 * x - 1 * x^2 + rnorm(n) y <- ifelse(z <= 0, yes = 0, no = 1) # Scatterplot matrix pairs(~ x + y + z) # Setup for side-by-side plots par(mfrow = c(1, 2)) # Misspecified mean structure fm1 <- glm(y ~ x, family = binomial(link = "probit")) scatter.smooth(x, y = resids(fm1), main = "Misspecified model", ylab = "Surrogate residual", lpars = list(lwd = 3, col = "red2")) abline(h = 0, lty = 2, col = "blue2") # Correctly specified mean structure fm2 <- glm(y ~ x + I(x ^ 2), family = binomial(link = "probit")) scatter.smooth(x, y = resids(fm2), main = "Correctly specified model", ylab = "Surrogate residual", lpars = list(lwd = 3, col = "red2")) abline(h = 0, lty = 2, col = "blue2")
Generate simulated data sets designed to illustrate various model misspecifications and diagnostics for ordinal regression models, as described in Liu and Zhang (2017).
sim_data( n = 2000, type = c("quadratic", "heteroscedastic", "gumbel", "proportionality", "interaction"), ... )sim_data( n = 2000, type = c("quadratic", "heteroscedastic", "gumbel", "proportionality", "interaction"), ... )
n |
Integer specifying the number of observations to simulate. Default is
|
type |
Character string specifying the type of data/model to simulate.
Default is |
... |
Additional optional arguments (currently ignored). |
A data frame containing the simulated predictor(s) and the ordered
factor response variable y.
Liu, Dungang and Zhang, Heping. Residuals and Diagnostics for Ordinal Regression Models: A Surrogate Approach. Journal of the American Statistical Association (accepted).
# Simulate quadratic data set.seed(101) df <- sim_data(n = 500, type = "quadratic") head(df)# Simulate quadratic data set.seed(101) df <- sim_data(n = 500, type = "quadratic") head(df)
Simulate surrogate response values for cumulative link regression models using the latent method described in Liu and Zhang (2017).
surrogate( object, nsim = 1L, method = c("latent", "jitter"), jitter_scale = c("response", "probability"), ... )surrogate( object, nsim = 1L, method = c("latent", "jitter"), jitter_scale = c("response", "probability"), ... )
object |
An object of class |
nsim |
Integer specifying the number of bootstrap replicates to use.
Default is |
method |
Character string specifying which method to use to generate the
surrogate response values. Current options are |
jitter_scale |
Character string specifyint the scale on which to perform
the jittering whenever |
... |
Additional optional arguments. (Currently ignored.) |
A numeric vector of class c("numeric", "surrogate") containing
the simulated surrogate response values. Additionally, if nsim > 1,
then the result will contain the attributes:
boot_reps: A matrix with nsim columns, one for each bootstrap
replicate of the surrogate values. Note, these are random and do not
correspond to the original ordering of the data.
boot_id: A matrix with nsim columns. Each column contains the
observation number each surrogate value corresponds to in boot_reps.
(This is used for plotting purposes.)
Surrogate response values require sampling from a continuous distribution;
consequently, the result will be different with every call to
surrogate. The internal functions used for sampling from truncated
distributions are based on modified versions of
truncdist::rtrunc() and truncdist::qtrunc().
For "glm" objects, only the binomial() family is supported.
Liu, Dungang and Zhang, Heping. Residuals and Diagnostics for Ordinal Regression Models: A Surrogate Approach. Journal of the American Statistical Association (accepted). URL http://www.tandfonline.com/doi/abs/10.1080/01621459.2017.1292915?journalCode=uasa20
Nadarajah, Saralees and Kotz, Samuel. R Programs for Truncated Distributions. Journal of Statistical Software, Code Snippet, 16(2), 1-8, 2006. URL https://www.jstatsoft.org/v016/c02.
# Generate data from a quadratic probit model set.seed(101) n <- 2000 x <- runif(n, min = -3, max = 6) z <- 10 + 3*x - 1*x^2 + rnorm(n) y <- ifelse(z <= 0, yes = 0, no = 1) # Scatterplot matrix pairs(~ x + y + z) # Setup for side-by-side plots par(mfrow = c(1, 2)) # Misspecified mean structure fm1 <- glm(y ~ x, family = binomial(link = "probit")) s1 <- surrogate(fm1) scatter.smooth(x, s1 - fm1$linear.predictors, main = "Misspecified model", ylab = "Surrogate residual", lpars = list(lwd = 3, col = "red2")) abline(h = 0, lty = 2, col = "blue2") # Correctly specified mean structure fm2 <- glm(y ~ x + I(x ^ 2), family = binomial(link = "probit")) s2 <- surrogate(fm2) scatter.smooth(x, s2 - fm2$linear.predictors, main = "Correctly specified model", ylab = "Surrogate residual", lpars = list(lwd = 3, col = "red2")) abline(h = 0, lty = 2, col = "blue2")# Generate data from a quadratic probit model set.seed(101) n <- 2000 x <- runif(n, min = -3, max = 6) z <- 10 + 3*x - 1*x^2 + rnorm(n) y <- ifelse(z <= 0, yes = 0, no = 1) # Scatterplot matrix pairs(~ x + y + z) # Setup for side-by-side plots par(mfrow = c(1, 2)) # Misspecified mean structure fm1 <- glm(y ~ x, family = binomial(link = "probit")) s1 <- surrogate(fm1) scatter.smooth(x, s1 - fm1$linear.predictors, main = "Misspecified model", ylab = "Surrogate residual", lpars = list(lwd = 3, col = "red2")) abline(h = 0, lty = 2, col = "blue2") # Correctly specified mean structure fm2 <- glm(y ~ x + I(x ^ 2), family = binomial(link = "probit")) s2 <- surrogate(fm2) scatter.smooth(x, s2 - fm2$linear.predictors, main = "Correctly specified model", ylab = "Surrogate residual", lpars = list(lwd = 3, col = "red2")) abline(h = 0, lty = 2, col = "blue2")