Linear regression - explained simply
Introduction and exam tips
Module 5 in plain language
Module 5 is about one question: how is an outcome (y) related to one or more explanatory variables (x), and how sure are you about that? You draw a straight line through a cloud of points and read the line. R does the calculation with lm(); you must be able to read the output.
Infinitely many lines fit a cloud of points. The method ordinary least squares (OLS) chooses the line for which the sum of squared residuals (SSR) is as small as possible. A is the difference between the observed y and the predicted y. In the population model that difference is called the : everything that affects y but is not in your model.
The line has two parts. The is the predicted y when all x are zero. The is the change in y on average when x rises by one unit. With observational data you prefer “is associated with” over “causes”: a regression does not prove causality.
With several predictors you write y = b0 + b1 x1 + b2 x2 + … You read each coefficient ceteris paribus: the effect of that x, while the other predictors in the model stay constant. Example from the retail chain (simulated data, 90 stores): per extra 1000 euros of advertising, weekly revenue rises on average by 2.66 thousand euros, ceteris paribus. Always state the units.
A coefficient is an estimate from a sample. The standard error (SE) says how precise it is. The t-value is b divided by SE (under H0: beta = 0). The p-value is the probability of an estimate this extreme if the population coefficient is zero. Below 0.05 means significant at 5%. The p-value is not the probability that H0 is true, and says nothing about the size of the effect.
The is b plus or minus roughly two times the SE. If zero lies in the interval, the coefficient is not significant at 5%.
R-squared is the share of the variation in y that the model explains: SSE/SST, or 1 - SSR/SST. In the retail example R-squared is 0.6644: the model explains 66.4% of the variation in revenue. There is no fixed threshold for “high enough”; it depends on the field.
R-squared rises or stays equal with every extra predictor, even a useless one. That is why you compare models with a different number of predictors using adjusted R-squared: 1 - (1 - R2)(n - 1)/(n - k - 1). It penalises extra predictors and can fall.
The at the bottom of the output tests whether all slopes are zero at the same time. A (anova(small, large)) tests whether a group of extra predictors adds something together.
A category such as region is converted into a : 1 for the category, 0 for the rest. With k categories you include k - 1 dummies. The category without a dummy is the reference category; the belongs to that group. Example from the housing data (Long Island): the coefficient for Suffolk is -157.98. That is the average difference in value per square metre with Nassau, the reference category, at equal taxes.
- : an omitted variable that is related to y and to an included x distorts that coefficient. You notice it when the coefficient changes strongly once you add the variable.
- : predictors that are strongly related. The estimate stays , but the standard errors become large. Check with the = 1/(1 - R2_j); above roughly 5 to 10 it is a problem.
- Extrapolation: predicting outside the range of the data is unreliable.
- Outliers: two outliers can turn a p-value of 0.216 into 0.047 (energy prices). So always look at the data.
An (x1 * x2) lets the effect of x1 depend on x2: the is then b1 + b3 x2. With a dummy that means a different slope per group. A makes the line curved; the minimum or maximum is at -b1/(2 b2). A gives percentage effects: in log(y) = b0 + b1 x, b1 is roughly a relative change per unit of x. Mediation means x works on y through an intermediate variable; moderation means a variable changes the strength of the effect. You use a standardized coefficient to compare predictors with different units.
- Read the coefficient and state the units, with .
- Judge the : significant or not, and why.
- State or as the share of explained variation.
- Discuss sample, bias and validity: who is and is not in the data?
Linear regression - exam summary
Introduction and exam tips
Module 5 for the exam
Module 5 yields 7 multiple-choice questions (17.5 points) and the open question 7.2 (9 points, at most 250 words). The formula sheet only contains R-squared, adjusted R-squared and the t-test; you must know all other formulas. The examples below come from real R output; simulated cases are marked as such.
OLS chooses the line with the smallest sum of squared residuals (SSR). A is observed y minus predicted y. The belongs to the population model and contains everything that affects y and is not in the model. With OLS and an the sum of the residuals is always zero, which is why you square them.
Exam tip: “OLS minimises the sum of the absolute residuals” is wrong; it is the squared residuals.
The intercept is the predicted y when all x are zero (with dummies: the prediction for the ). The slope b1 is the change in y on average for a rise of x by 1 unit. In a multiple regression you add ceteris paribus: the other predictors held constant.
Tap a part for the explanation from the summary.
The coefficient: the expected change in Y per unit of X, other variables held constant.
The uncertainty of the estimate: how much the estimate would fluctuate in another sample.
The estimate divided by the standard error. The further from zero, the stronger the evidence.
The p-value of the test that the coefficient is zero. Smaller than alpha: significant.
The share of the variation in Y that the model explains. A high R² is not yet proof of a cause.
You do not write R code in the exam, but you do read the output of `summary(lm(...))`. Read per row: Estimate, then p-value.
Example (simulated data, 90 stores, revenue in thousand euros): omzet = 43.27 + 2.66 reclame + 6.31 personeel - 7.08 parkeer. Per extra 1000 euros of advertising, revenue rises on average by 2.66 thousand euros, ceteris paribus. Prediction for reclame 10, personeel 8 and parkeer 1: 43.27 + 26.63 + 50.47 - 7.08 = 113.28 (rounded).
Regression on observational data shows association, not causality. Phrase it as “is associated with”.
The t-value is (b - beta0)/SE; under H0: beta = 0 it is b/SE. Reclame: 2.6625/0.6187 = 4.30, p = 4.4e-05, so significant. Parkeer: -7.0823/4.8682 = -1.46, p = 0.149, so not significant at 5%.
The p-value is the probability of an estimate this extreme if H0 is true. It is not the probability that H0 is true. The confidence interval is b +/- roughly 2 x SE. If it contains zero, not significant at 5%. Statistically significant is not the same as practically important: also look at the size.
R-squared = SSE/SST = 1 - SSR/SST, with SST = SSE + SSR. Retail model: R-squared 0.6644 means 66.4% of the variation in revenue is explained. Adjusted R-squared = 1 - (1 - R2)(n - 1)/(n - k - 1) = 1 - 0.3356 x 89/86 = 0.6527.
R-squared rises or stays equal with every extra predictor; adjusted R-squared can fall and is the measure to compare models with a different number of predictors. It does not decide whether a predictor is significant: the does. A high R-squared says nothing about causality or bias.
The F-test at the bottom of summary(lm) tests H0: all slopes are zero at the same time. F = (R2/k)/((1 - R2)/(n - k - 1)) = (0.6644/3)/(0.3356/86) = 56.75. A significant F says the model as a whole explains something, not that every predictor is significant.
The partial F-test (anova(small, large)) compares two nested models. Long Island housing data: the large model with four extra style dummies (both models have taxes and rooms) lowers the sum of squares from 793,934 to 607,120; F = ((793,934 - 607,120)/4)/(607,120/83) = 6.38, p = 0.00016. The four dummies add something together. rises from 0.0785 to 0.2953, from 0.0573 to 0.2444.
A category with k levels becomes k - 1 dummy variables. The remaining category is the reference category; the belongs to that group and each dummy coefficient is the difference with that group, . Pitfall: k dummies at once gives perfect (the dummy trap).
Example (houses on Long Island, 90 houses): valuem2 = 208.11 + 0.029 taxes - 157.98 f.regionSuffolk; 0.427. A house in Suffolk is worth on average 157.98 dollars per m2 less than in Nassau at equal taxes. If you change the reference to Suffolk, the dummy gets +157.98 and the intercept changes; R-squared and predictions stay equal.
If a variable is missing that is related to both y and an included x, that coefficient is distorted (omitted variable bias): it also picks up the effect of the omitted variable. You recognise it because the coefficient changes strongly once you add the variable. The reverse is not true: a variable that is unrelated to x distorts nothing.
Multicollinearity is strong association between predictors. Consequence: larger standard errors and unstable, often non-significant coefficients; the estimate stays . The VIF of variable j is 1/(1 - R2_j), with R2_j from the regression of x_j on the other predictors. At R2_j = 0.8 the VIF is 5. Housing data: rooms 1.713, bedrooms 1.532, bath 1.229: no problem. Solution: drop or combine one of the variables.
A expresses the effect in standard deviations: 1 sd more in x goes with b sd more in y. You use it to compare the relative strength of predictors with different units. The p-values do not change.
A : y = b0 + b1 x + b2 x^2 gives a marginal effect b1 + 2 b2 x and a minimum or maximum at -b1/(2 b2) (a minimum if b2 > 0). Housing data: lotsize -0.6455 and lotsize^2 0.0001853; minimum 0.6455/(2 x 0.0001853) = 1742 within a range of 381 to 2044; 0.7204. Outside that range the curve does not hold: is unreliable.
A : in log(y) = b0 + b1 x, b1 is roughly a relative change of 100 x b1 percent per unit of x (exactly: e^b1 - 1); in log-log b1 is an elasticity (percent per percent). Example (simulated data, 240 alumni): in log(salaris) the coefficient for vrouw is -0.056; that is roughly 5.6% lower salary, ceteris paribus (p = 0.081, not significant at 5%).
With y ~ x1 * x2 the effect of x1 equals b1 + b3 x2. The main coefficient b1 is the effect at x2 = 0. With a dummy and a number each group has its own . Example (simulated data, 150 deliveries): the interaction afstand x regen is 0.774 extra minutes per kilometre when it rains. Housing data rooms * f.region: interaction 6.92 with p = 0.584, so no evidence for a difference in slope.
Mediation means x works on y through an intermediate variable m. You estimate three regressions: y on x (total effect), m on x and y on x and m (direct effect). If the direct effect of x becomes zero once m is in the model, there is full mediation. is different: there a variable changes the strength of the effect ().
You predict with predict(model, newdata = data.frame(...)). A is wider than a , because it also contains the spread of an individual y. Energy data: 36.961 + 0.14 x 5 = 37.66. Predicting outside the range of x (extrapolation) is unreliable.
Two outliers can reverse a result: in the energy data the is 0.216 with all observations and 0.047 without the two observations with pCool above 35 ( 0.7% to 1.9%). That is a reason to look at the data, not to delete observations without reason.
- Coefficients: read b with units, , and say what it means for the case (2 points).
- P-values: significant at 5% yes or no, what H0 says, no probability of H0; significance says nothing about the size or importance of the effect (2 points).
- Model fit: or as share of explained variation, and RSE/RMSE as typical prediction error; a good fit on this data does not guarantee good predictions for new data (2 points).
- Sample: who is in it, bias, representativeness, internal and external validity (3 points).
Linear regression - deeper dive with the lecturer's own words
Introduction and exam tips
This deeper dive explains per topic what the lecturer emphasises, with the matching statements from the lecture recording (quoted in the original Dutch). Read the exam summary first; here it is about nuance and pitfalls.
The line is not an estimate by eye. Among all possible lines, OLS looks for the line for which the sum of squared residuals is minimal. A nuance that is often forgotten: that is a choice of the method, not a law of nature. Other criteria give other lines.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 34:10:
die trendlijn die je dan krijgt, die bepaalt Excel door eigenlijk die kleinste kwadraten methode toe te passen. Je zoekt onder alle trendlijnen, lijntjes die er mogelijk zijn, naar degene die de som van de kwadrateerde residueur zo klein mogelijk maakt.
You read a coefficient from observational data as association. The lecturer chooses his words deliberately.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 42:02:
Dus merk hierop, ik gebruik de term is geassocieerd met, omdat ik niet helemaal zeker weet of het echt causaliteit is.
On the exam you therefore score with “is associated with” and with a remark about bias and confounders, not with “causes”.
The p-value belongs to the hypothesis test with H0: beta = 0. The line is then flat.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 52:56:
stel dat het lijntje nu echt plat zou lopen, dan is de kans dat wij 0,38 eruit zouden krijgen op extreme, of minus 0,38 of nog extremer, die kans is maar 0,047, dus 4,7%.
The conditional phrasing (“suppose H0 is true”) is what earns points. The same energy data gives p = 0.216 with two outliers included: the conclusion depends on two observations.
R-squared is the piece of the pie that your model has eaten: SSE/SST, or 1 - SSR/SST. The lecturer uses the mean as the benchmark.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:07:23:
Je R-squared is simpelweg het stukje van de taart dat is opgegeten door je model.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:08:27:
Dan heeft hij niet beter gedaan dan onze benchmark, namelijk het simpelweg gebruik van de gemiddelde.
An R-squared of 0 therefore means: no better than predicting the mean. When is R-squared high enough? A physicist validating a law of nature expects almost 100%; someone predicting share prices is happy with far less.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:09:51:
En eigenlijk het antwoord daarop is, dat hangt er vanaf. Een heel onbevredigend antwoord natuurlijk
R-squared can only rise or stay equal. The adjusted variant corrects for that with the factor (n - 1)/(n - k - 1).
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:13:09:
Dus je wil alleen die extra variabele gaan toevoegen als hij echt een substantiële bijdrage levert aan het verklaren van onze data.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:13:41:
die adjusted R-squared die kan omlaag gaan als je een extra voorspellende variabele toevoegt dus als je die toevoegt en eigenlijk je model fit die wordt niet heel veel beter
Nuance: adjusted R-squared can even become negative, and then you no longer read it as a percentage. Exam mistake: thinking that adjusted R-squared always rises.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:33:12:
Je bent geneigd om te zeggen, we hebben k-categorieën. Dus we moeten k-dummy variabelen doen. Maar in plaats daarvan moet je er k-1 aan maken.
The category that drops out becomes the baseline. The worked example in the housing data shows how to read it.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:35:35:
Dat is het verschil ten opzichte van de referentiecategorie Nassau.
The is the value per square metre in Nassau; the dummy for Suffolk (-158) is the difference. If you swap the reference, the sign flips and the intercept changes.
A group of dummies (for example four styles) is not judged with four separate t-tests but with a partial F-test.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:54:06:
voegen deze variabelen nu iets toe aan mijn model, dan kun je die partiële F-toets gebruiken.
In the housing data: F = ((793,934 - 607,120)/4)/(607,120/83) = 6.38, p = 0.00016. The group adds something together, even if not every dummy is significant on its own.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:56:44:
je moet altijd jezelf afvragen, ik heb hier een effect, kan dat effect misschien worden gedreven door een variabele die ik nog niet heb meegenomen?
The lecturer also warns against the opposite: throwing everything in.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 1:58:46:
Niet zomaar zeggen, nou ik heb honderdduizend variabelen, ik gooi ze er allemaal in.
Choose predictors on the basis of theory and logic. More variables raise mechanically, but cost degrees of freedom and can cause (larger standard errors, no bias).
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 2:00:17:
nou die beta 3 die zegt met hoeveel verandert mijn helling nu als mijn x2 met eentje stijgt
With beta 3 = 5 the of x1 rises by 5 for every unit that x2 rises. The effect of x1 is b1 + b3 x2; b1 alone is the effect at x2 = 0.
A can behave absurdly outside the range of the data.
Gezegd door Harwin de Vries in Module 5 - Opname college (video), minuut 2:11:43:
je heel voorzichtig moet zijn met extrapoleren van jouw functie buiten de regio waar je veel data hebt.
In the housing data the minimum of the curve is at lotsize = 0.6455/(2 x 0.0001853) = 1742, within the range of 381 to 2044. Outside that you would not trust the prediction.
Dataset LongIsland01, 90 houses. Model: valuem2 = 208.11 + 0.02906 taxes - 157.98 f.regionSuffolk, 0.427, 0.4139.
- : a house in Suffolk is worth on average 157.98 dollars per m2 less than in Nassau, the , at equal taxes.
- Prediction at taxes = 3000: Nassau 208.11 + 87.18 = 295.29; Suffolk 295.29 - 157.98 = 137.31 dollars per m2.
- Partial F-test: adding the four style dummies gives F = 6.38 (p = 0.00016); R-squared rises from 0.0785 to 0.2953 and adjusted R-squared from 0.0573 to 0.2444. Together they add something.
- Interaction term rooms x region: 6.92 with p = 0.584. No evidence for a difference in slope.
- Quadratic term: the minimum of lotsize is at 0.6455/(2 x 0.0001853) = 1742, within the range of 381 to 2044.
- Question 7.2 asks for four things: coefficients, p-values, model fit and sample (bias, internal and external validity). Use and units, and phrase p-values conditionally.
- Multiple-choice questions about , dummy variables, and keep coming back; see the practice exams.
- Standardized coefficient and mediation are less heavily represented, but belong to the same material.
Case: Energy02, 211 buildings, pEnergy ~ pCool, then without the two buildings with pCool above 35 (real output).
- Coefficients (2 p): all buildings +0.14 energy intensity per extra percentage point of cooling space, without the two outliers +0.38, . The estimate therefore depends strongly on two observations.
- P-values (2 p): p = 0.216 with all buildings (not significant), p = 0.047 without the outliers (just significant at 5%). The is the probability of an estimate at least as extreme as the one found, if H0 is true; not the probability of H0. Significance says nothing about the size or importance of the effect.
- Model fit (2 p): 0.7% and 1.9%; pCool explains almost nothing; the residual is over 11 (11.25 and 11.23), so the model predicts new buildings hardly better than the mean.
- Sample (3 p): the two buildings were removed because they are influential (pCool above 35), not because they are wrong; removing them post hoc must be justified and the conclusion is not robust (p 0.216 to 0.047); omitted variable bias from other factors (age, use); whether 211 buildings are representative is unknown, so external validity is limited.