Check · Test yourselfPractice tests

Academy › Topic library › Going Deeper: The Math of Correlation and Linear Regression

Q-BOT ACADEMY · FREE VIDEO LESSON · 13 MIN

Going Deeper: The Math of Correlation and Linear Regression

Because any handful of dots will give you a line, even dots that are pure noise. Before you change a setting, you need three answers. Is the slope real? How far off could a prediction be? And does a straight line even fit?

12:34 · Q-Bot · narrated, with worked example and a check-yourself question

In short

Everything starts from S X X, S Y Y and S X Y. The slope is S X Y over S X X. The variation splits into explained plus error, and R squared is the explained share. s is the square root of the error sum of squares over n minus two. Test the slope with t, on n minus two degrees of freedom. Use the prediction interval for single items, stay inside your data, and always check the residuals!

Check yourself

Ellen tracks twelve months of slat deliveries. The correlation between the slats' moisture at receiving and the warped slats returned is r equals point six. At alpha point oh five, is it significant? A: t is about two point three seven, more than two point two two eight, so yes. B: t is about two point three seven, so no. C: no test is needed; r is under point eight, so it's weak. Or D: R squared is point three six, so moisture causes thirty six percent of the warping.

Show the answer

The answer is A. t equals point six times the square root of ten, over the square root of one minus point three six. That's about one point nine over point eight: two point three seven. With ten degrees of freedom, the critical value is two point two two eight, so reject the null; the p-value is about point oh four. B misread the comparison. C skipped the test. And D turned R squared into a cause; it's only a share of variation explained. That's the math of correlation and linear regression. Questions? Ask Q-Bot right here in the Academy. And one for the road: why did the regression line never worry about the future? It knew better than to extrapolate!

Where this topic is taught

This lesson is part of every free Academy course whose Body of Knowledge covers it. Each course puts it in context, with exercises and checks.

Full lesson transcript

The narration from the video, by chapter. Brindlewood Outdoor Furniture and its people are a fictional company used in every Q-Bot Academy lesson.

Intro

Brindlewood, a Tuesday afternoon. Lou drops a handful of used contact tips on Rosa's desk. We change these on every welding gun after thirty two hours of welding. Could we stretch it to sixty? Rosa turns to Marcus. Does a weld really get weaker as the tip wears? And if it does, how sure are we? Marcus grins. Last time a straight line told us how much. Today it tells us how sure!

Hey there, I'm Q-Bot. Welcome to the Q-Bot Academy. Today we're going deeper: the math of correlation and linear regression! We'll build the line from three sums, split the variation, test the slope, put intervals around a prediction, and check the residuals. If correlation and regression are new to you, start with the correlation and regression video.

Why go past the line?

Why go past the line itself? Because any handful of dots will give you a line, even dots that are pure noise. Before you change a setting, you need three answers. Is the slope real? How far off could a prediction be? And does a straight line even fit?

The three sums

Here are the building blocks. For every point, take how far X sits from its average, and how far Y sits from its average. S X X adds up the X distances squared. S Y Y adds up the Y distances squared. And S X Y adds up the X distance times the Y distance. Everything in this video is built from these three sums, the averages, and the number of points, n.

Now the familiar results. The correlation, r, equals S X Y divided by the square root of S X X times S Y Y. The slope, b one, equals S X Y over S X X. And the intercept, b zero, is the average Y minus the slope times the average X. For Marcus's gun distance study from the earlier video, S X X is forty, S Y Y is two point five four, and S X Y is minus ten. So r is minus point nine nine, and the slope is minus a quarter of a mil per inch.

Splitting the variation

Next, split the variation. The total is S Y Y. The part the line explains, the regression sum of squares, is S X Y squared over S X X. What's left is the error sum of squares, the residuals squared and added up. For the gun study, two point five four in total equals two point five explained, plus point oh four left over. R squared is the explained share: about point nine eight.

From the leftovers comes the standard error of the estimate, s: the square root of the error sum of squares over n minus two. Why minus two? Because the line used up two numbers, the slope and the intercept. For the gun study, point oh four over three, square root, is about point one one five mils. That's the typical scatter of a frame around the line.

Is the slope real?

Now the first question: is the slope real? The null hypothesis says the true slope is zero, so X tells you nothing about Y. The standard error of the slope is s over the square root of S X X. Then t equals the slope over its standard error, with n minus two degrees of freedom. Bad: r looks big, so it must be real. Good: run the t test, because with only a few points, a big r can come from luck.

Testing the correlation gives the same answer. t equals r, times the square root of n minus two, over the square root of one minus r squared. With one X, that's exactly the slope's t. And the F in a regression ANOVA table is just that t squared. Three doors, one room!

Worked example 1: the gun study

Worked example one: the gun study. The standard error of the slope is point one one five over the square root of forty, about point oh one eight three. t is minus point two five over point oh one eight three, about minus thirteen point seven, with three degrees of freedom. The critical value from a t table, or the spreadsheet's two-tailed t inverse, is three point one eight two. Thirteen point seven is far past it, and the p-value is about point oh oh oh eight. Reject the null: gun distance really does drive thickness!

How big is the effect? The confidence interval for the slope is b one, plus or minus the critical t times its standard error. Three point one eight two times point oh one eight three is about point oh five eight, so the slope runs from about minus point three one to minus point one nine mils per inch. Ninety five percent describes the method: intervals built this way catch the true slope ninety five times in a hundred. This one doesn't include zero, which matches the test.

Confidence or prediction interval?

Now the second question: how far off could a prediction be? At a chosen X, the line gives Y hat, with two different intervals. The confidence interval for the mean says where the average Y sits at that X. The prediction interval says where one single new item will land. It adds a one under the square root, for the scatter of a single item, so it's always wider. Both get wider the farther X sits from its average.

Which one do you use? Bad: judge a single weld against the narrow interval for the average. Good: use the prediction interval for single items, and the confidence interval only when the question really is about the average.

Worked example 2: Lou's contact tips

Worked example two: Lou's contact tips. Marcus and Lou weld ten Harbor chair frames on one station, with tips that have zero, four, eight, and so on, up to thirty six hours of welding on them. Priya pull-tests every frame. The strengths run from fifteen hundred five pounds force with a fresh tip, down to thirteen ninety two at thirty six hours.

The sums. The averages are eighteen hours and fourteen fifty two pounds force. S X X is thirteen twenty, S X Y is minus thirty nine sixty, and S Y Y is twelve thousand four hundred eight. The slope is minus thirty nine sixty over thirteen twenty: exactly minus three pounds force per hour of tip wear. The intercept is fourteen fifty two plus three times eighteen, fifteen oh six. r is minus point nine eight, and R squared is about point nine six.

Now the test. The error sum of squares is five twenty eight, and over eight degrees of freedom that's sixty six, so s is about eight point one. The standard error of the slope is eight point one over the square root of thirteen twenty, about point two two four. t is about minus thirteen point four, against a critical value of two point three oh six. Reject: tip wear really does weaken the weld! The slope's confidence interval runs from about minus three point five to minus two point five pounds force per hour.

Now Rosa's question at the current schedule, thirty two hours. The line predicts fifteen oh six minus three times thirty two: fourteen ten. The confidence interval for the mean runs from about fourteen oh one to fourteen nineteen. But Rosa cares about every single weld, so she wants the prediction interval: about thirteen eighty nine to fourteen thirty one. Even the low end is nearly a hundred ninety pounds force above the twelve hundred minimum!

And sixty hours? The line says thirteen twenty six. But the study only went to thirty six hours, and a worn tip may not wear in a straight line forever. The math can't answer that; only data can. So the schedule stays at thirty two hours for now, while Lou and Priya run tips out to sixty hours on test frames. If the line holds, Lou gets his longer tip life, with numbers behind it!

Does a straight line fit?

The third question: does a straight line even fit? Plot the residuals, the actual minus the predicted. Four checks. They should bounce randomly around zero, with no curve. Their spread should stay even, with no funnel. They should look roughly normal, on a normal probability plot. And in time order, they should show no pattern. Also look for a single point far off, or far out on X, pulling the line toward itself.

For Lou's tips, the residuals run from minus eight to plus thirteen, scattered around zero with no shape. The line fits. Bad: a curve or a funnel in the residuals, and you trust the line anyway. Good: a curve says try a squared term; a funnel says the spread isn't constant, so try a transformation, like a log, before you trust any interval.

More than one X

What about more than one X? That's multiple regression. Each coefficient is the change in Y for one unit of its X, holding the other X's fixed, and each gets its own t test. Use adjusted R squared, which only goes up if a new X earns its place. And watch for X's that move together, called multicollinearity: it makes the coefficients jumpy and hard to trust.

In a spreadsheet and in software

In a spreadsheet, slope, intercept, correl and r s q give the basics, and S T E Y X gives s. The line estimate function gives the slope, the intercept and their standard errors in one go, and the two-tailed t inverse gives the critical t. Any statistics software prints the whole table, plus the residuals to plot.

Common mistakes

Common mistakes. One: trusting a big r from a few points without a test. Two: dividing by n minus one instead of n minus two for s. Three: judging a single item with the confidence interval for the mean. Four: predicting outside the range you studied. Five: skipping the residual plots. And six: calling a significant slope proof of cause. Only a planned study, where you set X yourself, like Lou's tips, can show that.

Recap

Let's recap. Everything starts from S X X, S Y Y and S X Y. The slope is S X Y over S X X. The variation splits into explained plus error, and R squared is the explained share. s is the square root of the error sum of squares over n minus two. Test the slope with t, on n minus two degrees of freedom. Use the prediction interval for single items, stay inside your data, and always check the residuals!

Check yourself

Now, check yourself. Ellen tracks twelve months of slat deliveries. The correlation between the slats' moisture at receiving and the warped slats returned is r equals point six. At alpha point oh five, is it significant? A: t is about two point three seven, more than two point two two eight, so yes. B: t is about two point three seven, so no. C: no test is needed; r is under point eight, so it's weak. Or D: R squared is point three six, so moisture causes thirty six percent of the warping. Pause the video if you want to think about it.

The answer is A. t equals point six times the square root of ten, over the square root of one minus point three six. That's about one point nine over point eight: two point three seven. With ten degrees of freedom, the critical value is two point two two eight, so reject the null; the p-value is about point oh four. B misread the comparison. C skipped the test. And D turned R squared into a cause; it's only a share of variation explained.

That's the math of correlation and linear regression. Questions? Ask Q-Bot right here in the Academy. And one for the road: why did the regression line never worry about the future? It knew better than to extrapolate!

Independent study material; not affiliated with or endorsed by ASQ. Body of Knowledge codes identify where the topic appears in each certification's published outline.