`waldtest`

is a generic function for carrying out Wald tests.
The default method can be employed for comparing nested (generalized)
linear models (see details below).

`waldtest(object, …)` # S3 method for default
waldtest(object, …, vcov = NULL,
test = c("Chisq", "F"), name = NULL)

# S3 method for formula
waldtest(object, …, data = list())

# S3 method for lm
waldtest(object, …, test = c("F", "Chisq"))

object

an object. See below for details.

…

further object specifications passed to methods. See below for details.

vcov

a function for estimating the covariance matrix of the regression
coefficients, e.g., `vcovHC`

. If only two models
are compared it can also be the covariance matrix of the more general
model.

test

character specifying whether to compute the large sample Chi-squared statistic (with asymptotic Chi-squared distribution) or the finite sample F statistic (with approximate F distribution).

name

a function for extracting a suitable name/description from
a fitted model object. By default the name is queried by calling
`formula`

.

data

a data frame containing the variables in the model.

An object of class `"anova"`

which contains the residual degrees of freedom,
the difference in degrees of freedom, Wald statistic
(either `"Chisq"`

or `"F"`

) and corresponding p value.

`waldtest`

is intended to be a generic function for comparisons
of models via Wald tests. The default method consecutively compares
the fitted model object `object`

with the models passed in `…`

.
Instead of passing the fitted model objects in `…`

, several other
specifications are possible. For all objects in `list(object, …)`

the function tries to consecutively compute fitted models using the following
updating algorithm:

For each two consecutive objects,

`object1`

and`object2`

say, try to turn`object2`

into a fitted model that can be compared to (the already fitted model object)`object1`

.If

`object2`

is numeric, the corresponding element of`attr(terms(object1), "term.labels")`

is selected to be omitted.If

`object2`

is a character, the corresponding terms are included into an update formula like`. ~ . - term2a - term2b`

.If

`object2`

is a formula, then compute the fitted model via`update(object1, object2)`

.

Consequently, the models in `…`

can be specified as integers, characters
(both for terms that should be eliminated from the previous model), update formulas or
fitted model objects. Except for the last case, the existence of an `update`

method is assumed. See also the examples for an illustration.

Subsequently, a Wald test for each two consecutive models is carried out. This
is similar to `anova`

(which typically performs likelihood-ratio tests),
but with a few differences. If only one fitted model object is specified, it is compared
to the trivial model (with only an intercept). The test can be either the finite sample
F statistic or the asymptotic Chi-squared statistic (\(F = Chisq/k\) if \(k\) is the
difference in degrees of freedom). The covariance matrix is always estimated on the more general
of two subsequent models (and not only in the most general model overall). If `vcov`

is specified, HC and HAC estimators can also be plugged into `waldtest`

.

The default method is already very general and applicable to a broad range of fitted
model objects, including `lm`

and `glm`

objects. It can be
easily made applicable to other model classes as well by providing suitable methods
to the standard generics `terms`

(for determining the variables in the model
along with their names), `update`

(unless only fitted model objects are passed
to `waldtest`

, as mentioned above), `nobs`

(or `residuals`

, used for determining
the number of observations), `df.residual`

(needed only for the F statistic),
`coef`

(for extracting the coefficients; needs to be named matching the names
in `terms`

), `vcov`

(can be user-supplied; needs to be named matching the
names in `terms`

). Furthermore, some means of determining a suitable `name`

for
a fitted model object can be specified (by default this is taken to be the result of
a call to `formula`

, if available).

The `"formula"`

method fits a `lm`

first and then calls the `"lm"`

method. The `"lm"`

method just calls the default method, but sets the default
test to be the F test.

# NOT RUN { ## fit two competing, non-nested models and their encompassing ## model for aggregate consumption, as in Greene (1993), ## Examples 7.11 and 7.12 ## load data and compute lags data(USDistLag) usdl <- na.contiguous(cbind(USDistLag, lag(USDistLag, k = -1))) colnames(usdl) <- c("con", "gnp", "con1", "gnp1") ## C(t) = a0 + a1*Y(t) + a2*C(t-1) + u fm1 <- lm(con ~ gnp + con1, data = usdl) ## C(t) = b0 + b1*Y(t) + b2*Y(t-1) + v fm2 <- lm(con ~ gnp + gnp1, data = usdl) ## Encompassing model fm3 <- lm(con ~ gnp + con1 + gnp1, data = usdl) ## a simple ANOVA for fm3 vs. fm2 waldtest(fm3, fm2) anova(fm3, fm2) ## as df = 1, the test is equivalent to the corresponding t test in coeftest(fm3) ## various equivalent specifications of the two models waldtest(fm3, fm2) waldtest(fm3, 2) waldtest(fm3, "con1") waldtest(fm3, . ~ . - con1) ## comparing more than one model ## (equivalent to the encompassing test) waldtest(fm1, fm3, fm2) encomptest(fm1, fm2) ## using the asymptotic Chisq statistic waldtest(fm3, fm2, test = "Chisq") ## plugging in a HC estimator if(require(sandwich)) waldtest(fm3, fm2, vcov = vcovHC) # }

Run the code above in your browser using DataCamp Workspace