Rust library for solving linear, logistic, and generalized linear models through
iteratively reweighted least squares, using the
ndarray-linalg
module.
This package is in early alpha and the interface is likely to undergo many changes. Functionality may change from one release to the next.
The regression algorithm uses iteratively re-weighted least squares (IRLS) with a step-halving procedure applied when the next iteration of guesses does not increase the likelihood.
At the moment the master branch of ndarray-linalg
along with blas-src = 0.6
is required to pass CI, but this precludes publishing new versions of this
crate. For the time being you may see some version mismatches in the
instructions below as a result.
fortran and BLAS must be installed:
sudo apt update && sudo apt install gfortran libblas-dev
To use the OpenBLAS backend, install also libopenblas-dev
and use this crate with the "openblas-src" feature.
To use in your crate, add the following to the Cargo.toml
:
ndarray = { version = "0.13", features = ["blas"]}
blas-src = { version = "0.4", default-features = false, features = ["openblas"] }
ndarray-glm = { version = "0.0.3", features = ["openblas-static"] }
An example for linear regression is shown below.
``` rust use ndarray::array; use ndarray_glm::{linear::Linear, model::ModelBuilder, standardize::standardize};
// define some test data
let datay = array![0.3, 1.3, 0.7];
let datax = array![[0.1, 0.2], [-0.4, 0.1], [0.2, 0.4]];
// The design matrix can optionally be standardized, where the mean of each independent
// variable is subtracted and each is then divided by the standard deviation of that variable.
let datax = standardize(datax);
// The model is generic over floating point type for the independent data variables, and
// the type will be inferred from the type of the arrays passed to data().
// L2 regularization can be applied with l2reg().
let model = ModelBuilder::
For logistic regression, the y
array data must be boolean, and for Poisson
regression it must be an unsigned integer.
Custom non-canonical link functions can be defined by the user, although the
interface is not particularly ergonomic. See tests/custom_link.rs
for examples.
The author's notes on generalized linear models summarize many of the relevant concepts and provide some additional references.