…
…
In a table of 160 students, measure the relationship between study hours and final grade with a scatter plot, r and regression, catch the prior-preparation clue in the residuals, and decide what can be told to parents.
You are a volunteer in a high school guidance office. The school counselor is preparing for a parents' meeting and expects an answer from you: "How much does a student's grade go up when they study more each week? Can I tell parents, 'Make your child study one more hour a day and the grade will rise by this much'?" In this project you will plot and measure the relationship, summarize it with a line, look in the residuals for what the line misses, test a third variable, and finally decide which statements the data can support.
Data: study_grades.csv, the records of 160 students over one term; each row is one student. Fictional sample data: Hands-on Mentor made the table, and the student codes and numbers do not describe real people.
| Column | Description |
|---|---|
student | Student code (S001–S160) |
pretest | Score of the level test at the start of the term (0–100), that is, prior preparation |
study_hours | Average weekly study time during the term (hours) |
sleep_hours | Average sleep time per night (hours) |
absent_days | Days absent during the term |
final_grade | Final exam grade (0–100) |
Lessons used: reading CSV files, filtering and sorting, splitting into intervals with pd.cut and the groupby loop (earlier units), scatter plot and correlation (36), correlation and causation, testing a confounder inside groups (37), simple linear regression, prediction and residuals (38), multiple regression (39).
Cells share one session: each step works with df, m and the other variables defined by the previous one.
What you'll do
Required topics
study_grades.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.
You are a volunteer in a high school guidance office. The school counselor is preparing for a parents' meeting and asks: "How much does a student's grade go up when they study more each week? Can I tell parents, 'Make your child study one more hour a day and the grade will rise by this much'?"
You have the table study_grades.csv. Start with the basics: how many rows are there, what does each row describe (the observation unit), and what range do the weekly study hours cover? Keep that range in mind; it will matter when you make predictions.
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 the explanatory variable (weekly study hours) on the horizontal axis and the response (final grade) on the vertical axis. Write the unit in the axis labels.
Then measure the strength of the relationship with one number: the Pearson correlation coefficient r. Go one step further: also see the correlation of the final grade with the other numeric columns in one table. Is study time the variable most strongly related to the grade?
Finally, write the direction of the relationship in the variable direction as "increasing"
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
"decreasing"This step's code cell is for members.Sign up free
The counselor wants an answer to "how much". Simple linear regression gives it (lesson 38): fit the line that predicts the final grade from study hours, print the intercept and the slope, and look at R².
Then pick the correct reading of the slope and write its letter in the variable slope_reading:
Also write R² as a percentage in the variable explained, rounded: how much of the variation in grades is explained by study hours, and how much is left unexplained?
This step's code cell is for members.Sign up free
A parent asks: "My child studies 10 hours a week. What grade does this model predict? And what if it goes up to 40 hours?"
Make two predictions with the model. Then go back to the study hours range you found in step 1: is 40 hours inside that range? A prediction outside the data is called extrapolation, and we have no evidence that the line keeps the same shape out there. The prediction itself also tells you something: the final grade is out of 100.
Write whether you can trust the 40-hour prediction in the variable reliable_40 as True or False.
This step's code cell is for members.Sign up free
The line produces a prediction for every student. The difference between the actual grade and the prediction is the residual (lesson 38). A positive residual means the student scored higher than the model expected; a negative residual means lower.
Add the residuals to the table as a column and plot them against study hours, with a zero line as a reference. Then list the five students the model gets most wrong (the largest absolute value of the residual) and look at the pretest column. Do you see a difference between the students with large positive residuals and those with large negative residuals?
pretest is the score of the level test students took at the start of the term, that is, their prior preparation. Finally, check this hunch with a number: what is the correlation between the residuals and pretest?
This step's code cell is for members.Sign up free
The residuals gave a clue: students with high prior preparation get higher grades than the model expects. Maybe prior preparation is a confounder (lesson 37): a well-prepared student may both study more and already score higher. If so, part of the relationship between study time and grade comes from prior preparation.
Test this in two steps. First check the two arrows of the confounder: is pretest related to study hours and to the final grade? Then hold the confounder fixed: split the students into three groups by pretest score and look again at r between study time and grade within each group. Does the relationship disappear, weaken, or continue?
This step's code cell is for members.Sign up free
The groups showed that the relationship continues when prior preparation is held fixed, but the "how much" question is still open. Multiple regression answers it (lesson 39): add the pretest score to the model as a second explanatory variable.
In the new model, the coefficient of study hours now means "among students with the same pretest score, the average grade difference for those who study 1 more hour per week". Print the study coefficient and R² of both models side by side. Did the coefficient get larger or smaller? Write the result in the variable change as "increases", "decreases" or "same".
This step's code cell is for members.Sign up free
Now go back to the counselor's question: can they tell parents "make your child study one more hour a day and the grade will rise by this much"? From the statements below, choose the ones this data supports, and write their letters in alphabetical order in the list true_statements.
For every statement you do not choose, ask yourself why: does it use cause-and-effect language, does it forget a confounder we did not measure (motivation, course choice, the study environment at home), or does it go outside the data?
This step's code cell is for members.Sign up free