#### example 1.
n <- 100
k <- 5
x <- round(matrix(rnorm(n*k),n,k),digits=4)
y <- rnorm(n)
# if an optimized BLAS is not installed, depending on processor type, cp() may be
# faster than crossprod() for large matrices.
a1 <- crossprod(x)
a2 <- cp(x,,row.chunk = 50)
all.equal(a1, a2)
#### example 2.1.
x[,2] <- x[,1] + 2*x[,3] # x has rank 9
# estimation by least squares
A <- function(){
A1 <- control(crossprod(x))
ok <- A1$pivot[1:A1$rank]
as.vector(solve(A1$XTX,crossprod(x[,ok],y)))
}
# estimation by QR decomposition
B <- function(){
B1 <- qr(x)
qr.solve(x[,B1$pivot[1:B1$rank]],y)
}
a <- A()
b <- B()
all.equal(a,b)
### example 3.
n <- 1000
fat1 <- gl(20,50)
y <- rnorm(n)
da <- data.frame(y,fat1)
m <- model.matrix(y ~ factor(fat1),data = da)
is.sparse(m)
Run the code above in your browser using DataLab