Learn R Programming

heplots (version 1.0-2)

gsorth: Orthogonalize successive columns of a data frame or matrix

Description

gsorth uses sequential, orthogonal projections, as in the Gram-Schmidt method, to transform a matrix or numeric columns of a data frame into an uncorrelated set, possibly retaining the same column means and standard deviations as the original.

Usage

gsorth(y, order, recenter = TRUE, rescale = TRUE, adjnames = TRUE)

Arguments

y
A numeric data frame or matrix
order
An integer vector specifying the order of and/or a subset of the columns of y to be orthogonalized. If missing, order=1:p where p=ncol(y).
recenter
If TRUE, the result has same column means as original; else means = 0 for cols 2:p.
rescale
If TRUE, the result has same column standard deviations as original; else sd = residual variance for cols 2:p
adjnames
If TRUE, the column names of the result are adjusted to the form Y1, Y2.1, Y3.12, by adding the suffixes '.1', '.12', etc. to the original column names.

Value

  • Returns a matrix or data frame with uncorrelated columns. Row and column names are copied to the result.

Details

The method is equivalent to setting each of columns 2:p to the residuals from a linear regression of that column on all prior columns, i.e., z[,j] <- resid( lm( z[,j] ~ as.matrix(z[,1:(j-1)]), data=z) ) However, for accuracy and speed the transformation is carried out using the QR decomposition.

See Also

qr,

Examples

Run this code
GSiris <- gsorth(iris[,1:4])
GSiris <- gsorth(iris, order=1:4)   # same, using order
str(GSiris)
zapsmall(cor(GSiris))
colMeans(GSiris)
# sd(GSiris) -- sd(<matrix>) now deprecated
apply(GSiris, 2, sd)

# orthogonalize Y side
GSiris <- data.frame(gsorth(iris[,1:4]), Species=iris$Species)
iris.mod1 <- lm(as.matrix(GSiris[,1:4]) ~ Species, data=GSiris)
Anova(iris.mod1)

# orthogonalize X side
rohwer.mod <- lm(cbind(SAT, PPVT, Raven) ~ n + s + ns + na + ss, data=Rohwer)
Anova(rohwer.mod)

# type I tests for Rohwer data
Rohwer.orth <- cbind(Rohwer[,1:5], gsorth(Rohwer[, c("n", "s", "ns", "na", "ss")], adjnames=FALSE))

rohwer.mod1 <- lm(cbind(SAT, PPVT, Raven) ~ n + s + ns + na + ss, data=Rohwer.orth)
Anova(rohwer.mod1)
# compare with anova()
anova(rohwer.mod1)

Run the code above in your browser using DataLab