Corrected exercises: correlation and linear regression (201-337)
Here are ten corrected exercises on correlation and linear regression for the Advanced Statistics course 201-337 at CEGEP, preceded by a list of calculations. This is the chapter you will meet everywhere afterwards: in economics, in biology, in public health, in any report claiming that one thing explains another. It is also the chapter where students go wrong most often, because the calculator fits a line and computes a coefficient on any scatter plot, even an absurd one. The set covers the scatter plot, the linear correlation coefficient r, the least-squares line, the coefficient of determination r2, prediction and extrapolation, outliers and leverage points, residual analysis and the standard error of the estimate, linearizing an exponential or power model, the significance test for the correlation coefficient, and the distinction between correlation and causation.
Exercise 0.1 opens the set with ten direct linear regression calculations, as in textbook drill lists: the working sums Sxy, Sxx and Syy obtained from the raw sums, the correlation coefficient r=SxxSyySxy and the coefficient of determination r2, the slope and the intercept of the least-squares line, a predicted value and a residual, each with one value to find and checked as you type it.
By the end, you can describe a scatter plot before computing anything (exercise 1), write and interpret the least-squares line (exercise 2), judge a prediction and refuse an extrapolation (exercise 3), measure the effect of a single outlier or leverage point on r and on the slope (exercise 4), read a U-shaped residual plot (exercise 7), straighten out bacterial growth by regressing lnN on t (exercise 8), and test H0:ρ=0 with the t statistic on n−2 degrees of freedom, where the same r=0.50 is significant for 30 individuals and not for 10 (exercise 9). Exercise 6 carries out a complete regression of rent on floor area for apartments.
Exercise 5 deals with the point that truly matters: a correlation coefficient of 0.95 between ice cream sales and the number of drownings establishes strictly nothing, because a third variable, temperature, explains both. Being able to state that objection is what separates a trained reader from a gullible one, and exercise 10 practises it on five claims from the press, from chocolate and Nobel prizes to the dose of a drug.
Self-checking setType your answers and check them question by question: the page tells you right or wrong before you open the solution. With an account, every answer right on the first try earns points, and your right answers stay filled in when you come back.
•Always study the SCATTER PLOT before computing: shape, linearity, outliers. A coefficient computed on a nonlinear scatter plot means nothing.
•Working sums: Sxy=∑xy−n(∑x)(∑y), Sxx=∑x2−n(∑x)2 and Syy=∑y2−n(∑y)2.
•Linear correlation coefficient: r=SxxSyySxy, always between −1 and 1. The SIGN gives the direction, the ABSOLUTE VALUE the strength.
•Least-squares line y=a+bx: slope b=SxxSxy, then a=y−bx. It ALWAYS passes through the mean point (x,y).
•“Least squares” means that the line minimizes the sum of the squares of the VERTICAL deviations between the points and the line. These deviations are called residuals, and their sum is zero.
•Coefficient of DETERMINATION r2: the proportion of the variation in y explained by the linear relationship with x. It is expressed as a percentage.
•EXTRAPOLATING, that is, predicting outside the range of the observed x values, is never justified by the model: nothing guarantees that the relationship continues.
•CORRELATION IS NOT CAUSATION. Three competing explanations for any correlation: x causes y, y causes x, or a third, CONFOUNDING variable causes both. Chance on a small sample is a fourth.
•The correlation coefficient is NOT robust: a single outlier can make r2 drop from 99% to 65%, and a LEVERAGE point, far away in x, can halve the slope while leaving r high. Never delete an inconvenient point: investigate, publish with and without it, or switch to a robust method.
•Residuals: ei=yi−yi^. Their sum is zero by construction and proves nothing. The residual PLOT must be a patternless cloud; a curvature signals a nonlinear relationship, a funnel a non-constant spread. Standard error of the estimate: se=n−2∑ei2, and ∑ei2=Syy(1−r2).
•Linearization: an EXPONENTIAL model y=aebx is straightened by regressing lny on x (semi-log); a POWER model y=axb is straightened by regressing lny on lnx (log-log), where the slope gives the exponent directly.
•Significance of r: t=1−r2rn−2 on n−2 degrees of freedom. The same r=0.50 is not significant for 10 individuals and is significant for 30: significance measures how solid the EVIDENCE is, never the strength or the importance of the relationship.
Calculations: r, the slope and the intercept
Exercise 0.1: Computing r, the least-squares line and a prediction: ten direct calculations
For each line, compute the requested quantity from the sums or coefficients given, without drawing a scatter plot or interpreting. Give the exact value when it comes out evenly, otherwise round to the hundredth, and to the thousandth for r and r2.
Always the same order. First go from the raw sums to the working sums, Sxy=∑xy−n∑x∑y, Sxx=∑x2−n(∑x)2 and Syy=∑y2−n(∑y)2. From them you get r=SxxSyySxy, the slope b=SxxSxy and the intercept a=y−bx. Finally the line y^=a+bx is used to predict, and the residual of a point is read as e=y−y^.
a) n=5, ∑x=30, ∑y=85, ∑xy=564: compute Sxy.
b) n=8, ∑x=52, ∑x2=380: compute Sxx.
c) n=7, ∑y=60, ∑y2=572: compute Syy.
d) Sxy=−84, Sxx=60, Syy=150: compute the correlation coefficient r.
e) r=−0.74: compute the coefficient of determination r2.
f) Sxy=45, Sxx=18: compute the slope b of the least-squares line.
g) b=−1.6, x=12, y=40: compute the intercept a.
h) Line y^=14.2+3.5x: compute the predicted value for x=7.4.
i) Line y^=88−2.4x: compute the residual of the point (15,49).
j) n=5, ∑x=15, ∑y=55, ∑xy=186, ∑x2=55: compute the slope b, then the intercept a.
Show the solution
Answers
a)Sxy=564−510=54
b)Sxx=380−338=42
c)Syy=572−73600≈57.71
d)r=9000−84≈−0.885
e)r2=0.5476≈0.548
f)b=1845=2.5
g)a=40+19.2=59.2
h)y^=14.2+25.9=40.1
i)e=49−52=−3
j)b=1021=2.1; a=11−6.3=4.7, that is, y^=4.7+2.1x
Everything hinges on the working sums: an S is always computed as “sum of the products, minus the product of the sums divided by n”, and that is where students confuse ∑x2, the sum of the squares, with (∑x)2, the square of the sum. Once Sxy, Sxx and Syy are in hand, r, b and a are nothing more than quotients.
a)
Sxy=564−530×85=564−52550=564−510=54. The positive sign announces a positive slope.
b)
Sxx=380−8522=380−82704=380−338=42. Pitfall: writing n∑x2 instead of n(∑x)2; it is the sum that gets squared.
c)
Syy=572−7602=572−73600=572−514.29=57.71. Pitfall: rounding 73600 to the nearest unit too early, which gives 58; keep at least two decimals in the intermediate calculation.
d)
r=60×150−84=9000−84=94.868−84≈−0.885. The sign of r is always that of Sxy, since the square root is positive. Pitfall: forgetting the square root, 9000−84, which gives a tiny r.
e)
r2=(−0.74)2=0.5476≈0.548. Pitfall: keying in −0.742 without parentheses, which the calculator reads as −(0.742)=−0.5476; a coefficient of determination is never negative.
f)
b=SxxSxy=1845=2.5. Pitfall: dividing by Syy or by SxxSyy, which is the denominator of r, not that of the slope.
g)
a=y−bx=40−(−1.6)(12)=40+19.2=59.2. Pitfall: losing the double minus sign and writing 40−19.2=20.8.
h)
y^=14.2+3.5×7.4=14.2+25.9=40.1.
i)
y^(15)=88−2.4×15=88−36=52, so e=y−y^=49−52=−3: the point lies below the line. Pitfall: computing y^−y=+3, the residual the wrong way round.
j)
Sxy=186−515×55=186−165=21 and Sxx=55−5152=55−45=10, so b=1021=2.1. The means are x=515=3 and y=555=11, so a=11−2.1×3=11−6.3=4.7: the line is y^=4.7+2.1x.
Check: Sxx and Syy are always positive and r always stays between −1 and 1; a value outside these bounds signals a sum copied wrongly or a misplaced square. The line always passes through the mean point: in j), 4.7+2.1×3=11=y.
Tick the exercises you have done or want to review: a free account, no password, keeps your ticks and your right answers from one visit to the next, tells you which chapter to tackle next and lets you ask for the exercise you are missing. Create your space, an email is enough.
Part A: scatter plot, correlation and the line (/50)
Exercise 1: The scatter plot and the correlation coefficient
Before any formula, look at the scatter plot. The correlation coefficient measures ONLY the strength of a LINEAR relationship, and it can be misleading if that assumption has not been checked.
A study relates the number of hours of study per week x to the grade obtained y for six students: (2,35), (4,48), (5,52), (7,68), (9,80), (11,95).
a) Describe the scatter plot: direction, form, apparent strength of the relationship.
b) Compute ∑x, ∑y, ∑xy, ∑x2 and ∑y2.
c) Compute Sxy, Sxx and Syy.
d) Compute the correlation coefficient and interpret it.
e) Does a correlation coefficient close to zero mean that there is no relationship at all between the two variables?
Show the solution
Answers
a)Increasing scatter plot, aligned, with no curvature and no outlier: the relationship looks linear and very strong.
b)∑x=38, ∑y=378, ∑xy=2763, ∑x2=296, ∑y2=26282.
c)Sxy=369, Sxx=55.333, Syy=2468.
d)r=369.54369=0.9985: an almost perfect positive linear relationship.
e)No: r≈0 only rules out a LINEAR relationship. A symmetric parabola gives r=0 with y completely determined by x.
a)
The scatter plot is clearly INCREASING: the grades go up with the number of hours of study. The points lie very close to a line, with no visible curvature and no outlier: the relationship is linear and appears very strong. This preliminary reading is essential, because it is what justifies using the linear correlation coefficient.
r=SxxSyySxy=55.333×2468369=369.54369=0.9985. The correlation is POSITIVE, so the two variables move in the same direction, and its absolute value is extremely close to 1: the linear relationship is almost perfect. Such a coefficient is rare on real data; here it points to constructed data or to a very regular phenomenon.
e)
NO, and this is the classic trap. An r close to zero only means that there is no LINEAR relationship. There may very well be a strong but curved relationship: on a scatter plot shaped like a symmetric parabola, for example y=x2 with x values spread symmetrically around zero, the coefficient is exactly 0 even though y is completely determined by x. That is precisely why you DRAW the scatter plot before computing: the coefficient alone cannot tell “no relationship” apart from “a nonlinear relationship”.
Exercise 2: The least-squares line
Of all possible lines, only one minimizes the sum of the squares of the vertical deviations. Its two coefficients are computed from the same working sums.
Use the data of exercise 1 again: Sxy=369, Sxx=55.333, x=6.333 and y=63.0.
The graph below shows the data.
a) Compute the slope, then the intercept, and write the equation of the line.
b) Interpret the slope and the intercept in context, stating whether the intercept makes sense here.
c) Check that the line passes through the mean point.
d) What exactly does “least squares” mean? Why minimize the squares rather than the deviations themselves?
e) Compute the residual of the student who studied 5 hours and obtained 52. What does its sign mean?
Show the solution
Answers
a)b=6.669 and a=20.765, so y=20.77+6.67x.
b)One more hour of study is worth 6.67 points on average. The intercept, 20.77, is an extrapolation at x=0.
c)20.765+6.669(6.333)=63.0=y: the line passes through the mean point, by construction.
d)What is minimized is ∑(yi−y^i)2. The raw deviations cancel out for infinitely many lines.
e)y^(5)=54.11 and e=−2.11: the student is below the line, and it is the largest residual of the six.
a)
b=SxxSxy=55.333369=6.669. Then a=y−bx=63.0−6.669×6.333=63.0−42.235=20.765. The equation is therefore y=20.77+6.67x.
b)
The SLOPE means that one extra hour of study per week is associated with an average increase of 6.67 points in the grade. The INTERCEPT, 20.77, would be the grade of a student who studies zero hours. Handle this number with care: x=0 lies outside the observed range, which runs from 2 to 11 hours, so it is an extrapolation. It may be read as a plausible order of magnitude, but the model does not guarantee it.
c)
The mean point is (6.333,63.0). Substituting into the equation: 20.765+6.669×6.333=20.765+42.235=63.0. The line does pass through the mean point, and this is no coincidence: it follows directly from the formula a=y−bx, which is built to make it happen. It is a quick check to make systematically on every regression.
d)
“Least squares” means that the line chosen is the one that makes the sum ∑(yi−y^i)2 as SMALL as possible, where y^i is the predicted value. The raw deviations are not minimized because their sum is ZERO for infinitely many lines, the positive deviations cancelling the negative ones: the criterion would select nothing. Squaring prevents this cancelling, penalizes large deviations more than small ones, and leads to simple explicit formulas, which is not the case if you minimize the absolute values.
e)
The predicted value for x=5 is y^=20.765+6.669×5=54.11. The residual is y−y^=52−54.11=−2.11. Its NEGATIVE sign means that this student obtained a grade LOWER than the one the model predicted for that amount of study: the point lies below the line. It is in fact the largest residual in absolute value of the six, hence the worst-fitted point of the scatter plot.
Exercise 3: The coefficient of determination and prediction
The square of the correlation coefficient is read directly as a percentage: the share of the variation in y that the model explains. It is the indicator quoted to judge the quality of a fit.
Keep r=0.9985 and the line y=20.77+6.67x, valid for x between 2 and 11 hours.
The graph below shows the data.
a) Compute the coefficient of determination and state its interpretation.
b) What does the remaining 0.3% represent?
c) Predict the grade of a student who studies 8 hours a week.
d) Predict the grade of a student who studies 20 hours. Is this prediction legitimate?
e) What would the prediction be for 30 hours? What does this result reveal about the limits of the model?
Show the solution
Answers
a)r2=0.9971: 99.7% of the variation in the grades is explained by study time.
b)The residual variation: aptitude, quality of studying, form on the day, luck of the questions, measurement errors.
c)y^(8)=74.1. A legitimate interpolation, since 8 lies between 2 and 11.
d)y^(20)=154.1, impossible: it is an extrapolation outside the observed range.
e)y^(30)=220.8. The absurdity makes the error visible; the danger is an extrapolation that produces a plausible number.
a)
r2=0.99852=0.9971, that is, 99.7%. Interpretation: 99.7% of the observed variation in the grades is EXPLAINED by the linear relationship with the number of hours of study. This is an exceptionally good fit.
b)
The remaining 0.3% is the RESIDUAL variation, that is, everything study time does not account for: individual aptitude, the quality of the studying and not just its length, how the student feels on exam day, the luck of the questions chosen, as well as measurement errors. No statistical model claims to explain 100% of a human phenomenon, and an r2 of 1.000 on real data would in fact be suspicious.
c)
y^=20.765+6.669×8=20.765+53.35=74.1. The prediction is about 74. It is LEGITIMATE because 8 hours lies inside the range of the observed data, between 2 and 11: this is an interpolation.
d)
y^=20.765+6.669×20=20.765+133.37=154.1. The prediction exceeds 100: it is IMPOSSIBLE, since the grade cannot go above 100. The prediction is therefore not legitimate, and for a fundamental reason: x=20 is far beyond the observed range, so we are EXTRAPOLATING. Nothing in the data guarantees that the relationship stays linear beyond 11 hours; in reality it necessarily levels off.
e)
y^=20.765+6.669×30=220.8, more than twice the maximum possible. The absurdity of the result is actually good news here: it makes the error VISIBLE. The real danger of extrapolation is the case where it produces a plausible number that nothing flags as wrong. The rule is therefore never to predict outside the range of the observed x values, and always to state that range of validity next to the equation. A linear model is only a LOCAL approximation of a relationship that, in reality, almost always saturates.
Exercise 4: Outliers and leverage points
Take the six students again, relating hours of study x to grade y: (2,35), (4,48), (5,52), (7,68), (9,80), (11,95), for which r≈0.9985 and the line is y=20.77+6.669x. Add a seventh student to them, one at a time, and observe the damage.
The graph below shows the data.
a) A seventh student studied 5 hours and obtained 90. Recompute x, y, then Sxy, Sxx, Syy and the correlation coefficient.
b) Compute the new coefficient of determination and compare it with the old one. Comment on how large the effect of a single point out of seven is.
c) Replace this seventh student by another one, who studied 20 hours and obtained 100. Recompute the slope and the correlation coefficient. Compare with case a).
d) The influence a point exerts on the line is called its LEVERAGE, and it grows with the distance between its x and the mean of the x values. Justify this fact from the formula for the slope, and say which of the two added points is the more dangerous.
e) In practice, what should be done about a point that distorts the analysis in this way?
b)r2 falls from 0.997 to 0.650: one point out of seven has destroyed most of the relationship.
c)With (20,100): b=3.72 and r=0.906. The slope is almost halved without r raising any alarm.
d)In b=∑(xi−x)2∑(xi−x)(yi−y), a distant x weighs enormously. The point (20, 100) is the more dangerous.
e)Investigate, then publish both analyses, then consider a robust method. Never delete a point because it is inconvenient.
a)
With the point (5,90): n=7, ∑x=43, ∑y=468, ∑xy=3213, ∑x2=321, ∑y2=34382. So x≈6.143 and y≈66.86. Working sums: Sxy=3213−7(43)(468)≈338.1, Sxx=321−7432≈56.86, Syy=34382−74682≈3092.9. Coefficient: r=(56.86)(3092.9)338.1≈0.806.
b)
r2 goes from 0.997 to 0.650: the proportion of variation explained collapses from 99.7% to 65%. One point out of seven, that is 14% of the data, has destroyed most of the relationship. This is the most useful reminder of the chapter: the correlation coefficient is not a robust measure, it is extremely sensitive to extreme values, and reporting it without having looked at the scatter plot amounts to betting blindfolded.
c)
With (20,100): ∑x=58, ∑y=478, ∑xy=4763, ∑x2=696, ∑y2=36282. This gives Sxy≈802.4, Sxx≈215.4 and Syy≈3641.4, hence a slope b=215.4802.4≈3.72 and r≈0.906. The contrast with a) is instructive: here the coefficient stays high, apparently reassuring, while the SLOPE has almost been halved, going from 6.67 to 3.72. The model now predicts that one extra hour of study brings only 3.7 points instead of 6.7: the practical conclusion is turned upside down without r sounding the alarm.
d)
The slope is b=SxxSxy=∑(xi−x)2∑(xi−x)(yi−y). Each point enters through its deviation xi−x, and a point very far away in x contributes a huge term to the numerator as well as to the denominator: it weighs much more heavily than the others in determining the line. A point far away in x literally acts as a LEVER, with the mean point as the pivot. So it is the second point, (20,100), that is the more dangerous: the first one visibly lowers the coefficient and alerts the analyst, while the second moves the line silently, leaving an r of 0.906 that would go unquestioned in any report.
e)
Above all, do not delete it because it is inconvenient. The correct approach has three stages. First INVESTIGATE: is it a data-entry error, a badly converted unit, an individual who does not belong to the population under study? A value that is demonstrably wrong is corrected or set aside, with a written justification. Next, if the point is genuine, PUBLISH both analyses, with and without it, and let the reader judge its impact: this is standard practice in research. Finally, consider a ROBUST method, less sensitive to extremes than least squares, or a transformation that brings the x values closer together. An outlier is often the richest observation in the data set: it signals a measurement error, a heterogeneous population, or a phenomenon the model ignores.
Exercise 5: Correlation and causation
This is the most important point of the chapter, and the only one you will keep meeting for the rest of your life. A correlation, however strong, never establishes on its own that one variable causes another.
In a large city, over twelve months, a correlation of r=0.95 is observed between monthly ice cream sales and the number of drownings.
a) Can we conclude that eating ice cream increases the risk of drowning?
b) List the possible competing explanations for any observed correlation.
c) Identify the confounding variable in this example and explain how it works.
d) How could one, in principle, establish causation rather than a mere association?
e) A strong correlation is observed between the number of firefighters sent to a fire and the damage recorded. Analyse it.
Show the solution
Answers
a)No: r measures how the variables vary together, never the mechanism.
b)Four: x causes y; y causes x; a third variable causes both; or chance.
c)Temperature. Heat sells ice cream AND sends people swimming: both series follow the same cause.
d)A controlled experiment with random assignment, which balances even the unknown confounders. Failing that, control the known variables statistically.
e)The confounder is the size of the fire. Causation runs from the seriousness of the fire to the dispatch of firefighters, and the real effect of the firefighters is to REDUCE the damage.
a)
NO. The coefficient only measures how closely the two series vary together; it contains no information about the mechanism linking them. Inferring causation from a high r is a reasoning error, whatever the value of the coefficient.
b)
Four explanations compete, and the data alone cannot decide between them. First, x causes y. Second, y causes x, that is, reverse causation. Third, a THIRD VARIABLE, called a confounding variable, causes both at the same time. Fourth, it may be a coincidence due to sampling chance, all the more plausible when the sample is small or when a large number of series have been compared until two were found that match.
c)
The confounding variable is TEMPERATURE, or more broadly the season. When it is hot, people buy more ice cream; and when it is hot, they swim more, which mechanically increases the number of occasions for drowning. The two series rise and fall together over the year because both are driven by the same cause, without any direct link between them. Removing all ice cream from the city would change nothing in the number of drownings.
d)
Through a CONTROLLED EXPERIMENT with RANDOM assignment: subjects are allocated at random to an exposed group and a control group, then the groups are compared. Randomization guarantees that the two groups differ, on average, only in the factor manipulated, including for the confounding variables nobody has identified, and that is its decisive advantage. When the experiment is impossible or unethical, as here, one falls back on observational studies in which the known variables are CONTROLLED statistically, for example by examining the correlation at constant temperature, which in our case would vanish.
e)
The analysis is of the same kind, but with an instructive twist. It would be absurd to conclude that the firefighters cause the damage. The confounding variable here is the INITIAL SIZE of the fire: a big fire causes both the dispatch of many firefighters and heavy damage. One can even add that there is indeed a causal link, but in the OPPOSITE direction to the naive reading: it is the seriousness of the fire that causes the firefighters to be sent. And the real effect of the firefighters on the damage is surely to REDUCE it, an effect the raw correlation completely hides. This is the textbook example of a positive correlation coexisting with a negative causal effect.
Part B: exam level (/50)
Exercise 6: A complete regression on real data
A synthesis exercise: carry out a regression from start to finish and write up its interpretation, limits included.
A study relates the floor area of an apartment, in square metres, to its monthly rent in Montreal, in dollars. For 8 apartments, the results are n=8, ∑x=440, ∑y=10400, ∑xy=608000, ∑x2=25800 and ∑y2=14000000.
a) Compute the means, then Sxy, Sxx and Syy.
b) Compute the correlation coefficient, then CHECK that it is valid before interpreting it. What do you conclude?
c) Find the equation of the regression line.
d) Interpret the slope in context, with its units.
e) Estimate the rent of a 70 m² apartment, then write the limits of your estimate in two sentences.
b)r=1.299, impossible: the sums given are incompatible. The check ∣r∣≤1 is made systematically.
c)b=22.5 and a=62.5, so y=62.5+22.5x.
d)22.50 dollars of rent per square metre. The intercept has no concrete meaning.
e)y^(70)=1637.50 dollars. Eight apartments and inconsistent sums on one side; neighbourhood, condition and floor left out of the model on the other.
a)
x=8440=55.0 m² and y=810400=1300 dollars. Next Sxy=608000−8440×10400=608000−572000=36000. Sxx=25800−84402=25800−24200=1600. Syy=14000000−8104002=14000000−13520000=480000.
b)
r=1600×48000036000=76800000036000=2771336000=1.299... which is IMPOSSIBLE, since a correlation coefficient can never exceed 1. Such a result necessarily signals an error in the calculation or in the data. Going over it again: 1600×480000=7.68×108=27713, and 2771336000=1.299. The sums given are therefore mutually incompatible, and the right reflex on an exam is to POINT THIS OUT rather than carry on. This check, ∣r∣≤1, must be made systematically: it catches the vast majority of data-entry errors in the working sums.
c)
The line can nevertheless be computed, since it depends only on Sxy and Sxx: b=160036000=22.5 and a=1300−22.5×55.0=1300−1237.5=62.5. The equation is y=62.5+22.5x. These two coefficients are consistent and plausible, which points to Syy, hence to ∑y2, as the source of the inconsistency.
d)
The slope is 22.5 dollars per square metre: each additional square metre is associated with an average increase in rent of 22.50 dollars a month. The intercept, 62.50 dollars, would be the rent of an apartment with zero floor area: it obviously has no concrete meaning and must certainly not be interpreted. It is only a fitting parameter, needed so that the line passes through the mean point.
e)
y^=62.5+22.5×70=62.5+1575=1637.50 dollars. Limits: on the one hand, this estimate rests on only 8 apartments and on sums shown to be inconsistent, which rules out any quantitative confidence until the data have been checked; on the other hand, floor area explains only part of the rent, which also depends on the neighbourhood, the condition, the floor and the year, so that a point prediction without a confidence interval gives a false impression of precision.
Exercise 7: Residual analysis: what the coefficient does not show
Go back to the six original students, (2,35), (4,48), (5,52), (7,68), (9,80), (11,95), and to their least-squares line y^=20.765+6.6687x. A residual is the vertical deviation ei=yi−yi^ between the data value and the prediction.
The graph below shows the data.
a) Compute the six predicted values and the six residuals. What is their sum?
b) Prove that the sum of the residuals is necessarily zero for a least-squares line. Does this property make it possible to judge the QUALITY of the model?
c) The residuals are plotted against x. Describe what a satisfactory residual plot looks like, then the three classic anomalies one looks for and what each one reveals.
d) Compute the standard error of the estimate se=n−2∑ei2 and interpret it. Check the relation ∑ei2=Syy(1−r2), given that Syy=2468 and r2=0.99706.
e) A study reports r=0.82, but its residual plot clearly forms a U. What do you conclude, and what should be done?
Show the solution
Answers
a)Predictions 34.10, 47.44, 54.11, 67.45, 80.78, 94.12. Residuals +0.90, +0.56, −2.11, +0.55, −0.78, +0.88, with a sum of zero.
b)∑ei=n(y−a−bx)=0 by construction: no information about the quality of the model.
c)A good plot has no structure. Three anomalies: U-shaped curvature (nonlinearity), funnel (heteroscedasticity), isolated point (outlier or leverage).
d)∑ei2≈7.26, se=1.815≈1.35 points, and Syy(1−r2)≈7.26: identical.
e)The relationship is not linear despite r=0.82. Switch to a quadratic model or linearize, then check the residuals again.
a)
Predicted values: y^(2)=34.10, y^(4)=47.44, y^(5)=54.11, y^(7)=67.45, y^(9)=80.78 and y^(11)=94.12. Corresponding residuals: +0.90, +0.56, −2.11, +0.55, −0.78 and +0.88. Their sum is 0, up to rounding.
b)
The least-squares line always passes through the mean point: y=a+bx. Now ∑ei=∑(yi−a−bxi)=∑yi−na−b∑xi=ny−na−nbx=n(y−a−bx)=0. The sum is therefore zero by CONSTRUCTION, whatever the data, even absurd ones. It says strictly nothing about the quality of the model: it is a check on the calculation, not a diagnosis. To measure the fit, you must look at the sum of the SQUARES of the residuals, which does not vanish.
c)
A satisfactory residual plot shows NO structure: a horizontal cloud, scattered at random on both sides of zero, of constant thickness, with no trend and no pattern. Any visible shape is a defect of the model. First anomaly, a U-shaped or bell-shaped CURVATURE: the relationship is not linear, and the line passes systematically above and then below the points. Second anomaly, a FUNNEL that widens: the spread increases with x, which is called heteroscedasticity; the line remains acceptable but the prediction intervals are worthless, because they assume a constant spread. Third anomaly, an ISOLATED point very far from the others: an outlier or a leverage point, whose influence must be examined separately.
d)
∑ei2≈(0.90)2+(0.56)2+(2.11)2+(0.55)2+(0.78)2+(0.88)2≈7.26. So se=6−27.26=1.815≈1.35 points. Interpretation: the actual grades typically deviate from the line by about 1.3 points, which gives the order of magnitude of the error to expect from a prediction. We divide by n−2 and not by n because two parameters, the slope and the intercept, have already been estimated from these same data. Check: Syy(1−r2)=(2468)(1−0.99706)=(2468)(0.00294)≈7.26, identical. This relation also sheds light on the meaning of r2: it is exactly the share of Syy that the line has managed to absorb.
e)
An r of 0.82 looks excellent, but the U in the residual plot proves that the relationship is NOT linear: the line underestimates at both ends and overestimates in the middle, or the reverse, and this error is systematic, not random. A high correlation coefficient never guarantees linearity; it only measures how well a LINE fits, even when a curve would fit far better. Predictions made with this model will be biased in a predictable way, which is the worst of defects. The linear model must therefore be dropped in favour of a quadratic model, or the data linearized by a transformation, logarithm, square root or reciprocal, depending on the shape observed, then you check that the new residual plot no longer shows anything.
Exercise 8: Linearizing: when the relationship is not a straight line
A bacterial culture is counted every hour. At t=0, 1, 2, 3 and 4 hours, the counts are 50, 82, 135, 220 and 361 thousand cells respectively. Exponential growth is suspected, N=N0ekt.
The graph below shows the data.
a) Someone computes the correlation coefficient between t and N without thinking, and finds r≈0.962. Why is this figure misleading? What would the residual plot show?
b) Compute lnN at each time and examine the successive differences. What do they confirm?
c) Carry out the regression of lnN on t: give the slope and the intercept, then work back to k and N0 and write the model.
d) Compute the doubling time of the population, then predict the count at t=6 hours.
e) Another study relates the orbital period of a planet to its distance from the Sun, using a model of the form y=axb. Which transformation must then be applied, and how are a and b read from the resulting line?
Show the solution
Answers
a)r measures how well a line fits; it assumes linearity. The residuals would form a clear U.
b)3.912, 4.407, 4.905, 5.394, 5.889: differences 0.495, 0.499, 0.488, 0.495, hence multiplication by a constant factor.
c)Slope k≈0.494 h−1, intercept lnN0≈3.913, so N0≈50.1 and N≈50.1e0.494t.
d)t=kln2≈1.40 h, and N(6)≈970 thousand cells, outside the observed range.
e)Log-log: lny=lna+blnx. The slope gives b, the intercept gives lna. For the planets, the slope is 1.5, which is Kepler's third law.
a)
An r of 0.962 looks excellent, but it only measures how well a LINE fits. Yet the population more than septuples in four hours, and it accelerates: the scatter plot clearly curves upwards. A line can roughly follow an increasing curve and earn a high coefficient while being systematically wrong. The residual plot would show it at once, forming a very clear U: the line would pass below the points at both ends and above them in the middle. The correlation coefficient never tests linearity, it ASSUMES it.
b)
ln50≈3.912, ln82≈4.407, ln135≈4.905, ln220≈5.394, ln361≈5.889. Successive differences: 0.495, 0.499, 0.488 and 0.495, remarkably constant. Now a constant increase in the logarithm every hour means a MULTIPLICATION by a constant factor every hour, which is the very definition of exponential growth. The model is therefore confirmed, and on a semi-logarithmic scale the five points line up.
c)
With ∑t=10, ∑lnN≈24.507, ∑tlnN≈53.954 and ∑t2=30: Stt=30−5100=10 and St,lnN=53.954−5(10)(24.507)≈4.941. Slope: 104.941≈0.494; intercept: 524.507−(0.494)(2)≈3.913. Taking the logarithm of the model, lnN=lnN0+kt: the slope IS k≈0.494 h−1 and the intercept is lnN0, so N0=e3.913≈50.1 thousand. Model: N≈50.1e0.494t. The correlation coefficient on the transformed data exceeds 0.9999.
d)
Doubling time: we want t such that ekt=2, that is, t=kln2=0.4940.693≈1.40 h, about 1 h 24 min, which is consistent with the data since the population goes from 50 to about 100 in a little under an hour and a half. Prediction at t=6 h: N=50.1e(0.494)(6)=50.1e2.964≈9.7×102 thousand cells. Caution, however: t=6 lies outside the observed range, and a real culture eventually saturates when nutrients run out. The exponential model is almost always valid at the start and wrong at the end.
e)
For y=axb, take the logarithm of BOTH variables: lny=lna+blnx. So regress lny on lnx, on a log-log scale. The SLOPE of the resulting line gives the exponent b directly, and its intercept gives lna, hence a=eintercept. Remember the distinction: an EXPONENTIAL model is linearized on a semi-log scale, by transforming only y; a POWER model is linearized on a log-log scale, by transforming both. For the planets, the slope would come out very close to 1.5, which gives back Kepler's third law, T2∝d3.
N against t: the points curve upwards, and the line passes below them at both ends and above them in the middle despite r≈0.962.
lnN against t: the five points line up, slope k≈0.494, intercept lnN0≈3.913.
Exercise 9: The significance test for the correlation coefficient
Even with no real relationship at all, a few points drawn at random can display a high correlation coefficient. We therefore test H0:ρ=0 against H1:ρ=0 using t=1−r2rn−2, on n−2 degrees of freedom. Two-tailed critical values at the 5% level: 2.776 for 4 degrees of freedom, 2.306 for 8, 2.048 for 28 and 1.962 for 998.
a) Why must a correlation coefficient be tested? What exactly does ρ mean, and how does it differ from r?
b) For the six students, r=0.9985. Compute the test statistic and conclude at the 5% level.
c) A study of 10 individuals obtains r=0.50. Compute t and conclude. Does the result surprise you?
d) Another study obtains exactly the same r=0.50, but for 30 individuals. Conclude, and explain how the same coefficient can lead to two opposite verdicts.
e) A survey of 1000 people finds r=0.070. Test it, then compute r2. What lesson do you draw, and which other chapter of the course does it remind you of?
Show the solution
Answers
a)ρ is the coefficient of the population, fixed and unknown; r is only an estimate of it, which varies from one sample to another.
b)t≈36.5 against 2.776: reject H0 without hesitation.
c)t≈1.63 against 2.306: do not reject. With ten individuals, r=0.50 proves nothing.
d)t≈3.06 against 2.048: reject. Significance measures how solid the evidence is, not the strength of the relationship.
e)t≈2.22>1.962: significant. But r2=0.0049, that is, 0.5% explained. Significant and important are independent.
a)
Because r is computed on a SAMPLE and varies from one sample to another. The parameter ρ is the correlation coefficient of the whole POPULATION, a fixed and unknown value; r is only an estimate of it. With very few points, chance is enough to produce an impressive r: three points drawn at random almost always line up fairly well. The test therefore answers the only question that matters: is the observed association stronger than what chance would commonly produce if the two variables really had no link at all?
b)
t=1−0.99852(0.9985)4=0.00299781.997=0.054751.997≈36.5. The critical value for 4 degrees of freedom is 2.776: the statistic exceeds it by a factor of thirteen. We reject H0 without hesitation; the correlation is highly significant despite having only six points, because it is almost perfect.
c)
t=1−0.25(0.50)8=0.8661.414≈1.63, to be compared with 2.306 for 8 degrees of freedom. We do NOT reject H0: with ten individuals, a coefficient of 0.50 is entirely compatible with no relationship at all. The result often surprises, because an r of 0.50 is instinctively judged “moderately strong”. It is not, here: on so few points it proves nothing at all, and announcing it as a discovery would be a mistake.
d)
t=0.866(0.50)28=0.8662.646≈3.06, greater than the critical value 2.048 for 28 degrees of freedom: this time we REJECT H0. The same coefficient therefore leads to two opposite conclusions, and there is no contradiction in that: significance does not measure the STRENGTH of the relationship, which r already describes, but how solid the EVIDENCE is, which depends on the amount of information available. Thirty observations make improbable what ten made commonplace. All of inferential statistics lies in this distinction.
e)
t=1−0.0049(0.070)998=0.99752.211≈2.22, greater than 1.962: the coefficient is significantly different from zero at the 5% level. Yet r2=0.0049, barely 0.5% of the variation explained: next to nothing. The lesson is that statistical significance says only one thing, that the effect is probably not exactly zero, and that with a large enough sample absolutely any tiny effect ends up being significant. It says nothing about the IMPORTANCE of that effect, which only r2, the slope and the context can judge. It is exactly the same lesson as in the chapter on hypothesis tests, where a gap of 0.5 g on 500 g bags became significant simply by increasing the sample size: significant and important are two independent notions.
Exercise 10: Problem: reading a study and spotting what is wrong
Five claims taken from newspaper articles, all based on a regression that was actually carried out. Each one contains a specific reasoning error, which you must name.
For each statement, identify the error and state the criticism in one or two sentences.
a) “Children who have more books at home do better at school; so books should be given to families.”
b) “The correlation between a country's chocolate consumption and its number of Nobel prizes per capita is 0.79: eating chocolate would make you smarter.”
c) “Our model has an r2 of 0.98, so it is reliable for predicting sales over the next ten years.”
d) “Among hospitalized patients, smoking is associated with better survival: tobacco would protect against the virus.”
e) “No correlation was found between the dose of the drug and the effect observed; the drug is therefore ineffective.”
Show the solution
Answers
a)Correlation taken for causation; the confounder is socio-economic background.
b)The same error, plus the ecological fallacy: the correlation is between countries, and the confounder is wealth.
c)Improper extrapolation: a high r2 describes the past within the observed range, not ten years ahead.
d)Selection bias, Berkson's paradox: the analysis is restricted to hospitalized patients.
e)No LINEAR correlation taken for no relationship. The scatter plot must be drawn and a nonlinear model considered.
a)
Correlation confused with causation, with an obvious confounding variable: the SOCIO-ECONOMIC BACKGROUND and the parents' level of education. These factors produce both the presence of books and success at school. Giving books to families who are otherwise unchanged would therefore not necessarily have the hoped-for effect, as several randomized trials have shown. The practical conclusion does not follow from the observation.
b)
The same error, on an even more problematic unit of analysis: the correlation is computed on COUNTRIES, not on individuals, which rules out any conclusion about individuals. This is the ecological fallacy. The confounding variable here is the WEALTH of the country, which determines both the consumption of luxury products and investment in research. On top of that comes a selection problem: this correlation is famous because it was searched for among hundreds of possible variables, which guarantees finding a few strong ones by pure chance.
c)
Improper extrapolation. A high r2 measures how well the model fits PAST data, within the observed range; it says nothing about its validity beyond. Predicting ten years ahead assumes that the relationship stays linear and that conditions do not change, assumptions that no historical data can guarantee. A model can have a perfect fit and zero predictive value outside its domain.
d)
SELECTION bias, because the analysis is restricted to HOSPITALIZED patients. To be hospitalized, a non-smoker generally has to be more seriously ill than a smoker, since smoking is often enough on its own to justify admission. The groups compared are therefore not comparable, and the association observed is an artifact of the selection criterion, not an effect of tobacco. This is Berkson's paradox, and it really did produce headlines of this kind.
e)
No LINEAR correlation confused with no relationship. A dose-response relationship is very often NONLINEAR: no effect below a threshold, rapid growth after it, then a plateau, or even toxicity that reverses the trend at high doses. On such a bell-shaped or S-shaped curve, the linear correlation coefficient can be close to zero while the relationship is strong and fully deterministic. The correct conclusion is that the scatter plot must be DRAWN and a nonlinear model considered, not that the drug is ineffective.
Something missing from this set?A type of exercise your teacher gives, a problem you are stuck on, a mistake you spotted: tell me, I read every request.
Sending goes through your free space: that is how I can answer you and tell you when the exercise is added. An email is enough, no password. Create your space.
Rather not create an account? Write to me directly.
Get in touch for a first session. Regression is the most useful chapter of 201-337 for the rest of your studies: we work on the calculations and on the interpretation in equal measure.