R: Fast (Weighted) Linear Model Fitting

flm {collapse} | R Documentation

## Fast (Weighted) Linear Model Fitting

### Description

`flm` is a fast linear model command that takes matrices as input and (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.

### Usage

    flm(y, X, w = NULL, add.icpt = FALSE, return.raw = FALSE,
        method = c("lm", "solve", "qr", "arma", "chol", "eigen"),
        eigen.method = 3L, ...)

### 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`.

`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. |  | String |  | Description
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 but requires `crossprod(X)` to be positive definite).
6 |  | "eigen" |  | uses `RcppEigen::fastLmPure` (very fast but potentially unstable, depending on the method).
`eigen.method` | integer. Select the method of computation used by `RcppEigen::fastLmPure`:
Int. |  | Description
0 |  | column-pivoted QR decomposition.
1 |  | unpivoted QR decomposition.
2 |  | LLT Cholesky.
3 |  | LDLT Cholesky.
4 |  | Jacobi singular value decomposition (SVD).
5 |  | method 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. Sensible choices are `tol = value` - 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 LINPACK (the default)).

### Value

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

    coef <- flm(mtcars$mpg, qM(mtcars[c("hp","carb")]),
                mtcars$wt, add.icpt = TRUE)
    coef

    lmcoef <- coef(lm(mpg ~ hp + carb, weights = wt, mtcars))
    lmcoef

    all.equal(drop(coef), lmcoef)

    all_obj_equal(lapply(1:6, function(i)
      flm(mtcars$mpg, qM(mtcars[c("hp","carb")]),
          mtcars$wt, add.icpt = TRUE, method = i)))

[Package collapse version 1.7.6 Index]