Title: | Compare Fitted Models |
---|---|
Description: | The fit.models function and its associated methods (coefficients, print, summary, plot, etc.) were originally provided in the robust package to compare robustly and classically fitted model objects. See chapters 2, 3, and 5 in Insightful (2002) 'Robust Library User's Guide' <http://robust.r-forge.r-project.org/Robust.pdf>). The aim of the fit.models package is to separate this fitted model object comparison functionality from the robust package and to extend it to support fitting methods (e.g., classical, robust, Bayesian, regularized, etc.) more generally. |
Authors: | Kjell Konis [aut, cre] |
Maintainer: | Kjell Konis <[email protected]> |
License: | GPL |
Version: | 0.64 |
Built: | 2024-11-13 06:44:40 UTC |
Source: | CRAN |
Returns the location estimated from a location/scatter-type fitted model object.
center(object, ...)
center(object, ...)
object |
a fitted model object, typically. Sometimes
also a |
... |
additional arguments for method functions. |
For a covfm
object containing 2 models, this function plots the
Mahalanobis distance from the first model on the y-axis and the
Mahalanobis distance from the second model on the x-axis.
ddPlot.covfm(x, level = 0.95, strip = "", id.n = 3, ...)
ddPlot.covfm(x, level = 0.95, strip = "", id.n = 3, ...)
x |
a |
level |
a single numeric value between 0 and 1 giving the chi-squared percent point used to compute the outlyingness threshold. |
strip |
a character string printed in the “strip” at the top of the plot. |
id.n |
a single nonnegative integer specifying the number of extreme points to label in the plot. |
... |
additional arguments are passed to |
if the models can be compared then the plotted trellis
object is
invisibly returned. Otherwise x
is returned invisibly.
Returns the squared Mahalanobis distance of all rows in the design (model)
matrix and the sample mean vector
of the columns
of
with respect to the sample covariance matrix
.
This is (for vector
a row of
) defined as
where
and
designMD(object, ...)
designMD(object, ...)
object |
a fitted model object with a |
... |
additional arguments are ignored. |
a numeric vector containing the squared Mahalanobis distances.
stack.lm <- lm(stack.loss ~ ., data = stackloss) # Mahalanobis distance (not squared) sqrt(designMD(stack.lm))
stack.lm <- lm(stack.loss ~ ., data = stackloss) # Mahalanobis distance (not squared) sqrt(designMD(stack.lm))
Produces side-by-side plots of Mahalanobis distance computed using the
location and covariance matrix estimates contained in each element of a
covfm
object.
distancePlot.covfm(x, level = 0.95, id.n = 3, ...)
distancePlot.covfm(x, level = 0.95, id.n = 3, ...)
x |
a |
level |
a single numeric value between 0 and 1 giving the chi-squared percent point used to compute the outlyingness threshold. |
id.n |
a single nonnegative integer specifying the number of extreme points to label in the plot. |
... |
additional arguments are passed to |
the trellis
object is invisibly returned.
When there are 3 or more variables in the data, this function produces
a matrix with ellipses drawn in the upper triangle. The ellipse in cell
of the plot is drawn to be a contour of a standard bivariate
normal with correlation
. One ellipse is drawn in each
cell for each model in the
covfm
object. When there are 2
variables in the data, this function produces a scatter plot of the data
with an overlaid 95% confidence ellipse for each model in the
covfm
object.
ellipsesPlot.covfm(x, ...)
ellipsesPlot.covfm(x, ...)
x |
a |
... |
additional arguments are ignored. |
x is invisibly returned.
Fit a statistical model using different estimators (e.g., robust and least-squares) or combine fitted models into a single object. Generic methods then produce side-by-side comparisons of the parameter estimates and diagnostic plots.
fit.models(model.list, ...)
fit.models(model.list, ...)
model.list |
a list or a character vector containing names of modeling
functions. Only required when |
... |
see details. |
There are two distinct ways the fit.models
function can be used.
The first is to fit the same model using different estimators. In this
case, model.list
should be a character vector or a list where each
element is the name of a modeling function and the remaining arguments (in
...) are the common arguments to the functions in model.list
.
For example, the following command fits robust and least squares linear
models to Brownlee's Stack Loss Plant Data.
fit.models(c("rlm", "lm"), stack.loss ~ ., data = stackloss)
The resulting
fit.models
object is a list with the output of
rlm(stack.loss ~ ., data = stackloss)
in the first element and
lm(stack.loss ~ ., data = stackloss)
in the second. The
class attribute of the returned list is set (in this case) to "lmfm"
which is the fit.models
class (fmclass) for comparing linear-model-like
fits.
The second use of fit.models is to combine fitted model objects. In
this case, fit.models
combines its arguments into a fit.models object
(a list where element is occupied by argument
and sets the
class attribute to the appropriate
fit.models
class.
The returned object is a list containing the fitted models. The class of the retuned object depends on the classes of the model objects it contains.
fmclass.add.class
for adding a class to an existing
fit.models class and fmclass.register
to create a new
fit.models class.
# First, use fit.models to fit robust and least squares linear # regression models to Brownlee's Stack Loss Plant Data. # Step 1: rlm (robust linear model) is in the MASS package. library(MASS) # Step 2: tell fit.models rlm can be compared to lm fmclass.add.class("lmfm", "rlm") fm1 <- fit.models(c("rlm", "lm"), stack.loss ~ ., data = stackloss) summary(fm1) #rlm does not provide p-values or Multiple R-squared # Second, use fit.models to combine fitted models into a # fit.models object. lm.complete <- lm(stack.loss ~ ., data = stackloss) lm.clean <- lm(stack.loss ~ ., data = stackloss, subset = 5:20) fm2 <- fit.models(lm.clean, lm.complete) summary(fm2) plot(fm2) # Name the models in the fit.models object. fm3 <- fit.models(c(Robust = "rlm", "Least Squares" = "lm"), stack.loss ~ ., data = stackloss) fm4 <- fit.models(Clean = lm.clean, Complete = lm.complete)
# First, use fit.models to fit robust and least squares linear # regression models to Brownlee's Stack Loss Plant Data. # Step 1: rlm (robust linear model) is in the MASS package. library(MASS) # Step 2: tell fit.models rlm can be compared to lm fmclass.add.class("lmfm", "rlm") fm1 <- fit.models(c("rlm", "lm"), stack.loss ~ ., data = stackloss) summary(fm1) #rlm does not provide p-values or Multiple R-squared # Second, use fit.models to combine fitted models into a # fit.models object. lm.complete <- lm(stack.loss ~ ., data = stackloss) lm.clean <- lm(stack.loss ~ ., data = stackloss, subset = 5:20) fm2 <- fit.models(lm.clean, lm.complete) summary(fm2) plot(fm2) # Name the models in the fit.models object. fm3 <- fit.models(c(Robust = "rlm", "Least Squares" = "lm"), stack.loss ~ ., data = stackloss) fm4 <- fit.models(Clean = lm.clean, Complete = lm.complete)
The fit.models
package maintains a list of comparable models.
These functions provide an api to modify this list.
fmclass.register(fmclass, classes, validation.function = NULL) fmclass.add.class(fmclass, class, warn = TRUE)
fmclass.register(fmclass, classes, validation.function = NULL) fmclass.add.class(fmclass, class, warn = TRUE)
fmclass |
a character string naming the fit.models class to be added. |
classes |
a character vector naming one or more classes that can be
compared by the methods defined for the fit.models class in |
validation.function |
a function returning |
class |
a character string specifying a class compatible with the
methods of |
warn |
a logical value. If TRUE, a warning is printed if |
See the package vignette.
a null value is invisibly returned.
Produces an overlaid kernel density plot.
overlaidKernelDensityPlot(x, fun, ...)
overlaidKernelDensityPlot(x, fun, ...)
x |
a |
fun |
a function to extract the desired quantity from |
... |
additional arguments are passed to
|
the trellis
object is invisibly returned.
Produces an overlaid normal QQ plot.
overlaidQQPlot(x, fun, ...)
overlaidQQPlot(x, fun, ...)
x |
a |
fun |
a function to extract the desired quantity from |
... |
additional arguments are passed to
|
the trellis
object is invisibly returned.
Produces a scatter plot of the data with overlaid fits.
overlaidSimpleRegressionPlot(x, lwd.reg, col.reg, ...)
overlaidSimpleRegressionPlot(x, lwd.reg, col.reg, ...)
x |
a |
lwd.reg |
a vector with length equal to the number of models in
|
col.reg |
a vector with length equal to the number of models in
|
... |
additional arguments are passed to
|
the trellis
object is invisibly returned.
Generic plot method for “covfm” objects.
## S3 method for class 'covfm' plot(x, which.plots = 1:4, ...)
## S3 method for class 'covfm' plot(x, which.plots = 1:4, ...)
x |
a |
which.plots |
either |
... |
additional arguments are passed to the plot subfunctions. |
x
is returned invisibly.
Produces a set of comparison diagnostic plots. The plot options are
Deviance Residuals vs. Predicted Values,
Response vs. Fitted Values,
Normal QQ Plot of Pearson Residuals,
Normal QQ Plot of Deviance Residuals,
Pearson Residuals vs. Mahalanobis Distance,
Sqrt Deviance Residuals vs. Predicted Values.
## S3 method for class 'glmfm' plot(x, which.plots = 1:6, ...)
## S3 method for class 'glmfm' plot(x, which.plots = 1:6, ...)
x |
a |
which.plots |
either |
... |
other parameters to be passed through to plotting functions. |
x
is invisibly returned.
The selected plots are drawn on a graphics device.
sideBySideQQPlot
for 4 and 5 and
sideBySideScatterPlot
for the others.
# From ?glm: # A Gamma example, from McCullagh & Nelder (1989, pp. 300-2) clotting <- data.frame( u = c(5,10,15,20,30,40,60,80,100), lot1 = c(118,58,42,35,27,25,21,19,18), lot2 = c(69,35,26,21,18,16,13,12,12)) lot1 <- glm(lot1 ~ log(u), data = clotting, family = Gamma) lot2 <- glm(lot2 ~ log(u), data = clotting, family = Gamma) fm <- fit.models(lot1, lot2) plot(fm)
# From ?glm: # A Gamma example, from McCullagh & Nelder (1989, pp. 300-2) clotting <- data.frame( u = c(5,10,15,20,30,40,60,80,100), lot1 = c(118,58,42,35,27,25,21,19,18), lot2 = c(69,35,26,21,18,16,13,12,12)) lot1 <- glm(lot1 ~ log(u), data = clotting, family = Gamma) lot2 <- glm(lot2 ~ log(u), data = clotting, family = Gamma) fm <- fit.models(lot1, lot2) plot(fm)
Produces a set of comparison diagnostic plots. The plot options are
Normal QQ Plot of Residuals,
Kernel Density Estimate of Residuals,
Residuals vs. Mahalanobis Distance,
Residuals vs. Fitted Values,
Sqrt Residuals vs. Fitted Values,
Response vs. Fitted Values,
Residuals vs. Index (Time),
Overlaid Normal QQ Plot of Residuals,
Overlaid Kernel Density Estimate of Residuals,
Scatter Plot with Overlaid Fits (for simple linear regression models).
## S3 method for class 'lmfm' plot(x, which.plots = 1:10, ...)
## S3 method for class 'lmfm' plot(x, which.plots = 1:10, ...)
x |
an |
which.plots |
either |
... |
additional parameters are ignored. |
x
is invisibly returned.
The selected plots are drawn on a graphics device.
See sideBySideQQPlot
for 2,
sideBySideKernelDensityPlot
for 3,
sideBySideIndexPlot
for 8,
overlaidQQPlot
for 9,
overlaidKernelDensityPlot
for 10,
overlaidSimpleRegressionPlot
for 11, and
sideBySideScatterPlot
for the others.
data(stackloss) stack.lm <- lm(stack.loss ~ ., data = stackloss) stack.clean <- lm(stack.loss ~ ., data = stackloss, subset = 5:20) fm <- fit.models(stack.clean, stack.lm) plot(fm)
data(stackloss) stack.lm <- lm(stack.loss ~ ., data = stackloss) stack.clean <- lm(stack.loss ~ ., data = stackloss, subset = 5:20) fm <- fit.models(stack.clean, stack.lm) plot(fm)
Overlaid screeplot for the models in a “covfm” object.
screePlot.covfm(x, npcs, strip = "", ...)
screePlot.covfm(x, npcs, strip = "", ...)
x |
a |
npcs |
a postive integer value specifying the number of components to be plotted. |
strip |
a character string printed in the “strip” at the top of the plot. |
... |
additional arguments are passed to |
the trellis
object is invisibly returned.
Produces side-by-side index (time) plots.
sideBySideIndexPlot(x, fun, level = 0.95, id.n = 3, ...)
sideBySideIndexPlot(x, fun, level = 0.95, id.n = 3, ...)
x |
a |
fun |
a function to extract the desired quantity from |
level |
a numeric value between 0 and 1 specifying the confidence level used to draw the threshold in the plot. |
id.n |
a non-negative integer value specifying the number of extreme points to identify. |
... |
any additional arguments are passed to
|
the trellis
object is invisibly returned.
Produces side-by-side kernel density estimate plots.
sideBySideKernelDensityPlot(x, fun, ...)
sideBySideKernelDensityPlot(x, fun, ...)
x |
a |
fun |
a function to extract the desired quantity from |
... |
additional arguments are passed to
|
the trellis
object is invisibly returned.
Produces side-by-side QQ plots. An optional simulated confidence envelope can be included in each plot.
sideBySideQQPlot( x, fun, envelope = TRUE, half.normal = FALSE, n.samples = 250, level = 0.95, id.n = 3, qqline = TRUE, ... )
sideBySideQQPlot( x, fun, envelope = TRUE, half.normal = FALSE, n.samples = 250, level = 0.95, id.n = 3, qqline = TRUE, ... )
x |
a |
fun |
a function to extract the desired quantity from |
envelope |
a logical value. If |
half.normal |
a logical value. If |
n.samples |
a positive integer value giving the number of samples to compute in the simulation of the confidence envelope. |
level |
a numeric value between 0 and 1 specifying the confidence level for the envelope. |
id.n |
a non-negative integer value specifying the number of extreme points to identify. |
qqline |
a logical value. If |
... |
additional arguments are passed to
|
the trellis
object is invisibly returned.
Produces a side-by-side scatter plot.
sideBySideScatterPlot(object, x.fun, y.fun, ...)
sideBySideScatterPlot(object, x.fun, y.fun, ...)
object |
a |
x.fun |
a function to extract the x-axis quantity. |
y.fun |
a function to extract the y-axis quantity. |
... |
additional arguments. |
the trellis
object is invisibly returned.
Generic summary method for “covfm” objects.
## S3 method for class 'covfm' summary(object, corr = FALSE, ...)
## S3 method for class 'covfm' summary(object, corr = FALSE, ...)
object |
a “covfm” object. |
corr |
a logical value passed as an attribute to the |
... |
additional arguments for the summary method. |
Compute a summary of each model in a glmfm
object.
## S3 method for class 'glmfm' summary(object, correlation = FALSE, ...)
## S3 method for class 'glmfm' summary(object, correlation = FALSE, ...)
object |
a glmfm object. |
correlation |
a logical value. If |
... |
additional arguments required by the generic
|
a list with class summary.glmfm whose elements are summaries of each
model in object
.
# From ?glm: # A Gamma example, from McCullagh & Nelder (1989, pp. 300-2) clotting <- data.frame( u = c(5,10,15,20,30,40,60,80,100), lot1 = c(118,58,42,35,27,25,21,19,18), lot2 = c(69,35,26,21,18,16,13,12,12)) lot1 <- glm(lot1 ~ log(u), data = clotting, family = Gamma) lot2 <- glm(lot2 ~ log(u), data = clotting, family = Gamma) fm <- fit.models(lot1, lot2) summary(fm)
# From ?glm: # A Gamma example, from McCullagh & Nelder (1989, pp. 300-2) clotting <- data.frame( u = c(5,10,15,20,30,40,60,80,100), lot1 = c(118,58,42,35,27,25,21,19,18), lot2 = c(69,35,26,21,18,16,13,12,12)) lot1 <- glm(lot1 ~ log(u), data = clotting, family = Gamma) lot2 <- glm(lot2 ~ log(u), data = clotting, family = Gamma) fm <- fit.models(lot1, lot2) summary(fm)
Compute a summary of each model in an lmfm
object.
## S3 method for class 'lmfm' summary(object, correlation = FALSE, ...)
## S3 method for class 'lmfm' summary(object, correlation = FALSE, ...)
object |
an |
correlation |
a logical value. If TRUE, the correlation matrices for the coefficients are included in the summaries. |
... |
additional arguments required by the generic
|
a list with class summary.lmfm whose elements are summaries of each
model in object
.
data(stackloss) m1 <- lm(stack.loss ~ ., data = stackloss) m2 <- lm(stack.loss ~ . - Acid.Conc., data = stackloss) fm <- fit.models(m1, m2) print(fm.sum <- summary(fm))
data(stackloss) m1 <- lm(stack.loss ~ ., data = stackloss) m2 <- lm(stack.loss ~ . - Acid.Conc., data = stackloss) fm <- fit.models(m1, m2) print(fm.sum <- summary(fm))
Retrieve a correlation matrix estimate from a
fitted model object. The default method uses
cov2cor
to scale the
covariance matrix returned by
vcov
.
vcor(object, ...)
vcor(object, ...)
object |
a fitted model object, typically. Sometimes
also a |
... |
additional arguments for method functions. |