R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored] > # sweepex.r J F Monahan September 2007 > # > # sweep operator -- function and example > # > A <- matrix( c(3,1,0,1,4,2,0,2,6), 3, 3) > # write it out > A [,1] [,2] [,3] [1,] 3 1 0 [2,] 1 4 2 [3,] 0 2 6 > # define function > regsweep <- function(A,k) { + d <- A[k,k] # sweep out row k, col k + A[k,] <- A[k,]/d + b <- A[,k] + b[k] <- 0 # don't change row k here + A <- A - outer(b,A[k,]) # main operation + A[,k] <- -b/d # fix col k + A[k,k] <- 1/d # diagonal element & done + regsweep <- A } > # > # write it out > A <- regsweep(A,2) # sweep second row/col > A [,1] [,2] [,3] [1,] 2.75 -0.25 -0.5 [2,] 0.25 0.25 0.5 [3,] -0.50 -0.50 5.0 > A <- regsweep(A,3) # sweep third row/col > A [,1] [,2] [,3] [1,] 2.7 -0.3 0.1 [2,] 0.3 0.3 -0.1 [3,] -0.1 -0.1 0.2 > A <- regsweep(A,2) # sweep second row/col > A [,1] [,2] [,3] [1,] 3 1.0000000 0.0000000 [2,] 1 3.3333333 -0.3333333 [3,] 0 0.3333333 0.1666667 > A <- regsweep(A,1) # sweep first row/col > A [,1] [,2] [,3] [1,] 0.3333333 0.3333333 0.0000000 [2,] -0.3333333 3.0000000 -0.3333333 [3,] 0.0000000 0.3333333 0.1666667 > rm(list=ls()) # clean up > > # regsweep.r J F Monahan September 2007 > # > # sweep operator -- function and example > # response vector > y <-c( 4.5, 5.5, 6.5, 8, 10, 12) > # design matrix -- intercept, linear, quadratic > X <- matrix(1,6,3) # get shape and intercept, fix later > X[,2] <- 1:6 > X[,3] <- (1:6)*(1:6) > # put X and y together for easy printing > Xy <- cbind(X,y) # put them together > Xy # write it out y [1,] 1 1 1 4.5 [2,] 1 2 4 5.5 [3,] 1 3 9 6.5 [4,] 1 4 16 8.0 [5,] 1 5 25 10.0 [6,] 1 6 36 12.0 > # > # construct inner product matrix > A <- crossprod(Xy,Xy) > # write it out > A y 6.0 21 91 46.50 21.0 91 441 189.00 91.0 441 2275 895.00 y 46.5 189 895 400.75 > # define function > regsweep <- function(A,k) { + d <- A[k,k] # sweep out row k, col k + A[k,] <- A[k,]/d + b <- A[,k] + b[k] <- 0 # don't change row k here + A <- A - outer(b,A[k,]) # main operation + A[,k] <- -b/d # fix col k + A[k,k] <- 1/d # diagonal element & done + regsweep <- A } > # > # sweep first row/col to get means, css for X and y > A <- regsweep(A,1) # sweep first row/col > A y 0.1666667 3.50 15.16667 7.750 -3.5000000 17.50 122.50000 26.250 -15.1666667 122.50 894.83333 189.750 y -7.7500000 26.25 189.75000 40.375 > A <- regsweep(A,2) # sweep linear row/col > A y 0.8666667 -0.20000000 -9.333333 2.5 -0.2000000 0.05714286 7.000000 1.5 9.3333333 -7.00000000 37.333333 6.0 y -2.5000000 -1.50000000 6.000000 1.0 > A <- regsweep(A,3) # sweep quadratic row/col > A y 3.20 -1.950000 0.25000000 4.00000000 -1.95 1.369643 -0.18750000 0.37500000 0.25 -0.187500 0.02678571 0.16071429 y -4.00 -0.375000 -0.16071429 0.03571429 > A <- regsweep(A,3) # second sweep restores > A y 0.8666667 -0.20000000 -9.333333 2.5 -0.2000000 0.05714286 7.000000 1.5 9.3333333 -7.00000000 37.333333 6.0 y -2.5000000 -1.50000000 6.000000 1.0