Since you asked for it:
Here is a bare-bones implementation that omits the bells and whistles (and checks) of lm
. As a consequence it is faster, but you'd use it at your own risk, i.e., the warnings in help("lm.fit")
apply. Due to laziness, code for calculation of the F-stats was extracted from the summary.lm
source code and only slightly amended (so please consider licence()
and citation("stats")
).
fit1 <- lm.fit(cbind(1, x), y)
fstats <- function(obj) {
p <- obj$rank
rdf <- obj$df.residual
r <- obj$residuals
f <- obj$fitted.values
mss <- sum((f - mean(f))^2)
rss <- sum(r^2)
resvar <- rss/rdf
df.int <- 1L #assumes there is always an intercept
fstatistic <- c(value = (mss/(p - df.int))/resvar,
numdf = p - df.int, dendf = rdf)
fstatistic["pval"] <- pf(fstatistic[1L],
fstatistic[2L],
fstatistic[3L], lower.tail = FALSE)
fstatistic
}
fstats(fit1)
# value numdf dendf pval
#5.321048e+02 1.000000e+00 8.000000e+00 1.324022e-08