Can anyone give explanation how is .lm.fit() is faster than lm()

I am trying to understand the source code of these, something like below:

.lm.fit <- function(x, y, tol = 1e-07) .Call(C_Cdqrls, x, y, tol, check=TRUE)

Where does the functions points to? and why its faster than a defined lm()?

By Understanding this, I hope i can understand better when making a fitter functions, I am trying to make one for polr() version if able

That part is somewhat obvious from the source code you link. In a simplified way:

lm <- function(...){
  # check the arguments, redefine some arguments
  z <- lm.fit(...)
  # make nice output from z
}

lm.fit <- function(...)
  # check arguments etc
  z <- .Call(C_Cdqrls, x, y, tol, FALSE)
  # make nice output from z
}

.fm.fit <- function(...) .Call(C_Cdqrls, ...)

So, the function lm() has a lot of overhead, it does extra computations to ensure that it's easy to use, can catch mistakes early, etc. The actual computation is always done by C_Cdrls. But as a standard R user, you wouldn't want to call it directly, all these extra steps are useful: you don't care that much if your computation takes 0.7 s instead of 0.5 s, but you do care about wasting 10 minutes to write your input in the right format, or wasting half a day because you made a mistake and didn't get an error message.

As to the meaning of .Call(C_Cdqrls), you can check ?.Call, and see this is calling a function written in the C programming language. Most of R code ends up calling C functions if you dig deep enough. You can find the source code here, but be aware that C code can be hard to read if you're not already familiar with the language (there is a reason we're using R!)

I don't know polr, or why the existing functions of that name do not fit your needs, I would recommend trying to implement it in R first, and only consider using C or C++ if it's really too slow. I'd say the important takeaway is that there is a core that does the computation (can be written in R, C, C++ etc), and a "wrapper", that can detect mistakes, can accept a formula interface etc.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.