1

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.

boshek
  • 4,100
  • 1
  • 31
  • 55
  • I think this is a duplicate indeed. Had never seen that before. Should I just delete my question or answer it using the duplicate? – boshek Jan 23 '17 at 18:22
  • Ok great. I made a mistake in my original post. I was also looking for a intercept calculation. I had been originally calculating that like this: `b = (sy-a*sx)/n`. I don't see a intercept parameter being calculated in your function. Any thoughts on that? Or maybe I should modify the question... – boshek Jan 23 '17 at 18:33
  • Please yes! That would be great. – boshek Jan 23 '17 at 18:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133833/discussion-between-boshek-and--zheyuan-li). – boshek Jan 23 '17 at 19:09

0 Answers0