Weighted Least Squares wrapper¶
Source: R/fitWLS.R · Help: ?fitWLS
Refit a model by feasible generalised least squares, weighting each observation by the inverse of an estimated error variance.
Usage¶
Arguments¶
model- A fitted model of class
lm. data- Optional base::data.frame holding the variables named in
var_formula. It is needed only when they are not in the model frame, and is matched to the fitted rows by row name. var_formula- One-sided formula naming the variance regressors, for
example
~ x.NULL, the default, uses the regressors ofmodel. form- Character scalar choosing the variance function:
"exponential"(the default) or"power". See the section on variance functions.
Value¶
A new lm object fitted with weights. The estimated variances are
attached as the "variance_model" attribute, and the fitted variance
function (its form, regressors and coefficients) as the
"variance_function" attribute, which is what
performVarianceFormTest() reads, and what the bootstrap tests read to
estimate the weights again in each replicate. When model was fitted
with weights, the attribute also holds them, as initial_weights.
Details¶
The variance is modelled, not read off the residuals directly. A single squared residual is a one-degree-of-freedom estimate of \(\sigma_i^2\) and far too noisy to invert: weighting by \(1/e_i^2\) hands almost all of the weight to whichever observations the initial fit happened to reproduce most closely. This function instead fits a variance function to the squared residuals and takes \(\hat\sigma_i^2\) from its fitted values, the standard feasible-GLS recipe; weights are \(1/\hat\sigma_i^2\).
Variance functions¶
form chooses the shape of the variance function and var_formula the
variables \(z_i\) it depends on. Both forms are linear in the logarithm
of the variance and carry their own constant.
"exponential"- \(\log \sigma_i^2 = \gamma_0 + z_i^\top \gamma\),
the multiplicative model of Harvey (1976), estimated by regressing
\(\log e_i^2\) on \(z_i\). The log scale keeps the fitted variances
positive without constraining the auxiliary regression. This is the
default, and with
var_formula = NULLit is whatfitWLS()has computed since 0.9.0. "power"- \(\log \sigma_i^2 = \gamma_0 + \sum_j \delta_j \log z_{ij}\), that is \(\sigma_i^2 \propto \prod_j z_{ij}^{\delta_j}\), the model of Park (1966). It is the exponential form in the logarithms of the regressors, which must be strictly positive. \(\delta = 2\) is the textbook case of a standard deviation proportional to \(z\).
Other log-linear shapes are written through var_formula, for example
~ x + I(x^2) or ~ sqrt(x). The additive model
\(\sigma_i^2 = \alpha_0 + z_i^\top \alpha\) is not offered: nothing keeps
its fitted variances positive, and in the validation study it was not
usable in a material share of samples drawn from an additive model itself
(inst/validation/README.md).
Residuals that are numerically zero are floored before the logarithm is taken, with a warning.
Weights estimated this way are consistent under a correctly specified variance function, so standard errors from the returned fit are usable. They were not before 0.9.0, when the weights were the raw inverse squared residuals: the weighted residual sum of squares then collapsed towards \(n\) regardless of the data, and nominal 95% intervals covered the truth about 10% of the time.
Whether the variance function is correctly specified can be tested: pass
the result to performVarianceFormTest().
A test of constant variance on the result takes its weights as known,
although they were estimated from the same data, so its reference
distribution is only approximate: see the validation study in
inst/validation/README.md. The bootstrap
tests performWildBootstrapTest(), performWhiteTestBootstrap() and
bootstrapTestStatistic() allow for the estimation. Since 0.15.0 they run
fitWLS() again on each replicate by default: they fit it as model was
fitted, fit the recorded variance function again to its residuals, on the
recorded regressors, and refit it with the weights that gives. Their
argument reestimate_weights = FALSE holds the weights fixed instead, as
they did before 0.15.0.
If the variance function cannot be fitted, or yields no usable variation, the function falls back to equal weights, which reduces the result to the original OLS fit.
The weighted fit keeps the design and the offset of model, whether the
offset was given as an argument, lm(..., offset = o), or written in the
formula, offset(o). Its coefficients, fitted values and residuals are
those lm() returns for the same call with the estimated weights added.
On a model fitted with weights, the refit uses the estimated weights in
their place, and weights() of the result returns them.
References¶
Harvey, A. C. (1976). Estimating regression models with multiplicative heteroscedasticity. Econometrica, 44(3), 461–465. doi:10.2307/1913974
Park, R. E. (1966). Estimation with heteroscedastic error terms. Econometrica, 34(4), 888. doi:10.2307/1910108
See also¶
performVarianceFormTest() to test the variance function that was
fitted.