Mental Models204 cards · 12 fields

Mental Models/Mathematics & Statistics

Linear Regression

线性回归

One straight-line formula shows how much each factor moves the result.

What is a bottle of Bordeaux worth? Traditionally you had to wait for expert tasters to open it, taste it and give the vintage a score. Orley Ashenfelter, an economist at Princeton, said there was no need to taste it. Look only at three weather numbers from that year (winter rainfall, the average temperature during the grape-growing season, and rainfall at harvest time), add how many years the wine has been stored, and a single formula can estimate its auction price.

The formula multiplies each number by a weight and adds them up. He had weather records and auction prices for several dozen vintages, and he used these old records to set the weights: whichever set of weights gave prices closest to the real sale prices was the set he used. The direction they showed was clear: the warmer the growing season and the drier the harvest, the more expensive the wine.

In March 1990 The New York Times reported on the formula, and the wine experts were not pleased; in 1995 he and two co-authors published it formally. Before the bottle was even opened, the judgment the experts had spent years training was already worked out from a weather table and a few weights.

You're looking for a flat to rent, and you have a rule in your head: a base of 1,500 yuan, plus 40 yuan for each square meter, minus 30 yuan for every 100 meters farther from the subway. By this rule, a 60-square-meter flat 300 meters from the subway rents for 3,810 yuan a month. One the same size 500 meters from the subway is 3,750 yuan, and the 60-yuan difference comes only from the extra 200 meters.

The 40 yuan per square meter can be found from the rents flats actually went for. Suppose you have twenty flats that were just rented, all 300 meters from the subway. Then the differences in their rents depend only on size.

First guess 30 yuan per square meter and use that rule to estimate the twenty. Every square meter is undercounted, so the bigger the flat, the more it falls short, and the big flats all come out too low. Change it to 50 yuan, and now the big flats come out too high. Change it to 40, and some come out high and some low, and the misses are smallest overall. Several factors, each with its own weight, are added up to give the result, and the weights are set at the values that make the misses smallest: that is linear regression.

About 40 yuan per m² fits the twenty best
Avg miss per flat534 yuan
200030004000500030405060Size (m²)Rent (yuan)Actual rentYour rule
200030004000500030405060Size (m²)Rent (yuan)Actual rentYour rule

20 flats just rented, all 300 m from the subway. Drag "Yuan per m²" to see the line and the average miss per flat. The smallest miss is the weight regression fits.

The weights are the set that makes the total miss smallest

Use one rule to estimate twenty flats, and each estimate will be a little high or a little low. Square each flat's miss and add them up, and you have the rule's total miss:

is the actual rent of flat and is the rule's estimate. The smaller is, the closer the rule is to the real rents. You square before adding because if you simply added, an estimate 200 yuan too high and one 200 yuan too low would cancel out to zero. Squaring also magnifies big errors: a miss of 600 yuan, squared, is nine times a miss of 200 squared. So to make small, the weights bend first toward the few flats that are furthest off.

Back to the rule that guessed 30 yuan: it undercounts each square meter by 10 yuan, so the 68-square-meter flat comes out 680 yuan lower than it would at 40 yuan. Regression doesn't need to try one setting after another like this. It solves directly for the set of weights that makes smallest, a method called least squares. In a regression table, the fitted weights are called coefficients.

Add a variable, and the coefficient already there changes

Suppose you get hold of a company's payroll. Fit it with years of service alone, and each extra year adds 900 yuan to monthly pay. Put in "has a master's degree" as well, and the years-of-service term drops to 400 yuan a year, while the master's term is 3,000 yuan.

In its early years the company hired only people with master's degrees, so many of the long-serving staff have one. Looking at years of service alone, part of the higher pay of long-serving staff came from their degrees, but it was credited to years of service; put the degree in, and that part goes back to the degree. Write monthly pay as and years of service as , and let for a master's degree and for a bachelor's. The fitted formula is:

With the same degree, doesn't change, and then one year's difference in service makes only 400 yuan of difference in monthly pay. With the same years of service, someone with a master's earns 3,000 yuan more than someone with a bachelor's.

When two variables rise and fall almost together, their separate coefficients can't be read reliably

Fit body weight on left foot length and right foot length together. If someone's left foot is a centimeter longer, their right foot is almost always about a centimeter longer too. So as long as the two coefficients add up to the right total, however that total is split between left and right, the estimated weights come out about the same. As a result, one group of people may give the left foot a big coefficient and the right foot a small one, and another group may give the exact opposite, while the two added together barely move.

Linear regression works best when the factors don't interfere with one another, when a little more of each one adds a little more to the result, and when what you want to know is exactly how much the result changes when one factor alone goes up by one unit. Dose and body weight do pull on each other: the same dose acts differently on a light person and a heavy one, so a single weight for dose can't get it right.

Ranking factors by the size of their coefficients goes wrong, because a coefficient follows its unit. Change one factor's unit from meters to millimeters and its coefficient shrinks a thousandfold, while what the factor does hasn't changed at all. The formula has also only been tested within the range the data covers. Fit the heights of children aged three to ten and you get six centimeters of growth a year; carry that on to age thirty, and a person would be 2.5 meters tall.

Explain
Break down the gap in average pay between male and female staff: put in job level and years of service, and see what share of the gap is still left in the gender term.
Design
Score loan applications: income, debt ratio and number of late payments each get a weight, fitted from past repayment records, and the loan officer just adds them up to get the score.
Predict
Estimate how many lunches the canteen should prepare next week: temperature, whether it's a Monday and whether it's payday each get a term. Plug in next week's values and you get a specific number of meals.
  1. With which other variables held the same is this coefficient being read? Pull up the list of variables in the regression table, take out the one most closely related to it, fit again, and see how much it moves.
  2. Are there two variables that rise and fall almost together? Work out their correlation coefficient. The more closely they move together, the less you can read either coefficient on its own.
  3. What unit is the coefficient in? Before comparing two terms, put them in the same units, then see which is bigger.

Sports

What a guard's age is worth in points

You do data analysis for a basketball club, and a scout wants to know whether a young guard is worth signing. You take past guards' points per game in the following season and fit them on this season's minutes played, shooting percentage and age together. Suppose the age term is 0.4 points fewer for each year older: with the same minutes and shooting percentage, a 22-year-old guard is expected to score 2.4 points more than a 28-year-old. The scout takes this number into the price talks with the player's agent.

Economics

One more year of school

In 1974 Jacob Mincer put wages, years of schooling and years of work experience into one formula. He worked out differences in wages not in dollars but in percentages, so the schooling term can be read directly: for two people with the same work experience, one more year of schooling means wages higher by that percentage on average. Later, labor economists used the same kind of formula to estimate the return to education in country after country. A 2018 review gathered more than a thousand estimates from 139 countries: on average, one more year of schooling meant wages about 9% higher.

Personal life

The month the power bill ran over

You fit the last two years of monthly electricity bills on the month's average temperature and the number of days the whole family was at home. The formula says July should cost 410 yuan (an assumed figure), but the bill says 560. You dig out that month's records and find the water heater's timer was left on. Once you turn it off, the next month is back to around 420 yuan.

A food-delivery platform fit a formula on last month's orders: delivery minutes = 12 + 4.5 × distance (km) + 7 × rain (1 on a rainy day, otherwise 0). Last month, rainy-day orders were on average 2 km farther than sunny-day orders.

Last month, how many minutes longer did rainy-day orders take on average than sunny-day ones?

An online shop owner fit a formula on 24 months of data: with monthly ad spending between 2,000 and 10,000 yuan, each extra 1,000 yuan brings on average 5,000 yuan more in monthly sales, and the dots sit close to the line. He plans to spend 60,000 yuan next month, and by the formula he expects sales 250,000 yuan higher than at 10,000 yuan.

What is the biggest flaw in this prediction?

You fit a pricing formula for second-hand flats and put in both "gross floor area" (which includes a share of the building's common areas) and "usable floor area". In this batch, the usable area is almost always 80% of the gross area, so the two rise and fall together. The fit says each extra square meter of gross area adds 60,000 yuan to the price, while each extra square meter of usable area takes 20,000 yuan off. The estate agent is puzzled: the bigger the usable area, the cheaper the flat?

How should this minus sign be read?