Linear Regression: Fit a Least-Squares Line and Interpret Its Coefficients
Fit a least-squares line by hand, interpret both coefficients in context, and carry the same example through residuals, SSE, R-squared, diagnostics and self-checks.
KnowledgeGate Team
Exam prep & CS education

Linear regression formulas are short, but slope, intercept, prediction and residual are easy to mix up. A sign error can make an above-line point look below the line, while a careless interpretation can turn association into a false causal claim.
For the observations (1,2), (2,3), (3,5), and (4,4), the least-squares line is y-hat = 1.5 + 0.8x. Its residuals give SSE = 1.8 and R^2 = 0.64. These values describe association and fit, not causation or guaranteed performance on new data.
1. Linear regression equation: data, predictions and residuals
Simple linear regression writes the fitted line as
y-hat = b_0 + b_1 x.
Here, x is the explanatory variable, y is the observed response, and y-hat is the response predicted by the line. For our small teaching example, x represents weekly study hours and y represents a quiz score out of 10. That context helps us interpret units, but it does not establish causation.
For observation i, the residual is
e_i = y_i - y-hat_i.
A positive residual means the observation lies above the line, so the model underpredicts it. A negative residual means it lies below the line, so the model overpredicts it. A zero residual means prediction and observation match.
Least squares chooses b_0 and b_1 to minimise the sum of squared vertical residuals:
SSE = sum_i (y_i - y-hat_i)^2.
The fitted line summarises a pattern. It does not require every observation to sit on the line.
2. Least-squares line: fit the four-point dataset step by step
Use the observations (1,2), (2,3), (3,5), and (4,4). Their raw summaries are
n = 4, sum x = 10, sum y = 14, sum x^2 = 30, sum xy = 39.
Therefore, x-bar = 10/4 = 2.5 and y-bar = 14/4 = 3.5. The centred calculation is:
x | y | x-x-bar | y-y-bar | Product | (x-x-bar)^2 |
|---|---|---|---|---|---|
1 | 2 | -1.5 | -1.5 | 2.25 | 2.25 |
2 | 3 | -0.5 | -0.5 | 0.25 | 0.25 |
3 | 5 | 0.5 | 1.5 | 0.75 | 0.25 |
4 | 4 | 1.5 | 0.5 | 0.75 | 2.25 |
Total | 4.00 | 5.00 |
Thus S_xy = 4 and S_xx = 5. If centred quantities feel unfamiliar, Linear Algebra for GATE CS can strengthen the underlying algebra, though it is not a regression lesson.
Now calculate the slope and intercept:
b_1 = S_xy/S_xx = 4/5 = 0.8.
b_0 = y-bar - b_1(x-bar) = 3.5 - (0.8)(2.5) = 1.5.
The fitted line is y-hat = 1.5 + 0.8x. At x=2.5, it gives 1.5 + 0.8(2.5) = 3.5, confirming that the least-squares line passes through (x-bar, y-bar) = (2.5, 3.5).
This example uses the closed-form least-squares solution. Gradient Descent for Linear Regression derives iterative optimisation for the same model and runs one parameter update, while this calculation stays with the exact coefficient solution.

3. Linear regression coefficients: interpret slope and intercept in context
The slope b_1 = 0.8 has direction and units. Within this observed sample, one additional weekly study hour is associated with a predicted increase of 0.8 quiz-score points. Four observational points do not prove that the extra hour caused the increase.
The intercept b_0 = 1.5 is the predicted quiz score when x=0. Our observed range is 1 to 4 hours, so zero is outside the sample. The intercept is necessary for the equation but has weak practical meaning here.
Changing units changes coefficient numbers, not predictions. If z = 2x counts half-hours, then x = z/2, so y-hat = 1.5 + 0.4z. The slope becomes 0.4 points per half-hour. If c = x - 2.5 centres hours at their mean, then y-hat = 3.5 + 0.8c. Centring keeps the slope but makes the intercept the prediction at the mean.
4. Linear regression predictions, residuals and R-squared
For an interpolation at x=3.5,
y-hat = 1.5 + 0.8(3.5) = 4.3.
At the observed point x=3, the prediction is 3.9, so e = 5 - 3.9 = +1.1. The model underpredicts that observation by 1.1 points.
x | y | y-hat | e = y - y-hat | e^2 |
|---|---|---|---|---|
1 | 2 | 2.3 | -0.3 | 0.09 |
2 | 3 | 3.1 | -0.1 | 0.01 |
3 | 5 | 3.9 | 1.1 | 1.21 |
4 | 4 | 4.7 | -0.7 | 0.49 |
The residuals sum to -0.3 - 0.1 + 1.1 - 0.7 = 0, a property of ordinary least squares fitted with an intercept. Their squared values give SSE = 0.09 + 0.01 + 1.21 + 0.49 = 1.80.
Total response variation around the mean is
TSS = sum_i (y_i - 3.5)^2 = 2.25 + 0.25 + 2.25 + 0.25 = 5.
Therefore,
R^2 = 1 - SSE/TSS = 1 - 1.8/5 = 0.64.
In these four observations, 64% of the response variation is accounted for by this fitted linear relationship. It is not 64% accuracy, evidence of causation, or a guarantee for new data. Probability for GATE CS is a useful route for strengthening the probability foundations behind variation and uncertainty.
5. Linear regression diagnostics: inspect residuals before trusting the line
Read the residuals against a horizontal zero line: -0.3,-0.1,+1.1,-0.7. The largest miss is +1.1 at x=3, but four observations are too few to establish a reliable diagnostic pattern.
With more data, a residual plot can reveal three useful shapes:
A systematic curve can suggest a missed nonlinear relationship.
A widening funnel can suggest changing residual spread.
One isolated large residual can mark an unusual or influential case worth checking.
These are investigation cues, not automatic reasons to delete observations.
Extrapolation needs similar caution. At x=10, the equation gives y-hat = 1.5 + 0.8(10) = 9.5. The arithmetic is valid, but 10 hours is far outside the observed range of 1 to 4, so the prediction has weak support. More complex models may come later, but a linear baseline and its residual checks should come first.

6. Linear regression mistakes that produce a plausible but wrong answer
Mixing raw and centred formulas. The ratio sum xy/sum x^2 = 39/30 = 1.3 is not the slope when an intercept is included. Use S_xy/S_xx = 4/5 = 0.8, or the equivalent raw-summary formula below.
Reversing the residual. Keep e = y - y-hat. The point (3,5) has residual +1.1, not -1.1. Reversing the convention flips every above-line and below-line interpretation.
Overreading the result. Do not call the slope causal, assume the intercept is meaningful, or call R^2 = 0.64 an accuracy percentage. Keep full precision until residuals and SSE are complete.
7. Linear regression question patterns and fast self-checks
The common raw-summary form produces the same slope:
b_1 = [n(sum xy) - (sum x)(sum y)]/[n(sum x^2) - (sum x)^2]
= [4(39) - 10(14)]/[4(30) - 10^2]
= 16/20 = 0.8.
Then b_0 = (sum y - b_1(sum x))/n = (14 - 8)/4 = 1.5. Typical questions ask you to calculate a line from data or summaries, interpret a coefficient with correct units and scope, or judge a prediction using residuals and R^2.
Use three exact checks for this example:
The line passes through (2.5,3.5).
sum_i e_i = 0.
sum_i (x_i e_i) = (-0.3) + (-0.2) + 3.3 + (-2.8) = 0.
If any check fails, revisit the arithmetic before interpreting the model.
8. Linear regression short version and next step
The reusable sequence is:
Compute x-bar and y-bar.
Calculate S_xy and S_xx.
Obtain b_1 and b_0.
Predict y-hat and calculate residuals.
Compute SSE and R^2, then inspect fit and scope.
For this dataset, the anchors are y-hat = 1.5 + 0.8x, SSE = 1.8, and R^2 = 0.64. A fitted line is only the first pass. Interpretation, residual checking and range awareness decide whether it is useful.
For a broader structured route, continue with AI & ML for Placements. To compare adjacent learning paths, browse the Skill Development Courses category.
Keep learning
Gradient Descent for Linear Regression: Derive and Run One Update by Hand
See prediction, residual, gradient and parameter update as one continuous calculation. Then verify the lower loss and test what an oversized learning rate does.
Counting Figures Reasoning: Shortcuts for Triangles, Squares and Rectangles
Learn a checkable method for counting composite figures, with fully worked triangle, rectangle, crossed-square and painted-cube examples.
Hasse Diagram, Greatest and Least Elements: Upper and Lower Bounds with Worked Examples
Build one divisibility Hasse diagram, then use it to distinguish greatest from maximal and calculate bounds, LUB, and GLB step by step.
Hasse Diagram MCQs: 12 Solved Questions on Greatest, Least, Upper and Lower Bounds
Solve 12 Hasse diagram questions across MCQ, MSQ and NAT formats, with visible cover-pair counts, extremal-element checks, bounds and partition refinement.





