I am trying to figure out a way to calculate the slope and intercept in R where I don't have to use lm()
. The reason that I don't want to use lm()
is that it calculates many more parameters than I need for my purposes and therefore takes much longer than my approach. This is illustrate here:
## Data
set.seed(10)
x <- seq(1,1000000,by=0.15)
y <- rnorm(length(x))
## Using lm()
## Time it
start <- Sys.time()
lmResult <- lm(y ~ x)
a_lm = as.numeric(coef(lmResult)["x"])
Sys.time()-start
On my machine that takes about 10 secs. If I calculate the slope (a_hand
) by manually calculating the sum of squares like this I realize significant time savings - about 50 times:
## By hand
## Time it
start <- Sys.time()
sxx = sum(x^2)
sxy = sum(x*y)
sy = sum(y)
sx = sum(x)
n = length(x)
a_hand = (n*sxy-sy*sx)/(n*sxx-sx*sx)
Sys.time()-start
However, I am wondering if anyone can recommend a way realize ever more time savings. Specifically I am wondering if there is a lighter weight function in base R that I can use to calculate the coefficients without also calculating all the other parameters that lm()
calculates. I have found anything so the answer might just be no.