It seems like neither regression.js nor simple-statistics.js support multiple linear regressions featuring 2+ independent variables. However, with tensorflow.js, it does seem possible to achieve building a multiple linear regression with a model that looks something like this:
import * as tf from '@tensorflow/tfjs';
// Sample data: let's assume X1, X2 are independent variables and Y is the dependent variable.
const X1 = [1, 2, 3, 4];
const X2 = [2, 2, 4, 3];
const Y = [2, 4, 6, 8];
// Convert data to tensors
const xs = tf.tensor2d([X1, X2], [4, 2]); // Shape is [num_samples, num_features]
const ys = tf.tensor1d(Y);
// Build a model
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [2]})); // inputShape is 2 because we have 2 independent variables
// Compile the model
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Train the model
(async () => {
await model.fit(xs, ys, { epochs: 100 });
model.predict(tf.tensor2d([[5, 5], [6, 7]], [2, 2])).print(); // Predicting for new data
})();
Am I missing something with regards to the first two libraries not supporting multiple linear regressions? Pulling our tensorflow.js seems overkill for this, but also seems like it may be the only option here (outside of doing our own matrix math using mathjs). Anyone else have experience model fitting multiple linear regression in javascript?