Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I added the option for residuals to be added to the prediction of the original points. #80

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Below are the default values for the configuration parameter.
{
order: 2,
precision: 2,
residuals: false
}
```

Expand Down
104 changes: 60 additions & 44 deletions dist/regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@
var DEFAULT_OPTIONS = { order: 2, precision: 2, period: null };

/**
* Determine the coefficient of determination (r^2) of a fit from the observations
* and predictions.
*
* @param {Array<Array<number>>} data - Pairs of observed x-y values
* @param {Array<Array<number>>} results - Pairs of observed predicted x-y values
*
* @return {number} - The r^2 value, or NaN if one cannot be calculated.
*/
* Determine the coefficient of determination (r^2) of a fit from the observations
* and predictions.
*
* @param {Array<Array<number>>} data - Pairs of observed x-y values
* @param {Array<Array<number>>} results - Pairs of observed predicted x-y values
*
* @return {number} - The r^2 value, or NaN if one cannot be calculated.
*/
function determinationCoefficient(data, results) {
var predictions = [];
var observations = [];
Expand Down Expand Up @@ -96,14 +96,14 @@
}

/**
* Determine the solution of a system of linear equations A * x = b using
* Gaussian elimination.
*
* @param {Array<Array<number>>} input - A 2-d matrix of data in row-major form [ A | b ]
* @param {number} order - How many degrees to solve for
*
* @return {Array<number>} - Vector of normalized solution coefficients matrix (x)
*/
* Determine the solution of a system of linear equations A * x = b using
* Gaussian elimination.
*
* @param {Array<Array<number>>} input - A 2-d matrix of data in row-major form [ A | b ]
* @param {number} order - How many degrees to solve for
*
* @return {Array<number>} - Vector of normalized solution coefficients matrix (x)
*/
function gaussianElimination(input, order) {
var matrix = input;
var n = input.length - 1;
Expand Down Expand Up @@ -143,25 +143,51 @@
}

/**
* Round a number to a precision, specificed in number of decimal places
*
* @param {number} number - The number to round
* @param {number} precision - The number of decimal places to round to:
* > 0 means decimals, < 0 means powers of 10
*
*
* @return {numbr} - The number, rounded
*/
* Round a number to a precision, specificed in number of decimal places
*
* @param {number} number - The number to round
* @param {number} precision - The number of decimal places to round to:
* > 0 means decimals, < 0 means powers of 10
*
*
* @return {numbr} - The number, rounded
*/
function round(number, precision) {
var factor = Math.pow(10, precision);
return Math.round(number * factor) / factor;
}

/**
* The set of all fitting methods
*
* @namespace
*/
* Predicts the points used for function calculation based on the predct function generated.
* @param {Array<Array<number>>} data - The original points input
* @param {function(number)} predict - The prediction function generated
* @param {bool} enableResiduals - Indicates whether residuals added to the response
*
* @return {predictedPoints} - Returns the predicted points.
*/
function predictPoints(data, predict, enableResiduals) {
var points = [];
if (enableResiduals === true) {
points = data.map(function (point) {
var predictedCoordinates = predict(point[0]);
return {
coordinates: predictedCoordinates,
residuals: point[1] - predictedCoordinates[1] // Residual = Actual - Predicted
};
});
} else {
points = data.map(function (point) {
return predict(point[0]);
});
}
return points;
}

/**
* The set of all fitting methods
*
* @namespace
*/
var methods = {
linear: function linear(data, options) {
var sum = [0, 0, 0, 0, 0];
Expand All @@ -187,9 +213,7 @@
return [round(x, options.precision), round(gradient * x + intercept, options.precision)];
};

var points = data.map(function (point) {
return predict(point[0]);
});
var points = predictPoints(data, predict, options.residuals);

return {
points: points,
Expand Down Expand Up @@ -222,9 +246,7 @@
return [round(x, options.precision), round(coeffA * Math.exp(coeffB * x), options.precision)];
};

var points = data.map(function (point) {
return predict(point[0]);
});
var points = predictPoints(data, predict, options.residuals);

return {
points: points,
Expand Down Expand Up @@ -255,9 +277,7 @@
return [round(x, options.precision), round(round(coeffA + coeffB * Math.log(x), options.precision), options.precision)];
};

var points = data.map(function (point) {
return predict(point[0]);
});
var points = predictPoints(data, predict, options.residuals);

return {
points: points,
Expand Down Expand Up @@ -289,9 +309,7 @@
return [round(x, options.precision), round(round(coeffA * Math.pow(x, coeffB), options.precision), options.precision)];
};

var points = data.map(function (point) {
return predict(point[0]);
});
var points = predictPoints(data, predict, options.residuals);

return {
points: points,
Expand Down Expand Up @@ -343,9 +361,7 @@
}, 0), options.precision)];
};

var points = data.map(function (point) {
return predict(point[0]);
});
var points = predictPoints(data, predict, options.residuals);

var string = 'y = ';
for (var _i = coefficients.length - 1; _i >= 0; _i--) {
Expand Down
2 changes: 1 addition & 1 deletion dist/regression.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading