sweep.operator | R Documentation |
Perform the sweep operation (or reverse sweep) on the diagonal elements of a symmetric matrix.
sweep.operator(x, k = 1, reverse = FALSE)
x |
a symmetric matrix. |
k |
elements (if |
reverse |
logical. If |
The symmetric sweep operator is a powerful tool in computational statistics with uses in stepwise regression, conditional multivariate normal distributions, MANOVA, and more.
a square matrix of the same order as x
.
Goodnight, J.H. (1979). A tutorial on the SWEEP operator. The American Statistician 33, 149-158.
# tiny example of regression, last column contains 'y'
xy <- matrix(c(1, 1, 1, 1,
1, 2, 1, 3,
1, 3, 1, 3,
1, 1,-1, 2,
1, 2,-1, 2,
1, 3,-1, 1), ncol = 4, byrow = TRUE)
z <- crossprod(xy)
z <- sweep.operator(z, k = 1:3)
cf <- z[1:3,4] # regression coefficients
RSS <- z[4,4] # residual sum of squares
# an example not that small
x <- matrix(rnorm(1000 * 100), ncol = 100)
xx <- crossprod(x)
z <- sweep.operator(xx, k = 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.