A Line That Predicts Earnings
Guess, fit a straight line in R, read its two numbers, then ask how good it is.
The lalonde data (Matching package): 445 men in a job-training experiment
Six of the 445 rows. Earnings are in dollars.
- age
- age in years
- educ
- years of schooling
- black, hisp
- 1 = Black / Hispanic, 0 = not
- married
- 1 = married, 0 = not
- nodegr
- 1 = no high-school diploma, 0 = has one
- re74, re75, re78
- real earnings in 1974, 1975 and 1978
- u74, u75
- 1 = no earnings that year, 0 = some
- treat
- 1 = in the job-training program in 1976, 0 = not
1. What would you predict, and from what?
A model can predict one column from other columns. The column you predict is the dependent variable; the columns you predict it from are the independent variables.
As a group: which column would you most like to predict, and which columns would you use? Name the dependent and the independent variables, and say why those independent variables might help.
Points to compare
- The dependent variable is the one you want to know; the independent ones are what you already know when you predict.
- Time order helps: 1974 and 1975 earnings are known before 1978, so they can predict
re78. A model can't use information that arrives after the thing it predicts. - The study itself compared
re78between men withtreat= 1 andtreat= 0, to see whether training raised earnings.
Earnings in 1975 against earnings in 1974, one dot per man
Shaded band: men who earned $9,000 to $11,000 in 1974 (orange rings).
2. Guess first
Before any model: some men in these data earned about $10,000 in 1974.
What did they typically earn in 1975?
Answer: clearly less. Of the 11 men who earned $9,000–$11,000 in 1974, 10 earned less in 1975; the middle one earned $4,879.
1974 earnings tell you something about 1975: on the chart, men further right tend to sit higher. But not everything: for these 11 men, a good 1974 was usually followed by a worse 1975. A useful prediction has to capture both. Step 3 does it with a straight line.
The line that predicts 1975 earnings from 1974 earnings
3. Fit the line in R
In the code cell, the first two lines of code fit the line (you'll learn how lm works later) and print its two numbers.
- Press Run. R prints two numbers, one under
(Intercept)and one underre74. - Type the first number where the first
NAis, and the second number where the secondNAis. Rounding to 3 decimal places is fine. - Press Run again. The last lines use your two numbers to predict 1975 earnings for a man who earned $10,000 in 1974.
Setup code (runs by itself)
Hint 1
Only the twoNAs change. After your edit the line should look like b0 <- 123.4, with R's number in place of 123.4.Hint 2
The output of the first Run shows(Intercept) and re74, with a number under each. The first goes in b0, the second in b1. Leave out commas and dollar signs.Hint 3
The first number is about 568 and the second is a little under 0.4. Type them with all the decimals R shows, or rounded to 3 places.Then: what do the two numbers mean?
As a group: what does the line predict for a man who earned $0 in 1974? Why might R call that number the “intercept”? If that's the intercept, where is the slope, and what does it mean in dollars?
Points to compare
- At
re74= 0 the prediction isb0≈ $568: the height where the line crosses (intercepts) the vertical axis. - The slope is
b1≈ 0.385: each extra $1,000 earned in 1974 adds about $385 to the predicted 1975 earnings. Move the dial by $1,000 and watch the prediction change by that much, wherever you start. - 326 of the 445 men earned $0 in 1974, and their average in 1975 was $304, not $568. One straight line has to serve every man, so it isn't exactly right even where the data are thickest.
Model 1, written out
re75 = 568 + 0.385 × re74
Rounded from what R printed (567.95 and 0.38491); the worksheet rounds the slope to 0.38.
4. Variables and parameters
A model has two kinds of quantity: variables, which change from man to man, and parameters, fixed numbers that are the same for everyone, estimated by lm from the data.
In model 1, which are the parameters?
A different model: exam score = 40 + 5 × hours studied. Which description is right?
Model 1 and the 445 men
5. Is it any good?
Someone says: “This is such a simple model. Is it accurate? Is it good? Is it useful?” These are reasonable questions.
As a group: how could you begin to answer them? Name at least one thing you would compute or look at, and what result would make you trust the model more.
Points to compare
- Compare predictions with what happened. For each man, the miss (or error) is his actual 1975 earnings minus the line's prediction. Small misses mean an accurate model.
- Compare with a simpler guess. Without
re74, the best single guess for everyone is the average ofre75. A model is useful only if it misses by less than that. On its own data a least-squares line always does at least as well as the average, so the question is by how much. - Try it on data it hasn't seen. A model can fit its own data well and still predict new men badly.
- Ask “useful for what?” A miss of $2,000 may be fine for planning a program's budget and useless for deciding about one person.
Step 6 computes the first two.
6. How far off is the line?
Each man's miss is his real 1975 earnings minus the line's prediction. The RMSE (root mean squared error) turns 445 misses into one typical miss, in dollars. Before you run anything: compared with simply guessing the average for every man, the line's typical miss will be…
The code cell already computes the line's RMSE (rmse_model). Your job is the one NA:
- Look at the line that makes
rmse_model. The line that makesrmse_meanis the same, withNAwhere the misses go. - Replace that
NAwitherr_mean: the misses when you guess the average for everyone. - Press Run. R prints both typical misses, side by side.
Hint 1
Only one word changes: theNA in the rmse_mean line. Everything else stays.Hint 2
After your edit the line readsrmse_mean <- sqrt(mean(err_mean^2)), just like the rmse_model line above it with err_mean in place of err.Then: what do your two numbers say?
Compare your two RMSEs. Which conclusion fits?
What would you tell someone using this line?
As a group: a job-training office uses your line to tell a client, “You'll probably earn about $X next year.” In two sentences, what does a typical miss (RMSE) of about $2,400 mean for that client?
Model 1, from steps 3 and 6
re75 = 568 + 0.385 × re74
Typical miss: about $2,378 with the line, $3,147 guessing the average for everyone.
7. Where else?
As a group: pick something from your own studies or life you'd like to predict from one other number (next term's grades from this term's, say, but find your own). What would the slope mean, in its units? How would you check that the line beats just guessing the average?
Points to compare
- A good answer says the slope in units: “each extra point this term adds, say, 0.7 to next term's predicted grade”, as step 3 read 0.385 as $385 per extra $1,000.
- The check is step 6's comparison: the line's typical miss (RMSE) against the miss from guessing the average for everyone. Better still, measure it on cases the line wasn't fitted to (step 5).
- Then: is a miss that size small enough for what the prediction is for (step 5)? Fine for planning, perhaps; not for one person's decision.
You should now be able to:
- name the dependent and independent variables and the parameters of a simple model (steps 1 and 4);
- read a line's intercept as its prediction at zero, and its slope as the change in prediction per unit (step 3);
- judge a model by its misses, compared with a simpler guess (steps 5 and 6).
Assumptions
- data
lalondefrom the R package Matching: 445 men, earnings in dollars with cents as in the package. - simplificationOne independent variable and a straight line, fitted by least squares (
lm). Standard errors, t values and p-values are left for later. - interpretationIn step 2, “about $10,000” means $9,000 to $11,000 in 1974.
- parameterThe RMSE divides by n = 445.
summary(model1)reports a slightly larger “residual standard error”, which divides by n − 2. - simplificationThe charts draw the same least-squares line from the same data in the page itself; a built-in check confirms it matches R's numbers.
Left out: checking predictions on new data, and whether 1974 earnings cause 1975 earnings (the line only predicts).