ZestCalc

Linear Regression Calculator

Fit a linear model to your own data: exact coefficients from the normal equations, predictions, residuals, MSE, and R², with optional ridge regularization and a line you can drag points under.

Data

One sample per row. The last column is the target the model predicts; the others are features, up to 6. Type, paste from a spreadsheet or CSV, or start from an example.

Features3
Target
8 samples

Model

Updates as you type.

8 samples, 2 features, and an intercept.

y^=84.17413+2.361416 size (m²)−1.958036 age (years)\hat{y} = 84.17413 + 2.361416\,\text{size (m²)} - 1.958036\,\text{age (years)}
Off

Ridge adds λ times the sum of the squared coefficients to the error, which pulls every coefficient toward 0. The intercept is left alone.

Coefficients

Intercept
≈84.174
size (m²)
≈2.3614
age (years)
≈-1.958

Each coefficient is how much the prediction changes when that feature goes up by 1 and the others stay the same.

How well it fits

Mean squared error
MSE=12.70162\text{MSE} = 12.70162
Root mean squared error
RMSE≈3.5639\text{RMSE} \approx 3.5639
R²
R2=0.998611R^2 = 0.998611

The model explains 99.9% of the variation in price (k$).

Predict

Enter feature values for a new sample.

y^=143.503869\hat{y} = 143.503869

Predictions and residuals

#yŷy − ŷ
1150≈143.5≈6.4961
2195≈198.51≈-3.5055
3220≈224.14≈-4.1365
4290≈288.93≈1.0717
5310≈314.56≈-4.5594
6360≈357.75≈2.2461
7400≈399.11≈0.89186
8440≈438.5≈1.4957

Coefficients are exact in fractions view. Decimals are rounded to 6 places.

A model is a matrix times a vector

Linear regression predicts a target from features with one weight per feature plus an intercept. Stack the samples as rows of XX, put a column of ones in front for the intercept, and every prediction at once is one product:

y^=Xβ\hat{y} = X\beta

The best β\beta makes the residuals y−y^y - \hat{y} as small as possible in total squared error. Setting the derivative of that error to zero gives the normal equations:

XTX β=XTyX^{T}X\,\beta = X^{T}y

That's a small square system, one row and column per coefficient, however many samples there are. This calculator solves it exactly with row reduction, so the coefficients come out as fractions when the data are whole numbers. Geometrically, XβX\beta is the projection of yy onto the column space of XX, which the Least Squares Calculator draws.

Reading the result

  • Coefficients. Each one is how much the prediction moves when its feature goes up by 1 and every other feature stays put. The units matter: a coefficient on size in square meters and one on age in years aren't comparable until the features are on the same scale.
  • MSE and RMSE. The mean squared error averages the squared residuals. Its square root, RMSE, is in the target's own units, so it reads as a typical miss.
  • R². One minus the squared error left over, divided by the target's total variation around its mean. R2=0.9R^2 = 0.9 means the model accounts for 90% of the variation. Adding features never lowers R² on the data you fit, so a high value alone doesn't mean the model will predict new data well.

When features repeat each other, and what ridge does

If one feature is an exact combination of others, like height in centimeters and height in decimeters, XTXX^{T}X is singular. Infinitely many coefficient sets then fit equally well, and the "Repeated feature" example shows this. The Matrix Inverse Calculator explains why a singular matrix has no inverse.

Ridge regression adds λ\lambda to the diagonal of XTXX^{T}X, everywhere except the intercept's entry:

(XTX+λD) β=XTy(X^{T}X + \lambda D)\,\beta = X^{T}y

That's the same as adding λ∑βj2\lambda \sum \beta_j^2 to the error. For any λ>0\lambda > 0 the system has exactly one solution, and every coefficient is pulled toward zero. In machine learning this is L2 regularization, or weight decay. It trades a little error on the training data for a model that's steadier on new data. Ridge depends on the units of each feature, which is why features are usually standardized first.