Skip to content

Multivariate linear regression for Dart with support for multiple outputs and optional intercept, implemented using Golub-Reinsch Singular Value Decomposition

License

Notifications You must be signed in to change notification settings

noahweasley/Multivariate-Linear-Regression

Repository files navigation

Multivariate Linear Regression

Style: very good analysis Test Coverage Powered by Mason License: MIT

Multivariate linear regression for Dart with support for multiple outputs and optional intercept, implemented using Golub-Reinsch Singular Value Decomposition.

Inspired by ml-matrix and regression-multivariate-linear Node.js libraries.


Installation

In order to start using Multivariate Linear Regression you must have the Dart SDK installed on your machine.

Install via dart pub add:

dart pub add multivariate_linear_regression

Usage

import 'package:multivariate_linear_regression/multivariate_linear_regression.dart';

void main() {
  final x = [
    [0.0, 0.0],
    [1.0, 2.0],
    [2.0, 3.0],
    [3.0, 4.0],
  ];

  final y = [
    [0.0, 0.0, 0.0],
    [2.0, 4.0, 3.0],
    [4.0, 6.0, 5.0],
    [6.0, 8.0, 7.0],
  ];

  final mlr = MultivariateLinearRegression(
    x: x,
    y: y,
  );

  print(mlr.predict([3.0, 3.0]));
  print(mlr.predictBatch([[1.0, 2.0], [2.0, 3.0]]));
  print(mlr.weights);
  print(mlr.stdError);
  print(mlr.stdErrors);
  print(mlr.tStats);
  print(mlr.toJson());
}

API Overview

  • MultivariateLinearRegression({x, y, intercept = true, statistics = true})

    • Creates a regression model.
    • x: List of input rows.
    • y: List of output rows.
    • intercept: Whether to include an intercept column.
    • statistics: Whether to compute variance, standard errors, and t-stats.
  • MultivariateLinearRegression.load(model)

    • Recreates a model using the original training data.
  • predict(List<double> x)

    • Predicts outputs for a single input vector.
  • predictBatch(List<List<double>> x)

    • Predicts outputs for multiple input rows.
  • weights

    • Returns the regression coefficients.
  • stdError

    • Standard error of the regression.
  • stdErrors

    • Standard error for each coefficient.
  • tStats

    • t-statistics for each coefficient.
  • stdErrorMatrix

    • Covariance matrix of coefficients (requires statistics = true).
  • toJson()

    • Converts the model to a JSON-compatible map, including regression statistics if enabled.

Continuous Integration

Multivariate Linear Regression comes with a built-in GitHub Actions workflow powered by Very Good Workflows.

On each pull request and push, the CI formats, lints, and tests the code. The project uses Very Good Analysis for a strict set of analysis rules. Code coverage is enforced using Very Good Coverage.

Note: The coverage is currently at 96.4% due to a few small utility paths in SVD computations that are difficult to trigger via unit tests.


Running Tests

To run all unit tests and generate coverage:

dart pub global activate coverage 1.15.0
dart test --coverage=coverage
dart pub global run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info

To view the coverage report using lcov:

genhtml coverage/lcov.info -o coverage/
open coverage/index.html

About

Multivariate linear regression for Dart with support for multiple outputs and optional intercept, implemented using Golub-Reinsch Singular Value Decomposition

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages