Learn R Programming

spaMM (version 3.2.0)

get_matrix: Extract matrices from a fit

Description

get_matrix is a first attempt at a unified extractor of various matrices from a fit. All augmented matrices follow (Henderson's) block order (upper blocks: X,Z; lower blocks: 0,I). get_ZALMatrix returns the design matrix for the random effects \(v\).

Usage

get_matrix(object, which="model.matrix", augmented=TRUE, ...)
get_ZALMatrix(object, as_matrix, force_bind=FALSE)

Arguments

object

An object of class HLfit, as returned by the fitting functions in spaMM.

augmented

Boolean; whether to return an augmented matrix for all model coefficients (fixed-effects coefficients and random-effect predictions) or only for fixed effects. Not operative for all which values (currently only for which="left_ginv").

which

Which element to extract. For "model.matrix", the design matrix for fixed effects (similarly to stats::model.matrix); for "ZAL", the design matrix for random effects (same as codeget_ZALMatrix()); for "AugX", the (unweighted) augmented design matrix of the least-square problem; for "hat_matrix", the projection matrix that gives model predictions from the (augmented) response vector; for "left_ginv", the pseudo-inverse that gives the model coefficients from the (augmented) response vector. See Details for definitiosn and further options.

as_matrix

Deprecated.

force_bind

Boolean; with the default value FALSE, the function may return an object of class ZAXlist, which is poorly documented and for development purposes only.

Other arguments that may be needed in some future versions of spaMM.

Value

A matrix, possibly in sparse-matrix format.

Details

(Given the pain that is to to write maths in R documentation files, readers are gently asked to be tolerant about any imperfections of the following).

Model coefficents estimates of a (weighted) linear model can be written as \(\bold{(X'WX)}^{-1}\bold{X'Wy}\) where X is the design matrix for fixed effects, W a diagonal weight matrix, and y the response vector. In a linear mixed model, the same expression holds in terms of Henderson's augmented design matrix, of augmented (still diagonal) weight matrix, and of augmented response vector. For GLMMs and hierarchical GLMs generally, the solution of each step of the iteratively reweighted least squares algorithm again has the same expression in terms of appropriately defined augmented matrices and vectors.

get_matrix returns, for given values of the which argument, the following matrices from the model fit: "AugX": X; "wei_AugX": WX; "wAugX": \(\sqrt{\bold{W}}\)X; "left_ginv": \(\bold{X^{-}}=\bold{(X'WX)}^{-1}\bold{X'W}\) (viewed as a pseudo-inverse since \eqnboldX^-X is an identity matrix); "hat_matrix": \(\bold{X X^{-}}=\bold{X }\bold{(X'WX)}^{-1}\bold{X'W}\).