Skip to content

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,
  );

  // OR
  // final mlr1 = MultivariateLinearRegression()..fit(x, y);

  // OR
  // final mlr2 = MLR(
  //   x: x,
  //   y: y,
  // );

  // OR
  // final ml3 = MLR()..fit(x, 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

Constructor

MultivariateLinearRegression({
  List<List<double>>? x,
  List<List<double>>? y,
  bool intercept = true,
  bool statistics = true,
})

Creates a multivariate linear regression model.

  • x — Input feature matrix (rows = samples, columns = features)
  • y — Output matrix (rows = samples, columns = targets)
  • intercept — Includes a bias (intercept) term when set to true
  • statistics — Enables computation of additional metrics (standard errors, t-stats, etc.)

Load Existing Model

factory MultivariateLinearRegression.load(MultivariateLinearRegression model)

Reconstructs a trained model from previously trained model


Fitting

void fit({required List<List<double>> x, required List<List<double>> y})

Fits the model to the provided training data. Can be used to fit a new model or refit an existing model with new data.


Prediction

List<double> predict(List<double> input)

Returns predicted outputs for a single input vector.

List<List<double>> predictBatch(List<List<double>> inputs)

Returns predictions for multiple input rows.


Coefficients & Metrics

List<List<double>> get weights

Matrix of regression coefficients (includes intercept if enabled).

double get stdError

Overall standard error of the model.

List<List<double>> get stdErrors

Standard error for each coefficient.

List<List<double>> get tStats

T-statistics corresponding to each coefficient.

List<List<double>> get stdErrorMatrix

Covariance matrix of the coefficients.

Available only when statistics = true


Serialization

Map<String, dynamic> toJson()

Serializes the model into a JSON-compatible format, including statistics when enabled.


Contributing

Contributions are welcome. Before opening a pull request, please read the contributing guide:


Support

If you find this package useful, please consider supporting it:

Your support helps improve the project and keeps it actively maintained 😊

About

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

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages