Draws the best straight line through a cloud of points.
lm(y ~ x)
Key idea
Linear regression fits a straight line, written y = a + b*x, through the dots. (e.g. y = 2 + 3x)
The slope b is how much y changes for each one-unit rise in x. (e.g. b = 3)
The intercept a is the predicted y when x is 0. (e.g. a = 2)
You can use the line to predict y at a new x value. (e.g. x = 4 -> y = 14)
It assumes the relationship is roughly a straight line.
Definition
y^=a+bx,b=∑(xi−xˉ)2∑(xi−xˉ)(yi−yˉ)
x
the predictor (what you know)
y^
the predicted value
a
the intercept
b
the slope
b=∑(x−xˉ)2∑(x−xˉ)(y−yˉ)≈4.6,y^≈45.2+4.6x
Where you'd use it
From past data, lm might fit the line score ≈ 45 + 4.6 × hours. To predict a student who studies 6 hours, you plug in 6 and get about 73, turning study time into an expected exam score.
Fitting ice-cream sales against temperature gives a line whose slope says each extra degree adds, say, 20 sales; plug in tomorrow’s forecast of 30 °C to estimate the day’s sales.
For houses, lm learns price ≈ a + b × floor area; feeding in a 100 m² home returns a predicted price, but trusting it for a 1000 m² mansion far outside your data would be risky extrapolation.
Frequently asked questions
How do I read the slope?
The change in y for a one-unit increase in x.
Is a high R² enough?
No — check the residual plots, and remember correlation isn’t causation.