# swp: The Matrix Sweep Operator In matlib: Matrix Functions for Teaching and Learning Linear Algebra and Multivariate Statistics

## Description

The swp function “sweeps” a matrix on the rows and columns given in index to produce a new matrix with those rows and columns “partialled out” by orthogonalization. This was defined as a fundamental statistical operation in multivariate methods by Beaton (1964) and expanded by Dempster (1969). It is closely related to orthogonal projection, but applied to a cross-products or covariance matrix, rather than to data.

## Usage

 1 swp(M, index) 

## Arguments

 M a numeric matrix index a numeric vector indicating the rows/columns to be swept. The entries must be less than or equal to the number or rows or columns in M. If missing, the function sweeps on all rows/columns 1:min(dim(M)).

## Details

If M is the partitioned matrix

≤ft[ \begin{array}{cc} \mathbf {R} & \mathbf {S} \\ \mathbf {T} & \mathbf {U} \end{array} \right]

where R is q \times q then swp(M, 1:q) gives

≤ft[ \begin{array}{cc} \mathbf {R}^{-1} & \mathbf {R}^{-1}\mathbf {S} \\ -\mathbf {TR}^{-1} & \mathbf {U}-\mathbf {TR}^{-1}\mathbf {S} \\ \end{array} \right]

## Value

the matrix M with rows and columns in indices swept.

## References

Beaton, A. E. (1964), The Use of Special Matrix Operations in Statistical Calculus, Princeton, NJ: Educational Testing Service.

Dempster, A. P. (1969) Elements of Continuous Multivariate Analysis. Addison-Wesley Publ. Co., Reading, Mass.

Proj, QR
 1 2 3 4 5 6 7 8 data(therapy) mod3 <- lm(therapy ~ perstest + IE + sex, data=therapy) X <- model.matrix(mod3) XY <- cbind(X, therapy=therapy\$therapy) XY M <- crossprod(XY) swp(M, 1) swp(M, 1:2)