flm function

Fast (Weighted) Linear Model Fitting

Fast (Weighted) Linear Model Fitting

flm is a fast linear model command that (by default) only returns a coefficient matrix. 6 different efficient fitting methods are implemented: 4 using base R linear algebra, and 2 utilizing the RcppArmadillo and RcppEigen packages. The function itself only has an overhead of 5-10 microseconds, and is thus well suited as a bootstrap workhorse.

flm(...) # Internal method dispatch: default if is.atomic(..1) ## Default S3 method: flm(y, X, w = NULL, add.icpt = FALSE, return.raw = FALSE, method = c("lm", "solve", "qr", "arma", "chol", "eigen"), eigen.method = 3L, ...) ## S3 method for class 'formula' flm(formula, data = NULL, weights = NULL, add.icpt = TRUE, ...)

Arguments

  • y: a response vector or matrix. Multiple dependent variables are only supported by methods "lm", "solve", "qr" and "chol".

  • X: a matrix of regressors.

  • w: a weight vector.

  • add.icpt: logical. TRUE adds an intercept column named '(Intercept)' to X.

  • formula: a lm formula, without factors, interaction terms or other operators (:, *, ^, -, etc.), may include regular transformations e.g. log(var), cbind(y1, y2), magrittr::multiply_by(var1, var2), magrittr::raise_to_power(var, 2).

  • data: a named list or data frame.

  • weights: a weights vector or expression that results in a vector when evaluated in the data environment.

  • return.raw: logical. TRUE returns the original output from the different methods. For 'lm', 'arma' and 'eigen', this includes additional statistics such as residuals, fitted values or standard errors. The other methods just return coefficients but in different formats.

  • method: an integer or character string specifying the method of computation:

    Int.StringDescription
    1"lm"uses .lm.fit .
    2"solve"solve(crossprod(X), crossprod(X, y)) .
    3"qr"qr.coef(qr(X), y) .
    4"arma"uses RcppArmadillo::fastLmPure .
    5"chol"chol2inv(chol(crossprod(X))) %*% crossprod(X, y) (quite fast, requires crossprod(X) to be positive definite i.e. problematic if multicollinearity).
    6"eigen"uses RcppEigen::fastLmPure (very fast but, depending on the method, also unstable if multicollinearity).
  • eigen.method: integer. Select the method of computation used by RcppEigen::fastLmPure:

    Int.Description
    0column-pivoted QR decomposition.
    1unpivoted QR decomposition.
    2LLT Cholesky.
    3LDLT Cholesky.
    4Jacobi singular value decomposition (SVD).
    5method based on the eigenvalue-eigenvector decomposition of X'X.

    See vignette("RcppEigen-Introduction", package = "RcppEigen") for details on these methods and benchmark results. Run source(system.file("examples", "lmBenchmark.R", package = "RcppEigen")) to re-run the benchmark on your machine.

  • ...: further arguments passed to other methods. For the formula method further arguments passed to the default method. Additional arguments can also be passed to the default method e.g. tol = value to set a numerical tolerance for the solution - applicable with methods "lm", "solve" and "qr" (default is 1e-7), or LAPACK = TRUE with method "qr" to use LAPACK routines to for the qr decomposition (typically faster than the LINPACK default).

Returns

If return.raw = FALSE, a matrix of coefficients with the rows corresponding to the columns of X, otherwise the raw results from the various methods are returned.

Note

Method "qr" supports sparse matrices, so for an X matrix with many dummy variables consider method "qr" passing as(X, "dgCMatrix") instead of just X.

See Also

fhdwithin/HDW, fFtest, Data Transformations , Collapse Overview

Examples

# Simple usage coef <- flm(mpg ~ hp + carb, mtcars, w = wt) # Same thing in programming usage flm(mtcars$mpg, qM(mtcars[c("hp","carb")]), mtcars$wt, add.icpt = TRUE) # Check this is correct lmcoef <- coef(lm(mpg ~ hp + carb, weights = wt, mtcars)) all.equal(drop(coef), lmcoef) # Multi-dependent variable (only some methods) flm(cbind(mpg, qsec) ~ hp + carb, mtcars, w = wt) # Returning raw results from solver: different for different methods flm(mpg ~ hp + carb, mtcars, return.raw = TRUE) flm(mpg ~ hp + carb, mtcars, method = "qr", return.raw = TRUE) # Test that all methods give the same result all_obj_equal(lapply(1:6, function(i) flm(mpg ~ hp + carb, mtcars, w = wt, method = i)))
  • Maintainer: Sebastian Krantz
  • License: GPL (>= 2) | file LICENSE
  • Last published: 2025-03-10