G2SWEEP: Generalized inverse matrix of type 2 for linear regression

View source: R/G2SWEEP.R

G2SWEEPR Documentation

Generalized inverse matrix of type 2 for linear regression

Description

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

Usage

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

Arguments

A

a matrix to be inverted. If A is not a square matrix, G2SWEEP calls the g2inv function.

Augmented

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 details. If the input matrix A is not a square matrix, the Augmented option cannot be TRUE.

eps

A value less than this is considered zero.

Details

The generalized inverse of g2-type is used by some software to do linear regression. See 'SAS Technical Report R106, The Sweep Operator: Its Importance in Statistical Computing' by J. H. Goodnight for details.

Value

when Augmented=FALSE

ordinary g2 inverse

when Augmented=TRUE

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

attribute "rank"

the rank of the input matrix

Author(s)

Kyun-Seop Bae k@acr.kr

See Also

lfit, ModelMatrix

Examples

  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 June 15, 2026, 9:07 a.m.