Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a faster lm function

Tags:

r

lm

I would like to get the slope of a linear regression fit for 1M separate data sets (1M * 50 rows for data.frame, or 1M * 50 for array). Now I am using the lm() function, which takes a very long time (about 10 min).

Is there any faster function for linear regression?

like image 656
Bangyou Avatar asked Aug 21 '14 00:08

Bangyou


People also ask

What is the lm function?

The lm() function is used to fit linear models to data frames in the R Language. It can be used to carry out regression, single stratum analysis of variance, and analysis of covariance to predict the value corresponding to data that is not in the data frame.

How does R lm function work?

Linear Regression Example in R using lm() Function. Summary: R linear regression uses the lm() function to create a regression model given some formula, in the form of Y~X+X2. To look at the model, you use the summary() function. To analyze the residuals, you pull out the $resid variable from your new model.

What method does lm function use for regression?

lm uses the QR factorization method (a direct rather than iterative method) to solve linear least squares problems.

What is the lm formula?

LM Equation The LM equation calculates the demand for money, and the equation is represented here: L = k * Y - h * I. L = Demand for Real Money. k = Income Sensitivity of Demand for Real Money.


1 Answers

Yes there are:

  • R itself has lm.fit() which is more bare-bones: no formula notation, much simpler result set

  • several of our Rcpp-related packages have fastLm() implementations: RcppArmadillo, RcppEigen, RcppGSL.

We have described fastLm() in a number of blog posts and presentations. If you want it in the fastest way, do not use the formula interface: parsing the formula and preparing the model matrix takes more time than the actual regression.

That said, if you are regressing a single vector on a single vector you can simplify this as no matrix package is needed.

like image 109
Dirk Eddelbuettel Avatar answered Sep 20 '22 18:09

Dirk Eddelbuettel