This function implements the method of Dufour04;textualskedastic for testing for heteroskedasticity in a linear regression model.
dufour_etal(
mainlm,
hettest,
R = 1000L,
alternative = c("greater", "less", "two.sided"),
errorgen = stats::rnorm,
errorparam = list(),
seed = 1234,
...
)
Either an object of class
"lm"
(e.g., generated by lm
), or
a list of two objects: a response vector and a design matrix. The objects
are assumed to be in that order, unless they are given the names
"X"
and "y"
to distinguish them. The design matrix passed
in a list must begin with a column of ones if an intercept is to be
included in the linear model. The design matrix passed in a list should
not contain factors, as all columns are treated 'as is'. For tests that
use ordinary least squares residuals, one can also pass a vector of
residuals in the list, which should either be the third object or be
named "e"
.
A character specifying the name of a function
that implements a heteroskedasticity test on a linear regression model.
The function is called with the statonly
argument set to
TRUE
to improve computational efficiency.
An integer specifying the number of Monte Carlo replicates to
generate. Defaults to 1000
.
The tailedness of the test whose statistic is computed by
hettest
function; one of "greater"
(the default),
"less"
, or "two.sided"
.
A function, or a character specifying the name of a
function, from which the random errors are to be generated. The function
should correspond to a continuous probability distribution that has (or
at least can have) a mean of 0. Defaults to rnorm
.
An optional list of parameters to pass to errorgen
.
This argument is ignored if errorgen
is
rnorm
, since mean
must be set to 0, and
sd
is set to 1 because the heteroskedasticity test implemented by
hettest
function is assumed to be scale invariant. If
errorgen
is not rnorm
, errorparam
should be chosen
in such a way that the error distribution has a mean of 0.
An integer specifying a seed to pass to
set.seed
for random number generation. This allows
reproducibility of Monte Carlo results. A value of NA
results in not setting a seed.
Additional arguments to pass to function with name hettest
An object of class
"htest"
. If object
is not assigned, its attributes are displayed in the console as a
tibble
using tidy
.
The test implements a Monte Carlo procedure as follows. (1) The observed
value of the test statistic \(T_0\) is computed using function
with name hettest
. (2) R
replications of the random error
vector are generated from the distribution specified using
errorgen
. (3) R
replications of the test statistic,
\(T_1,T_2,\ldots,T_R\), are computed from the generated error vectors.
(4) The empirical p
-value is computed as
\(\frac{\hat{G}_R(T_0)+1}{R+1}\), where
\(\hat{G}_R(x)=\sum_{j=1}^{R} 1_{T_j \ge x}\), \(1_{\bullet}\)
being the indicator function. The test is right-tailed, regardless of the
tailedness of hettest
. Note that the heteroskedasticity
test implemented by hettest
must have a test statistic that is
continuous and that is invariant with respect to nuisance parameters
(\(\sigma^2\) and \(\beta\)). Note further that if hettest
is goldfeld_quandt
with method
argument
"parametric"
, the replicated Goldfeld-Quandt \(F\) statistics
are computed directly within this function rather than by calling
goldfeld_quandt
, due to some idiosyncratic features of this test.
Note that, if alternative
is set to "two.sided"
, the
one-sided \(p\)-value is doubled (twosidedpval
cannot
be used in this case).
# NOT RUN {
mtcars_lm <- lm(mpg ~ wt + qsec + am, data = mtcars)
dufour_etal(mtcars_lm, hettest = "breusch_pagan")
# }
Run the code above in your browser using DataLab