Enter your data points below. You need at least two points.
Predict Y for a new X
Results
Slope (m): N/A
Y-Intercept (b): N/A
Equation: y = N/A * x + N/A
Predicted Y for X=N/A: N/A
Understanding Linear Regression
Linear regression is a fundamental statistical method used to model the relationship between a dependent variable (Y) and one or more independent variables (X). In its simplest form, called simple linear regression, we aim to find the best-fitting straight line through a set of data points, representing the relationship between a single independent variable (X) and a dependent variable (Y).
The Equation of a Line
The goal of linear regression is to find the parameters of the linear equation that best describes the data. The standard equation of a straight line is:
y = mx + b
y is the dependent variable (what we want to predict).
x is the independent variable (the predictor).
m is the slope of the line, representing the change in y for a one-unit change in x.
b is the y-intercept, representing the value of y when x is zero.
How the Calculator Works (The Math)
This calculator uses the method of least squares to determine the values of m (slope) and b (y-intercept) that minimize the sum of the squared differences between the observed y values and the predicted y values from the line.
Given a set of n data points (x1, y1), (x2, y2), …, (xn, yn):
1. Calculate the Means:
x̄ = (Σxᵢ) / n (Mean of X values)
ȳ = (Σyᵢ) / n (Mean of Y values)
2. Calculate the Slope (m):
The formula for the slope m is:
m = Σ[(xᵢ - x̄)(yᵢ - ȳ)] / Σ[(xᵢ - x̄)²]
Alternatively, a more computationally friendly form is:
m = [n(Σxᵢyᵢ) - (Σxᵢ)(Σyᵢ)] / [n(Σxᵢ²) - (Σxᵢ)²]
3. Calculate the Y-Intercept (b):
Once the slope m is known, the y-intercept b can be calculated using the means:
b = ȳ - m * x̄
4. Prediction:
After calculating m and b, you can predict the value of y for any new value of x using the equation: y_predicted = m * x_new + b.
Use Cases for Linear Regression
Economics: Predicting stock prices based on historical data, forecasting sales based on advertising spend.
Finance: Analyzing the relationship between interest rates and loan defaults, estimating asset returns.
Science: Modeling the relationship between temperature and ice cream sales, predicting crop yield based on rainfall.
Machine Learning: As a foundational algorithm for more complex predictive models.
Social Sciences: Studying the correlation between education level and income.
b = ȳ – m * x̄ = 3.67 – (1.5 * 2) = 3.67 – 3 = 0.67
Regression Equation:
y = 1.5x + 0.67
Prediction:
If we want to predict Y for X = 4:
y_predicted = 1.5 * 4 + 0.67 = 6 + 0.67 = 6.67
var dataPointCount = 2;
function addMoreDataPoints() {
dataPointCount++;
var container = document.getElementById('dataPointsContainer');
var newPointDiv = document.createElement('div');
newPointDiv.className = 'data-point-input';
newPointDiv.id = 'dataPoint' + dataPointCount;
newPointDiv.innerHTML = `
`;
container.appendChild(newPointDiv);
}
function calculateLinearRegression() {
var xValues = [];
var yValues = [];
var predictXValue = parseFloat(document.getElementById('predictX').value);
var hasInvalidInput = false;
for (var i = 1; i <= dataPointCount; i++) {
var xInput = document.getElementById('x' + i);
var yInput = document.getElementById('y' + i);
var x = parseFloat(xInput.value);
var y = parseFloat(yInput.value);
if (isNaN(x) || isNaN(y)) {
hasInvalidInput = true;
break;
}
xValues.push(x);
yValues.push(y);
}
if (hasInvalidInput || xValues.length < 2) {
document.getElementById('calculationResult').innerHTML = "Please enter valid numbers for at least two X and Y data points.";
document.getElementById('predictionResult').innerHTML = "";
return;
}
var n = xValues.length;
var sumX = 0;
var sumY = 0;
var sumXY = 0;
var sumXX = 0;
for (var i = 0; i < n; i++) {
sumX += xValues[i];
sumY += yValues[i];
sumXY += xValues[i] * yValues[i];
sumXX += xValues[i] * xValues[i];
}
var slopeNumerator = n * sumXY – sumX * sumY;
var slopeDenominator = n * sumXX – sumX * sumX;
var slope = 0;
var yIntercept = 0;
var slopeStr = "N/A";
var interceptStr = "N/A";
var equationStr = "y = N/A * x + N/A";
if (slopeDenominator !== 0) {
slope = slopeNumerator / slopeDenominator;
yIntercept = (sumY – slope * sumX) / n;
slopeStr = slope.toFixed(4);
interceptStr = yIntercept.toFixed(4);
equationStr = `y = ${slope.toFixed(4)} * x + ${yIntercept.toFixed(4)}`;
} else {
// Handle case where all X values are the same
// In this scenario, slope is undefined or infinite if Ys differ,
// or it's a vertical line. For simplicity, we can indicate this.
// If all Ys are also the same, it's a horizontal line (slope 0).
var allYSame = true;
for(var i = 1; i < yValues.length; i++) {
if (yValues[i] !== yValues[0]) {
allYSame = false;
break;
}
}
if (allYSame) {
slope = 0;
yIntercept = yValues[0];
slopeStr = "0.0000";
interceptStr = yIntercept.toFixed(4);
equationStr = `y = 0.0000 * x + ${yIntercept.toFixed(4)}`;
} else {
// Cannot form a standard linear regression line if X are same and Y differ
slopeStr = "Undefined (all X same)";
interceptStr = "N/A";
equationStr = "Cannot determine unique line.";
}
}
var resultHTML = `Slope (m): ${slopeStr}
Y-Intercept (b): ${interceptStr}
Equation: ${equationStr}`;
document.getElementById('calculationResult').innerHTML = resultHTML;
var predictionStr = "N/A";
if (!isNaN(predictXValue) && slopeDenominator !== 0) {
var predictedY = slope * predictXValue + yIntercept;
predictionStr = predictedY.toFixed(4);
document.getElementById('predictedXValue').textContent = predictXValue;
document.getElementById('predictionResult').innerHTML = `Predicted Y for X=${predictXValue}: ${predictionStr}`;
} else if (!isNaN(predictXValue) && slopeDenominator === 0 && slope === 0) { // Horizontal line case
var predictedY = yIntercept; // For horizontal line, y is constant
predictionStr = predictedY.toFixed(4);
document.getElementById('predictedXValue').textContent = predictXValue;
document.getElementById('predictionResult').innerHTML = `Predicted Y for X=${predictXValue}: ${predictionStr}`;
}
else {
document.getElementById('predictedXValue').textContent = predictXValue;
document.getElementById('predictionResult').innerHTML = `Predicted Y for X=${predictXValue}: N/A`;
if (isNaN(predictXValue)) {
document.getElementById('predictionResult').innerHTML += "Please enter a valid number for 'New X Value' to get a prediction.";
} else if (slopeDenominator === 0) {
document.getElementById('predictionResult').innerHTML += "Prediction unavailable due to undefined slope.";
}
}
}