G2SWEEP: Generalized inverse matrix of type 2, g2 inverse

View source: R/G2SWEEP.R

G2SWEEPR Documentation

Generalized inverse matrix of type 2, g2 inverse


Generalized inverse is usually not unique. Some programs use this algorithm to get a unique generalized inverse matrix.


  G2SWEEP(A, Augmented=FALSE, eps=1e-08) 



a matrix to be inverted


If this is TRUE and A is a model(design) matrix X, the last column should be X'y, the last row y'X, and the last cell y'y. See the reference and example for the detail.


Less than this value is considered as zero.


Generalized inverse of g2-type is used by some softwares to do linear regression. See 'SAS Technical Report R106, The Sweep Operator: Its importance in Statistical Computing' by J. H. Goodnight for the detail.


when Augmented=FALSE

ordinary g2 inverse

when Augmented=TRUE

g2 inverse and beta hats in the last column and the last row, and sum of square error (SSE) in the last cell

attribute "rank"

the rank of input matrix


Kyun-Seop Bae k@acr.kr

See Also

lfit, ModelMatrix


  f1 = uptake ~ Type + Treatment # formula
  x = ModelMatrix(f1, CO2)  # Model matrix and relevant information
  y = model.frame(f1, CO2)[, 1] # observation vector
  nc = ncol(x$X) # number of columns of model matrix
  XpY = crossprod(x$X, y)
  aXpX = rbind(cbind(crossprod(x$X), XpY), cbind(t(XpY), crossprod(y)))
  ag2 = G2SWEEP(aXpX, Augmented=TRUE)
  b = ag2[1:nc, (nc + 1)] ; b # Beta hat
  iXpX = ag2[1:nc, 1:nc] ; iXpX # g2 inverse of X'X
  SSE = ag2[(nc + 1), (nc + 1)] ; SSE # Sum of Square Error
  DFr = nrow(x$X) - attr(ag2, "rank") ; DFr # Degree of freedom for the residual

# Compare the below with the above
  REG(f1, CO2)
  aov1(f1, CO2)

sasLM documentation built on Nov. 19, 2023, 5:12 p.m.