| absences | points |
|---|---|
| 1 | 552 |
| 9 | 210 |
| 6 | 315 |
| 4 | 454 |
| 0 | 619 |
| 5 | 378 |
| 13 | 108 |
| 2 | 534 |
| 5 | 398 |
| 3 | 462 |
| 0 | 593 |
| 1 | 645 |
| 0 | 602 |
| 7 | 315 |
| 10 | 307 |
Tutorial 6: Linear Regression
Exercise 1
Bivariate data relating the number of classroom absences and the number of points earned prior to the final exam in the course was gathered from a random sampling of 15 students.
Here is the table gathering the collected data:
Here is the scatter plot representing the data:

We also give the following quantities (with \(x =\) the number of absences and \(y =\) the number of points):
- \(\bar{x} = 4.4\)
- \(\bar{y} = 432.8\)
- \(\text{cov}(x,y) = -625.63\)
- \(\text{sd}(x) = 4.01\)
- \(\text{sd}(y) = 160.95\)
What is the correlation between the number of absences and the number of points earned by the students?
From your answer to the previous question and from the scatter plot figures, what could be the link between the number of absences and the number of points earned by the students?
How would you model this relationship?
Can you compute the estimation of the parameters in this model?
What assumptions are generally done in this context?
A student with 7 absences would probably have how many points in the course? A student with 20 absences would probably have how many points? Do you need any of the previous assumptions to do this prediction?
Solution
The correlation coefficient is \[\text{cor}(x,y) = \frac{\text{cov}(x,y)}{\text{sd}(x)\,\text{sd}(y)} = -0.97\]
The correlation close to -1 and the points in the scatter plot aligning around a line with negative slope suggest that there exists a negative linear relationship between the the number of absences and the number of points earned by the students in the sample.
We will use a linear regression model to relate the number of points earned by the students with their number of absences. Formally, we would have: \[y_i = \beta_0 + \beta_1\, x_i + \varepsilon_i\] for \(i=1,\dots,n\), where \(n\) is the sample size, \((x_i, y_i)\) corresponds to the respective number of absences and number of points for student \(i\) in the sample, and \(\varepsilon_i\) is the error term.
We can compute the estimation for the linear coefficients in the model, namely the intercept \(\beta_0\) and the slope \(\beta_1\) by using a least square optimization, i.e. finding the pair \((\hat\beta_0,\hat\beta_1)\) that minimizes (regarding the pair \((\beta_0,\beta_1)\)) the sum of squared error terms across the sample \(\sum_{i=1}^n (y_i - \beta_0 - \beta_1\,x_i)^2\). We have a direct formula to compute \(\hat\beta_0\) and \(\hat\beta_1\):
- \(\hat\beta_1 = \frac{\mbox{cov}(x,y)}{\mbox{var}(x)} = -38.82\)
- \(\hat\beta_0 = \bar y - \hat{\beta}_1 \bar x = 603.63\)
In the linear model framework, we generally assume that the error terms or residuals \((\varepsilon_i)_{i=1,\dots,n}\) are independent and identically distributed with mean = 0 and constant variance \(\sigma^2\) (a.k.a. the homoskedasticity assumption). Additionally, we can use the Gaussian linear model framework where we simply assume that all \(\varepsilon_i\) are also Gaussian, i.e. \(\varepsilon_i \sim \mathcal{N}(0, \sigma^2)\).
We can compute the predicted number of points \(\hat{y}\) for a given number of absences \(a\) using the linear model that we have built: \[\hat{y} = 603.63 + -38.82 a\]
With 7 absences, a student is expected to get \(\hat{y} = 603.63 + -38.82 \times 7 = 331.86\) points before the final exam.
And with 20 absences, a student is expected to get \(\hat{y} = 603.63 + -38.82 \times 20 = -172.86\) points.
Note: Here we see the potential limitations of our model. It is likely (depending on the point system) that points could not be lower than 0. In that case, our linear model is not suitable for very large absence number or we should possibly use a different model to account for this lower bound.
Exercise 2
Physicians feel that there is a relationship between a person’s age and the number of days they are sick that year. A random sample of 20 patients was obtained to this end comparing age and days sick.
Here is the table gathering the collected data:
| age | sickDays |
|---|---|
| 12 | 0 |
| 19 | 2 |
| 35 | 8 |
| 4 | 10 |
| 68 | 21 |
| 42 | 16 |
| 21 | 18 |
| 24 | 24 |
| 32 | 29 |
| 75 | 15 |
| 14 | 3 |
| 8 | 7 |
| 25 | 12 |
| 39 | 17 |
| 56 | 15 |
| 18 | 11 |
| 72 | 31 |
| 64 | 26 |
| 45 | 20 |
| 87 | 30 |
We also give the following quantities (with \(x =\) the age and \(y =\) the number of sick days):
- \(\bar{x} = 38\)
- \(\bar{y} = 15.75\)
- \(\text{cov}(x,y) = 157.89\)
- \(\text{sd}(x) = 24.77\)
- \(\text{sd}(y) = 9.3\)
We fit a linear model and we get the following result:
- \(\sum_{i=1}^n (y_i - \bar{y})^2 = 1643.75\)
- \(\sum_{i=1}^n (\hat{y}_i - \bar{y})^2 = 771.87\)
- \(\sum_{i=1}^n (\hat{y}_i - y_i)^2 = 871.88\)
Here is the scatter plot representing the data and we also represent the fitted linear model:
`geom_smooth()` using formula = 'y ~ x'

What can you say about this situation? How is the linear model suitable to question the physician hypothesis?
How was the linear model fitted?
What can you say about this model?
How many days in a year would you predict that a 60 year old person will be sick?
Solution
- We use a linear model to question the possible linear relationship between the number of days being sick during the year and the age of the patient.
The correlation between the two quantities is \(\text{cor}(x,y) = \frac{\text{cov}(x,y)}{\text{sd}(x)\,\text{sd}(y)} = 0.69\) and the scatter plot suggest a possible positive linear relationship between the number of days being sick during the year and the age of the patient.
- We use the following linear model \[y_i = \beta_0 + \beta_1 x_i + \varepsilon_i\] for \(i=1,\dots,n\), where \(n\) is the sample size, \((x_i, y_i)\) respectively corresponds to the age and the number of days being sick during the year and the number of points for patient \(i\) in the sample, and \(\varepsilon_i\) is the error term.
The intercept \(\hat\beta_0\) and slope \(\hat\beta_1\) coefficients were estimated using the following formulae:
- \(\hat\beta_1 = \frac{\mbox{cov}(x,y)}{\mbox{var}(x)} = 0.26\)
- \(\hat\beta_0 = \bar y - \hat{\beta}_1 \bar x = 5.97\)
- Based on the analysis of variance, we can compute the \(R^2\) determination coefficient: \[R^2 = \frac{MSS}{TSS} = 1 - \frac{RSS}{TSS}\] where \(TSS = \sum_{i=1}^n (y_i - \bar{y})^2\) is the total sum of squares, \(MSS = \sum_{i=1}^n (\hat{y}_i - \bar{y})^2\) is the sum of squares explained by the model, and \(RSS = \sum_{i=1}^n (\hat{y}_i - y_i)^2\) is the residual sum of squares, with \(TSS = MSS + RSS\).
Here, we have \(R^2 = 0.47\), which is closest to 0 than to 1, meaning that the linear relationship is questionable (maybe not significant?).
- According to the fitted model (that we may not consider trustworthy), at age 60, we predict that a patient will spend \(\hat{y} = 5.97 + 0.26 \times 60 = 11.12\) days being sick per year.