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

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

eps

Less than this value is considered as zero.

Details

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.

Value

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

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 Oct. 2, 2024, 9:06 a.m.