Multiple Linear Regression
The Math
Definition can be found here.
In simple terms MLR can described as
"The case of one explanatory variable is called simple linear regression; for more than one, the process is called multiple linear regression."
The formula / model for MLR is
- \(y\), is the predicted values
- \(\beta_0\), is the y-intercept
- \(\beta_1x_1\), is the first regression coefficient (\(\beta_1\)) of the first independent variable (\(X_1\))
- \(\dots\), do the same for all independent variables.
- \(\beta_nx_n\), is the regression coefficient of the last independent variable
- \(\epsilon\), is the model error.
The same formula in Matrix form can be represented as
Our goal is to find \(\beta\) (coefficients / parameters) and to minimize \(\epsilon^2\). The methods used to do so is called the Least Squared Method.
Now, the least squared estimator for the given model is
The proof for this derivation can be found here.
The Code
Here is an example of training the Multiple Linear Regression Model with an example data.
Data
When fitting the MLinearRegressionModel using the function mlinregress_fit, the \(X\) matrix must be
The \(1^{st}\) column are the values of \(x_1\) and \(2^{nd}\) columns are the values of \(x_2\).
The \(Y\) matrix will look like the following
In code
#include <stdio.h>
#include "ds/array.h"
#include "ml/multiregress.h"
int main(void) {
float x_vals[] = {
1, 1,
1, 2,
2, 2,
0, 1};
float y_vals[] = {
3.25,
6.5,
3.5,
5.0};
Mat *x_mat = mat_create_from_array(x_vals, 4, 2);
Mat *y_mat = mat_create_from_array(y_vals, 4, 1);
MLinearRegressionModel *model = mlinregress_init();
mlinregress_fit(model, x_mat, y_mat);
printf("coefficients:\n");
mat_print(model->coefs);
printf("intercept: %f\n", model->intercept);
// Array *arr = arr_create(2);
// ARR_AT(arr, 0) = 4;
// ARR_AT(arr, 1) = 3;
printf("predicted: %f\n", mlinregress_predict(model, (float[]){4, 3}, 2));
// printf("predicted: %f\n", mlinregress_predict_arr(model, arr));
mat_free_no_array(x_mat);
mat_free_no_array(y_mat);
mlinregress_free(model);
}
Run the code, instructions here
coefficients:
-2.38
3.25
intercept: 2.062500
predicted: 2.312500