…
…
Model the rent in student housing listings with multiple regression; add the neighborhood as a categorical variable, read the coefficients as "holding the others fixed", look at the residuals and at the model's error on listings it has never seen, and draw the limit of "if I furnish my home, does the rent go up?"
A university student council runs a listings board where students find homes (fictional). The council wants to put a label on every listing: "Is this rent reasonable compared with similar homes?" A landlord also asks the board: "If I furnish my home, how much more can I ask?" They ask you to build a model that explains rent, to measure how reliable the model is, and to say how much of the landlord's question this data can answer.
Data: rent_listings.csv, 230 listings posted on the board in September and October; each row is one listing (fictional sample data).
| Column | Description |
|---|---|
listing_no | Listing number |
month | Month the listing was posted: September or October |
neighborhood | Campus Edge, Downtown, Seaside, Hilltop |
area_m2 | Size of the home (m²) |
min_to_campus | Walking time to campus (minutes) |
furnished | Is the home furnished (1/0) |
photo_count | Number of photos in the listing |
rent | Monthly rent asked in the listing ($) |
Steps: 1. Split · 2. Simple model · 3. Residuals · 4. Neighborhood · 5. Distance and furnishing · 6. Prediction · 7. Extra columns and test error · 8. Limits · 9. Answer to the landlord.
Lessons used: conditional filtering (5), grouping (7), correlation and confounders (36, 37), simple and multiple regression, C(), residuals, prediction and extrapolation, adjusted R² (38, 39), A/B testing and fallacies (42, 43). The one thing you do for the first time in this project is to build a model with part of the listings and test it on listings it has never seen (step 7); all the tools you use come from the lessons.
Cells share one session; each step uses the variables of the previous one. The random columns are generated with np.random.default_rng(42), so when you run the cell again you see the same numbers.
What you'll do
Required topics
rent_listings.csvDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The student council's listings board wants to label each listing "Is this rent reasonable compared with similar homes?" A model will sit behind the label, and the model will be used on future listings. So we start with a rule: we build the model only with September listings and do not touch the October listings until the very end. At the end we will measure how far off the model is on these listings it has never seen (step 7).
Read the table, and split the September listings into train and the October listings into test. Then, in the train table only, show each neighborhood's mean rent, mean distance to campus and share of furnished listings in one table (summary, rounded to one decimal).
While you look at the table, note two things: which neighborhood is farthest from campus, and in which neighborhood the furnished listings are concentrated. Both observations come back in steps 5 and 8.
This step's code cell is for members.Sign up free
The first question is the simplest: how much more do big homes cost? Build the model that explains rent with area only on the train table (m1). Print the coefficients with one decimal and R² with three.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Get ready to read the slope with its unit: 1 extra m² goes with how many dollars per month? R² also tells us how much of the difference in rent goes together with area. Where does the rest come from? That is the question of the next step.
This step's code cell is for members.Sign up free
m1 explains less than half of the differences in rent. Where is the rest? A residual is the difference between the observed rent and the model's prediction. If the residuals are systematically positive in one group and negative in another, the model does not know about that grouping.
Compute the mean residual by neighborhood (resid_by_area, rounded to a whole number). Then, from the readings below, write the letter of the correct one in the variable reading:
This step's code cell is for members.Sign up free
The neighborhood is a text variable. It enters the model with C(), and each coefficient is a difference from the reference neighborhood. statsmodels picks the reference in alphabetical order; here the reference is Campus Edge.
Build the model rent ~ area_m2 + C(neighborhood) (m2), print the coefficients with one decimal and show R² with three. Save the Hilltop coefficient as hilltop_gap.
Try to turn the coefficients into sentences: what does the Hilltop coefficient say "compared with a Campus Edge home of the same size"? Why is the Seaside coefficient so small, when Seaside has the highest mean rent? We will look for the answer in the next step.
This step's code cell is for members.Sign up free
Landlord Lena (fictional) writes to the board: "Furnished homes cost more. If I furnish my home, how much more can I ask?" First look at the raw gap: how many dollars higher is the mean rent of furnished listings than of unfurnished ones (raw_gap)? Then add distance to campus and furnishing to the model (m3) and show the coefficients in a table together with their p-values.
Compare two things. First, is the furnishing coefficient the same as the raw gap? Remember in which neighborhood the furnished listings were concentrated in the table of step 1. Second, is the Seaside coefficient the same as in m2?
Write the letter of the sentence that best explains the change in Seaside in the variable reading:
m2 did not know the distance, Seaside's difference was mixed with the lowering effect of distance. When homes at the same distance from campus are compared, Seaside is clearly more expensive.m2 was correct.This step's code cell is for members.Sign up free
The board wants to see the "expected rent" for two new listings:
| Home | area_m2 | neighborhood | min_to_campus | furnished |
|---|---|---|---|---|
| 1 | 60 | Campus Edge | 5 | 1 |
| 2 | 180 | Downtown | 15 | 0 |
First print the area range the model has seen. Then put the two homes in a table and predict with m3 (pred, rounded to a whole number). Finally, write which prediction can be trusted in the list reliable (True or False, in the order of the homes).
This step's code cell is for members.Sign up free
A friend suggests: "The more columns we put in the model, the better it gets." To test this, let's add 20 random columns that cannot have anything to do with the listings (like the random column test in lesson 39, but with 20 of them) and compare the four-variable m3 with this big model (m4) on two measures:
train) listings and on the October (test) listings the model has never seen, and show it in the err table (rows m3, m4; columns train, test).What happens to the mean error on the October listings (test) when 20 random columns are added?
When you see the result, ask yourself: which model would you pick for the label, and which number are you looking at when you say so?
This step's code cell is for members.Sign up free
We built the model; now let's draw the limits of the results. First one more number: compute and print the correlation between the number of photos in a listing and the rent.
Then read the six sentences below. For each one ask: does this data and these models really show it? Write the letters of the ones they show in the list supported, and the letters of the ones they do not show in the list not_supported.
In every sentence think about three traps: could there be an unmeasured confounder (are furnished homes different in other ways too)? Could the direction of the arrow be reversed? What exactly does the number in the table measure?
This step's code cell is for members.Sign up free
Lena's question is still open: "If I furnish my home, how much more can I ask?" The listing data cannot give a certain answer. So which study could? Write the letter of the most reliable one in the variable experiment:
Then write a note of two or three sentences to Lena (advice, in your own words, at least 150 characters): the size of the relationship (with a number), what this data cannot say, and the model's typical error on new listings. Do not use cause-and-effect verbs like "raises" or "makes"; the check looks for them, and you judge the content.
Weak example: "The model shows that furnishing raises the rent by $68.90, and since R² is 0.83 this is certain." It uses cause-and-effect language, gives a number with cent precision, and takes R² as proof.
This step's code cell is for members.Sign up free