require(utils)set.seed(129)
n <- 7 ; p <- 2
X <- matrix(rnorm(n * p), n, p) # no intercept!
y <- rnorm(n)
w <- rnorm(n)^2
str(lmw <- lm.wfit(x = X, y = y, w = w))
str(lm. <- lm.fit (x = X, y = y))
## These are the same calculations at C level, but a parallel BLAS
## might not do them the same way twice, and if seems serial MKL does not.
lm.. <- .lm.fit(X,y)
lm.w <- .lm.fit(X*sqrt(w), y*sqrt(w))
id <- function(x, y) all.equal(x, y, tolerance = 1e-15, scale = 1)
stopifnot(id(unname(lm.$coef), lm..$coef),
id(unname(lmw$coef), lm.w$coef))
if(require("microbenchmark")) {
mb <- microbenchmark(lm(y~X), lm.fit(X,y), .lm.fit(X,y))
print(mb)
boxplot(mb, notch=TRUE)
}
Run the code above in your browser using DataLab