wals {WALS} | R Documentation |
Weighted-Average Least Squares for linear regression models
Description
Performs model averaging for linear regression models using the Weighted-Average Least Squares method by Magnus et al. (2010). See also De Luca and Magnus (2011), Kumar and Magnus (2013) and Magnus and De Luca (2016).
Usage
wals(x, ...)
## S3 method for class 'formula'
wals(
formula,
data,
subset = NULL,
na.action = NULL,
weights = NULL,
offset = NULL,
prior = weibull(),
model = TRUE,
keepY = TRUE,
keepX = FALSE,
sigma = NULL,
...
)
## S3 method for class 'matrix'
wals(
x,
x2,
y,
subset = NULL,
na.action = NULL,
weights = NULL,
offset = NULL,
prior = weibull(),
keepY = TRUE,
keepX = FALSE,
sigma = NULL,
...
)
## Default S3 method:
wals(x, ...)
Arguments
x |
Design matrix of focus regressors. Usually includes a constant
(column full of 1s) and can be generated using |
... |
Arguments for workhorse |
formula |
an object of class |
data |
an optional data frame, list or environment
(or object coercible by |
subset |
an optional vector specifying a subset of observations to be used in the fitting process. |
na.action |
not implemented yet. |
weights |
not implemented yet. |
offset |
not implemented yet. |
prior |
Object of class |
model |
if |
keepY |
if |
keepX |
if |
sigma |
if NULL (default), then the variance of the error term is
estimated. See |
x2 |
Design matrix of auxiliary regressors. Usually does not include
a constant column and can also be generated using |
y |
Response as vector. |
Details
R port of MATLAB code wals.m (version 2.0, revision 18 December 2013) by J.R. Magnus and G. De Luca, available from https://www.janmagnus.nl/items/WALS.pdf. Calculates WALS estimates when focus regressors (X1) are present in all submodels and model averaging takes place over the auxiliary regressors (X2).
Formulas typically contain two parts, i.e. they are of the form
"y ~ X11 + X12 | X21 + X22
", where the variables before "|
" are
the focus regressors (includes a constant by default) and the ones after
"|
" are the auxiliary regressors. If only a one-part formula is
specified, then all regressors are considered as auxiliary regressors and only
a constant is employed as focus regressor, i.e.
"y ~ X1 + X2
" is equivalent to "y ~ 1 | X1 + X2
".
WARNING: Interactions in formula do not work properly yet. It is recommended to manually create the interactions beforehand and then to insert them as 'linear terms' in the formula.
wals.default()
raises an error if x
is not an object of class
"matrix"
or a class that extends "matrix"
. Otherwise it calls
wals.matrix()
. It is a modified version of glmboost.default
from the mboost
package version 2.9-8 (2023-09-06) (Hofner et al. 2014).
Value
wals.formula()
returns an object of class
"wals"
. This is a list that contains all elements returned from
walsFit
and additionally
y |
If |
x |
list. If |
weights |
returns the argument |
offset |
returns the argument |
cl |
Call of the function. |
formula |
|
terms |
List containing the model terms of the focus and auxiliary regressors separately, as well as for the full model. |
levels |
List containing the levels of the focus and auxiliary regressors separately, as well as for the full model. |
contrasts |
List containing the contrasts of the design matrices of focus and auxiliary regressors. |
model |
If |
See returns of walsFit
for more details.
wals.matrix()
returns an object of class "walsMatrix"
,
which inherits from "wals"
. This is a list that contains all elements
returned from walsFit
and additionally the response y
,
the list x
with model matrices x1
and x2
, the call
cl
, offset
and weights
.
wals.default()
raises an error if x
is not an object
of class "matrix"
or a class that extends "matrix"
. Otherwise
returns an object of class "walsMatrix"
. See above for more details.
References
De Luca G, Magnus JR (2011).
“Bayesian model averaging and weighted-average least squares: Equivariance, stability, and numerical issues.”
The Stata Journal, 11(4), 518–544.
doi:10.1177/1536867X1201100402.
Hofner B, Mayr A, Robinzonov N, Schmid M (2014).
“Model-based Boosting in R: A Hands-on Tutorial Using the R Package mboost.”
Computational Statistics, 29, 3–35.
Kumar K, Magnus JR (2013).
“A characterization of Bayesian robustness for a normal location parameter.”
Sankhya B, 75(2), 216–237.
doi:10.1007/s13571-013-0060-9.
Magnus JR, De Luca G (2016).
“Weighted-average least squares (WALS): A survey.”
Journal of Economic Surveys, 30(1), 117-148.
doi:10.1111/joes.12094.
Magnus JR, Powell O, Prüfer P (2010).
“A comparison of two model averaging techniques with an application to growth empirics.”
Journal of Econometrics, 154(2), 139-153.
doi:10.1016/j.jeconom.2009.07.004.
Examples
## Replicate table on p. 534 of De Luca & Magnus (2011)
fitDM <- wals(gdpgrowth ~ lgdp60 + equipinv + school60 + life60 + popgrowth |
law + tropics + avelf + confucian, data = GrowthMPP,
prior = laplace())
tableDM <- cbind("coef" = coef(fitDM), "se" = sqrt(diag(vcov(fitDM))))
print(round(tableDM, 7))
## Replicate first panel of Table I in Amini & Parmeter (2012)
data("datafls", package = "BMS")
# NOTE: Authors manually scale data, then rescale the resulting coefs and se.
X <- model.matrix(y ~ ., data = datafls)
Xscaled <- apply(X, MARGIN = 2, function(x) x/max(x))
Xscaled <- Xscaled[,-1]
scaleVector <- apply(X, MARGIN = 2, function(x) max(x))
flsScaled <- as.data.frame(cbind(y = datafls$y, Xscaled))
# NOTE: prescale = FALSE, still used old version of WALS in Magnus et al. (2010).
# Not recommended anymore!
fitFLS <- wals(y ~ 1 | ., data = flsScaled, prescale = FALSE, eigenSVD = FALSE,
prior = laplace())
tableFLS <- cbind('coef' = coef(fitFLS)/scaleVector,
'se' = sqrt(diag(vcov(fitFLS)))/scaleVector)
printVars <- c("(Intercept)", "GDP60", "Confucian", "LifeExp", "EquipInv",
"SubSahara", "Muslim", "RuleofLaw")
print(round(tableFLS[printVars,], 4))
## Replicate third panel of Table I in Amini & Parmeter (2012)
data("SDM", package = "BayesVarSel")
# rescale response
SDM$y <- SDM$y / 100
# NOTE: Authors manually scale data, then rescale the resulting coefs and se.
X <- model.matrix(y ~ ., data = SDM)
Xscaled <- apply(X, MARGIN = 2, function(x) x/max(x))
Xscaled <- Xscaled[,-1]
scaleVector <- apply(X, MARGIN = 2, function(x) max(x))
SDMscaled <- as.data.frame(cbind(y = SDM$y, Xscaled))
# NOTE: prescale = FALSE, still used old version of WALS in Magnus et al. (2010).
# Not recommended anymore!
fitDW <- wals(y ~ 1 | ., data = SDMscaled, prescale = FALSE, eigenSVD = FALSE,
prior = laplace())
tableDW <- cbind(coef(fitDW)/scaleVector, sqrt(diag(vcov(fitDW)))/scaleVector)
printVars <- c("(Intercept)", "EAST", "P60", "IPRICE1", "GDPCH60L", "TROPICAR")
print(round(tableDW[printVars,], 5))
## Example for wals.matrix()
X <- model.matrix(mpg ~ disp + hp + wt + vs + am + carb, data = mtcars)
X1 <- X[,c("(Intercept)", "disp", "hp", "wt")] # focus
X2 <- X[,c("vs", "am", "carb")] # auxiliary
y <- mtcars$mpg
wals(X1, X2, y, prior = weibull())