Analyze the relationship between two sets of data points and determine the best-fit line equation.
Calculation Results
Regression Equation
Correlation Coefficient (r)
Coefficient of Determination (r²)
Slope (m)
Predict Y from X
Understanding Linear Regression
Linear regression is a statistical method used to model the relationship between a dependent variable (Y) and one or more independent variables (X). In its simplest form, simple linear regression, we aim to find a straight line that best fits the data points provided.
The Regression Formula
The equation for the regression line is expressed as:
ŷ = b + mx
ŷ (y-hat): The predicted value of Y.
m (Slope): The change in Y for every one-unit change in X.
b (Intercept): The value of Y when X is zero.
r (Correlation): Indicates the strength and direction of the linear relationship (ranging from -1 to 1).
Practical Example
Imagine you are studying the relationship between Hours Studied (X) and Exam Scores (Y). You have the following data:
Hours Studied (X)
Exam Score (Y)
2
65
4
78
6
88
8
92
Using the regression calculator, you might find an equation like ŷ = 58.5 + 4.5x. This suggests that for every hour studied, the score increases by approximately 4.5 points, and even with 0 hours of study, the base score is predicted to be 58.5.
Interpreting the Correlation Coefficient (r)
The value of r helps determine how reliable your model is:
Close to 1: Strong positive relationship (as X goes up, Y goes up).
Close to -1: Strong negative relationship (as X goes up, Y goes down).
Close to 0: No linear relationship between the variables.
var globalSlope = 0;
var globalIntercept = 0;
function calculateRegression() {
var xInput = document.getElementById('xValues').value;
var yInput = document.getElementById('yValues').value;
var errorDiv = document.getElementById('regression-error');
var resultsDiv = document.getElementById('regression-results');
errorDiv.style.display = 'none';
resultsDiv.style.display = 'none';
// Parse inputs
var xArr = xInput.split(',').map(function(item) { return parseFloat(item.trim()); });
var yArr = yInput.split(',').map(function(item) { return parseFloat(item.trim()); });
// Validation
if (xArr.some(isNaN) || yArr.some(isNaN)) {
errorDiv.innerHTML = "Error: Please ensure all values are valid numbers separated by commas.";
errorDiv.style.display = 'block';
return;
}
if (xArr.length !== yArr.length) {
errorDiv.innerHTML = "Error: The number of X values must match the number of Y values. (X: " + xArr.length + ", Y: " + yArr.length + ")";
errorDiv.style.display = 'block';
return;
}
if (xArr.length < 2) {
errorDiv.innerHTML = "Error: Please provide at least two data points.";
errorDiv.style.display = 'block';
return;
}
var n = xArr.length;
var sumX = 0;
var sumY = 0;
var sumXY = 0;
var sumX2 = 0;
var sumY2 = 0;
for (var i = 0; i < n; i++) {
sumX += xArr[i];
sumY += yArr[i];
sumXY += (xArr[i] * yArr[i]);
sumX2 += (xArr[i] * xArr[i]);
sumY2 += (yArr[i] * yArr[i]);
}
// Formulas for Linear Regression
// m = (n*sumXY – sumX*sumY) / (n*sumX2 – sumX^2)
// b = (sumY – m*sumX) / n
var denominator = (n * sumX2) – (sumX * sumX);
if (denominator === 0) {
errorDiv.innerHTML = "Error: Vertical line detected. Slope is undefined.";
errorDiv.style.display = 'block';
return;
}
var m = (n * sumXY – sumX * sumY) / denominator;
var b = (sumY – m * sumX) / n;
globalSlope = m;
globalIntercept = b;
// Correlation Coefficient (r)
var rNumerator = (n * sumXY) – (sumX * sumY);
var rDenominator = Math.sqrt(((n * sumX2) – (sumX * sumX)) * ((n * sumY2) – (sumY * sumY)));
var r = rNumerator / rDenominator;
var r2 = r * r;
// Display outputs
document.getElementById('slope-output').innerText = m.toFixed(4);
document.getElementById('equation-output').innerText = "y = " + b.toFixed(2) + " + " + m.toFixed(2) + "x";
document.getElementById('correlation-output').innerText = isNaN(r) ? "N/A" : r.toFixed(4);
document.getElementById('determination-output').innerText = isNaN(r2) ? "N/A" : r2.toFixed(4);
resultsDiv.style.display = 'block';
}
function predictYValue() {
var px = parseFloat(document.getElementById('predictX').value);
var output = document.getElementById('prediction-output');
if (isNaN(px)) {
output.innerText = "Enter X";
return;
}
var py = globalIntercept + (globalSlope * px);
output.innerText = "Y = " + py.toFixed(4);
}