…
…
Explore the relationship between temperature and bike rentals with a scatter plot, r, regression, and residuals, test a confounder candidate, and write a six-sentence note to the manager without claiming cause and effect.
You are an intern at a city transport office. Before a meeting, the manager asks you: "More people ride bikes when it gets warmer. Should we add stations as the weather warms up?"
You have two years of daily rentals from the bike-share system and the weather for those days. In this task you will combine what you learned in lessons 36–39 on data you have not seen before, outside the Bazaar case: plot and measure a relationship, summarize it with a line, find what the line misses, test a confounder, and write the result in the language of association. There are no new concepts.
Data: bike_rentals.csv, one row for each day from January 1, 2024 to December 31, 2025 (fictional sample data).
| Column | Description |
|---|---|
date | The day |
temp_c | Daily average temperature (°C) |
rain_mm | Daily rainfall (mm) |
wind_kmh | Average wind speed (km/h) |
holiday | Public holiday (1/0) |
rentals | Number of rentals that day |
Steps: 1. Read · 2. Plot · 3. Model · 4. Look at the residuals · 5. Test a confounder · 6. Check with a model (for those who finished lesson 39; if you skip it, move on with "Show solution") · 7. Present · 8. Write.
Lessons used: month from a date (6), bands with pd.cut (11), choosing a chart (17), scatter plots and correlation (36), correlation and causation (37), simple regression (38), multiple regression (39, optional).
This task is a "suggested stop": it does not lock your progress and has no quiz.
When you finish the task, ask yourself these four questions; the answers are below.
1. When r = 0.79, why can we not say "the relationship is linear"?
2. In summer months r is negative, but over the whole year it is positive. Which concept explains this?
3. Why did the coefficient of temperature drop when rain was added to the model?
4. If you applied the same steps to ice cream sales and temperature data, which confounders would you test?
Answers: (1) r only measures the strength of a linear relationship; in the scatter plot and in the band averages, rentals fall after 25 °C, so the relationship is curved. (2) A confounder and looking inside groups (lesson 37): the difference between seasons creates the trend over the whole year, but within summer the hottest days have fewer rentals. (3) Rainy days are both cooler and lower in rentals; while rain was missing from the model, its share was loaded onto the coefficient of temperature. (4) Holidays and school breaks, tourist season, weekends: all of them can change together with warm periods and with sales.
What you'll do
Required topics
bike_rentals.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.
Before answering the manager, get to know the data. In bike_rentals.csv each row is one day: the day's average temperature, rain, wind, whether it was a holiday, and the number of rentals.
Read the file, then check how many rows there are and what one row stands for (the unit of observation). Next, collect the mean, minimum, and maximum of temperature and rentals in one table. The range will matter in step 3: a model's predictions are only reliable inside the range the data covers.
This step's code cell is for members.Sign up free
The first look at the relationship between two numeric variables is a scatter plot (lesson 36). Put temperature on the horizontal axis (the explanatory variable) and rentals on the vertical axis. Add the units to the axis labels and add a title.
Then measure the strength of the relationship with two numbers: Pearson r gives the strength of a linear relationship, and Spearman gives the strength of a one-directional (rank-based) relationship. Look at the plot and answer the four questions: direction, shape, strength, unusual points. In this step, write the direction and the shape for the two variables.
Watch out: a high r does not mean the relationship has to be linear. Look closely at what the points do on the hottest days.
This step's code cell is for members.Sign up free
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Now summarize the relationship with a single line (lesson 38): smf.ols("rentals ~ temp_c", data=df).fit(). The slope tells you, with its unit, how much the average number of rentals changes when temperature goes up by 1 °C. R² tells you how much of the variation in rentals this line explains.
The manager also asks, "What happens at 40 degrees?" Ask the model for one prediction at 20 °C and one at 40 °C. Then ask yourself: how hot was the hottest day in the data (step 1)? Can you trust the 40 °C prediction?
This step's code cell is for members.Sign up free
Where in the data is the line good, and where is it poor? Residuals tell you: the actual value minus the prediction (lesson 38). Plot the residuals against temperature and add a line at 0. If the model is good, the points scatter on both sides of 0 with no pattern; if you see a curve, the model is missing something.
See the same thing in numbers too: cut temperature into 5 °C bands (pd.cut, lesson 11) and compute the average rentals and the average residual in each band. In which bands does the model predict too high, and in which too low?
This step's code cell is for members.Sign up free
Rentals are high on hot days, but is temperature doing that? Other things come along with hot days. First write your own candidate: a third variable that could be related to both temperature and rentals (lesson 37). There is no right or wrong answer here; what matters is that you write your idea down before testing it.
Then test two candidates:
Is the relationship inside the groups in the same direction as the overall one? Does rain change together with both temperature and rentals?
This step's code cell is for members.Sign up free
This step is for those who finished lesson 39 (multiple regression). If you skipped that lesson, look at the output with "Show solution" and move on with "Complete step"; the rest of the task does not depend on this step.
The model version of looking inside groups is adding the candidate to the model. Add rain and holiday: rentals ~ temp_c + rainy + holiday. The temperature coefficient now tells you the association of 1 °C with average rentals "when rain and holiday status are the same". Compare it with the 93.3 from step 3: did it go up or down?
This step's code cell is for members.Sign up free
The manager will not look at the scatter plot, the residuals, or the coefficient table; they will see one chart. Their question is "Do more people ride when it gets warmer?" The chart that answers it: the average daily rentals by temperature band (lesson 17).
Rules: the bands are categories, so use a bar chart; the vertical axis starts at zero; the axis labels include units; and the title states the result, not just the variable name (for example, which band is highest and what happens in the heat).
This step's code cell is for members.Sign up free
The last job: a six-sentence note to the manager. Each sentence has one task:
1. The direction and strength of the relationship (with r)
2. The slope, interpreted with its unit (how many rentals per °C)
3. The shape of the relationship and its limit (what happens in the heat)
4. The confounder candidate and the result of testing it (season or rain)
5. What this data does not say (the number of stations is not in the data)
6. A recommendation and how to test it (a trial)
Use the language of association: "rise together", "associated with". Cause-and-effect verbs such as "increases", "causes", or "leads to" cannot be supported by this data.
Self-check list (tick after you write):
Weak example (find out why): "Temperature increases bike use (r = 0.79), 40 degrees gives 4,283 rentals, so we should add stations in summer." It has three errors: cause-and-effect language, a prediction stretched beyond the data, and a recommendation about a variable that is not in the data.
The check only looks at form (six sentences, each at least 40 characters, numbers, and cause-and-effect verbs). Judge the content yourself with the list; a sample note is in "Show solution".
This step's code cell is for members.Sign up free