**MeanMean**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The sweep operator as defined in (Dempster, 1969), commonly referred to as the SWP operator, is a useful tool for a computational statistician working with covariance matrices. In particular, the SWP operator allows a statistician to quickly regress all variables against one specified variable, obtaining OLS estimates for regression coefficients and variances in a single application. Subsequent applications of the SWP operator allows for regressing against more variables.

In this blog post, I will define the sweep operator, provide an application of the sweep data to simulated data, and provide some references for further study. Examples will be provided in R through the `ISR3`

package (Lisic, 2016).

The basic SWP operator is parameterized by a matrix and a set of indices associated with rows and columns of the matrix being swept. E.g. to sweep the matrix by the index, such that , the operator has the form . The operator may also be written more descriptively by listing the elements of as . The application of the SWP operator to the row and column of produces a new matrix with elements

As an example, consider the application of the SWP operator to the first row and column of the covariance matrix for the random variables ,

resulting in

How is this useful? Well, if we substitute the covariance matrix with the sample covariance matrix, where is an estimator of and

is an estimator of , then by sweeping by the index, , we can get the simple linear regression (SLR) estimates for for all :

where is the simple linear model estimator of the regression coefficient for the linear model for , is the inverse of the covariance matrix of unscaled by , and is the sample covariance of the residuals from the regression models on unscaled by .

## Example

Lets try this with some real data. I have used the SWP function included with the `ISR3`

package.

library(ISR3) set.seed(12) n <- 100 p <- 3 # generate a positive definite matrix for covariance Sigma <- rWishart(1,p+1,diag(p))[,,1] Sigma_inv <- chol2inv(chol(Sigma)) Sigma_inv_chol <- chol(Sigma_inv) # generate 'n' multivate normal deviates X <- Sigma_inv_chol %*% matrix(rnorm(n*p),nrow=p) X <- t(X) colnames(X) <- sprintf("X_%d",1:p) XX <- t(X) %*% X #Sweep by the first row/column of XX SWP_1 <- SWP(XX,1) SWP_1

## X_1 X_2 X_3 ## X_1 -0.002381092 0.1375626 -0.01674788 ## X_2 0.137562633 50.0987932 -4.95181624 ## X_3 -0.016747875 -4.9518162 41.73979419

We can compare this output with the results from the simple linear models and :

# linear models fit_21 <- lm(X_2 ~ -1 + X_1, data=as.data.frame(X)) fit_31 <- lm(X_3 ~ -1 + X_1, data=as.data.frame(X)) # SLR coefficents for X_1 print(fit_21$coefficients)

## X_1 ## 0.1375626

print(fit_31$coefficients)

## X_1 ## -0.01674788

# sum of squares print(sum(fit_21$residuals^2))

## [1] 50.09879

print(sum(fit_31$residuals^2))

## [1] 41.73979

# covariance between X_2 and X_3 given X_1 print(sum(fit_21$residuals * fit_31$residuals))

## [1] -4.951816

# negative inverse of the covariance matrix of X_1 print( -1/((n-1)*var(X[,1])) )

## [1] -0.002381293

Here we see that the coefficients from `lm`

match up to our swept results. Likewise, the diagonal terms for the second and third column are equivalent to the sum of squares or the residuals. The remaining unmatched term is just the negative inverse of the covariance matrix of unscaled by .

A subsequent sweep on the second rows and columns produces the matrix,

where , is now the inverse covariance matrix of and and is the matrix

Again, we can quickly check the results by comparing the output from `lm`

to our SWP operator.

A final sweep by the third row and column produces , the negative inverse of the unscaled covariance matrix of . This can be quickly verified through `R`

:

SWP(XX,1:3) + chol2inv(chol(XX))

## X_1 X_2 X_3 ## X_1 0.000000e+00 0.000000e+00 1.355253e-20 ## X_2 0.000000e+00 3.469447e-18 8.673617e-19 ## X_3 1.355253e-20 8.673617e-19 0.000000e+00

## Undo!

We can also undo sweeps with the reverse sweep (RSWP) operator through the following element-wise operations

Caution should be used when reverse sweeping, as in application is not necessarily equal to due to floating point error. In particular when the matrix is ill-conditioned there may be considerable departure between the calculated identity and the original matrix.

## What Next?

A few good references on the subject beyond (Dempster, 1969) can be found in (Goodknight, 1979) and (Little and Rubin, 2014). There are a few slight differences between these implementations, but all generally do the same thing.

One function that doesn’t do the same thing is the `sweep`

function in `R`

. This function can be used to write a SWP implementation, but doesn’t sweep by itself.

Code examples can be found in the `ISR3`

package on `CRAN`

and in lots of other packages.

Examples in `C`

can be found in `ISR3`

and as an internal function in the `MNP`

package (Imai and Van Dyk, 2013).

Enjoy your sweeping, next time I’ll go over sweeping with the generalized inverse!

## References

Dempster, A.P. (1969). *Elements of continuous multivariate analysis*. Reading, MA: Addison-Wesley.

Goodnight, J. H. (1979). A tutorial on the SWEEP operator. The American Statistician, 33(3), 149-158.

Imai, K., and Van Dyk, D. A. (2013). MNP: R Package for fitting the Multinomial Probit Model, R package version 2.6-4, https://CRAN.R-project.org/package=MNP

Lisic, J. J. (2016). ISR3: Iterative Sequential Regression, R package version 0.98, https://CRAN.R-project.org/package=ISR3

Little, R. J., and Rubin, D. B. (2014). *Statistical analysis with missing data*. John Wiley & Sons.

**leave a comment**for the author, please follow the link and comment on their blog:

**MeanMean**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.