STAT 306 - Module 01
By the end of this lecture, you should be able to:
lm().There is a probability distribution of \(Y\) for each value of \(x\);
The means of these distributions are linearly related to \(x\);
The explanatory variable \(x\) is assumed to be fixed for each individual/sample;
Why treat \(x_i\) as fixed?
In this course, we treat the predictor values \(x_i\) as fixed constants (a deterministic design). Students frequently wonder how useful such a model is, as in many situations \(X\) is actually a random variable.
The answer is that the key properties we will derive here remain valid even if \(X\) is random, provided we condition on \(X_i = x_i\) and assume \(\mathbb{E}(\varepsilon_i \mid X_i) = 0\). Using fixed \(x\) avoids overcomplicated conditional notation.
Since \(x_i\) is fixed, we write \(\mathbb{E}[Y_i]\) rather than \(\mathbb{E}[Y_i \mid X_i = x_i]\) throughout these notes — they mean the same thing here, but the former is lighter notation. Keep this in mind whenever you see \(\mathbb{E}[Y_i]\): it is always implicitly evaluated at the fixed value \(x_i\).
We have that, for \(i=1,\ldots,n\), \[Y_i = \beta_0+\beta_1x_i+\varepsilon_i,\quad \text{where } \varepsilon_i \sim N(0, \sigma^2)\]
We have that, for \(i=1,\ldots,n\), \[Y_i = \beta_0+\beta_1x_i+\varepsilon_i,\quad \text{where } \varepsilon_i \sim N(0, \sigma^2)\] What is \(\mathbb{E}\left[Y_i\right]\)?
We have that, for \(i=1,\ldots,n\), \[Y_i = \beta_0+\beta_1x_i+\varepsilon_i,\quad \text{where } \varepsilon_i \sim N(0, \sigma^2)\] What is \(\mathbb{E}\left[Y_i\right]\)?
We have that, for \(i=1,\ldots,n\), \[Y_i = \beta_0+\beta_1x_i+\varepsilon_i,\quad \text{where } \varepsilon_i \sim N(0, \sigma^2)\] What is \(Var\left[Y_i\right]\)?
We have that, for \(i=1,\ldots,n\), \[Y_i = \beta_0+\beta_1x_i+\varepsilon_i,\quad \text{where } \varepsilon_i \sim N(0, \sigma^2)\] What is \(Var\left[Y_i\right]\)?
We have that, for \(i=1,\ldots,n\), \[Y_i = \beta_0+\beta_1x_i+\varepsilon_i,\quad \text{where } \varepsilon_i \sim N(0, \sigma^2)\] What is the distribution of \(Y_i\)?
We have that, for \(i=1,\ldots,n\), \[Y_i = \beta_0+\beta_1x_i+\varepsilon_i,\quad \text{where } \varepsilon_i \sim N(0, \sigma^2)\] What is the distribution of \(Y_i\)?
We have that, for \(i=1,\ldots,n\), \[Y_i = \beta_0+\beta_1x_i+\varepsilon_i,\quad \text{where } \varepsilon_i \sim N(0, \sigma^2)\] What is \(Cov\left(Y_i, Y_j\right)\) for \(i \neq j\)?
We have that, for \(i=1,\ldots,n\), \[Y_i = \beta_0+\beta_1x_i+\varepsilon_i,\quad \text{where } \varepsilon_i \sim N(0, \sigma^2)\] What is \(Cov\left(Y_i, Y_j\right)\) for \(i \neq j\)?
You might need to refresh this page to show the plot
\[ \text{Weight} = -166+140\times\text{Height}+\varepsilon \]
\[ \begin{aligned} \text{Weight} &= -166 + 140 \times 1.63 + \varepsilon \\ &= 62.2 + \varepsilon \end{aligned} \]
{
const mean = -166+140*height;
const stdDev = sigma;
const elementId = 'normal-density';
const title = `Weight Distribution for a person who is ${height} metres tall.`;
const xlab = 'Weight (kg)';
const ylab = 'Density';
const titleFontSize = '22px';
const labelFontSize = '18px';
const tickFontSize = '16px';
const margin = { top: 40, right: 20, bottom: 50, left: 70 };
d3_createNormalDensityPlot({
elementId,
mean,
stdDev,
title,
xlab,
ylab,
titleFontSize,
labelFontSize,
tickFontSize,
margin
});
}\[ \text{Weight} = 62.2 + \varepsilon \]
\[ \overbrace{\color{red}{\underbrace{\beta_0+\beta_1 x_i}_{\text{Regression Line}:\\\quad \mathbb{E}[Y_i]}} + \epsilon_i}^{\text{Point: } Y_i} \]
\(\mathbb{E}[Y] = \beta_0 + \beta_1 x\) is the population mean of \(Y\) at the fixed value \(x\).
If we had repeated measurements for each value of \(X=x\), we could estimate the mean for each value of \(X\) separately, just by taking the average of the \(Y\) values for each value of \(X\).
Figure 3: Regions with no data. Estimating \(\mathbb{E}[Y]\) separately at each point is completely impossible without a structural model.
But, what if we assume a linear structure between the mean of the population and the value of \(X\)?
The estimation of the means \(\mathbb{E}[Y]\) for all values of \(X\) reduces to estimating \(\beta_0\) and \(\beta_1\).
We denote the estimators by \(\widehat{\beta}_0\) and \(\widehat{\beta}_1\).
lm function.Important: Association is not causality.
In general, we cannot conclude that changes in \(X\) cause a change in \(Y\). The conclusion of causality requires more than a good model.
This means that an increase of \(1\text{ mm}\) in flipper length is associated with an expected increase of \(50.15\text{ g}\) in body mass.
Not ok to say: “An increase of \(1\text{ mm}\) in flipper length increases body mass by \(50.15\text{ g}\).”
To predict the value of \(Y\) for a given \(x\), we use the estimated regression line: \[ \widehat{Y} = \widehat{\beta}_0 + \widehat{\beta}_1 x \]
\(\widehat{Y}\) is called the predicted value.
predict()predict() function receives as input the fitted model and new observations for which we want to predict the response value.data.frame whose column names match the model formula.
predict(model, 200) will not work.Common Pitfall: newdata
newdata argument or misname it, R will silently ignore the new data and use the original training data instead.
predict(model, data = new_penguin) returns all 344 fitted values (\(\widehat{Y}_i\)) with no error or warning!There’s no way for us to know whether the relationship is still linear outside the range of the data;
Be cautious when using the model outside the range of the data, as the relationship between \(X\) and \(Y\) may differ significantly.
flipper_length_mm we want to use the sex of penguins to explain their body_mass_g. Our model then is:\[ \text{body_mass_g}_i = \beta_0 + \beta_1\text{sex}_i + \varepsilon_i \]
A categorical variable with \(C\) categories will be encoded using \(C\) variables.
The penguins’ sex can be male or female. Then,
\[ x_{i}^{\text{(male)}} = \begin{cases} 1 & \text{if penguin } i \text{ is male}\\ 0 & \text{otherwise} \end{cases}\qquad x_{i}^{\text{(female)}} = \begin{cases} 1 & \text{if penguin } i \text{ is female}\\ 0 & \text{otherwise} \end{cases} \]
\[ Y_i = \beta_0 + \beta_1 x_{i}^{\text{(male)}} + \beta_2 x_{i}^{\text{(female)}} + \varepsilon_i \]
\[ x_{i}^{\text{(male)}} = \begin{cases} 1 & \text{if penguin } i \text{ is male}\\ 0 & \text{otherwise} \end{cases}\qquad x_{i}^{\text{(female)}} = \begin{cases} 1 & \text{if penguin } i \text{ is female}\\ 0 & \text{otherwise} \end{cases} \]
\[\begin{equation} \text{sex}_i = \begin{cases} 1 & \text{if penguin $i$ is male}\\ 0 & \text{if penguin $i$ is female} \end{cases} \end{equation}\]
patient_status could be defined:\[\begin{equation} \text{patient_status}_i = \begin{cases} 1 & \text{if patient $i$ is sick}\\ 0 & \text{if patient $i$ is healthy} \end{cases} \end{equation}\]
What is the Expected Value of \(Y\) given that the penguin is male?
What is the Expected Value of \(Y\) given that the penguin is female?
Use the above to explain the meaning of \(\beta_0\) and \(\beta_1\) in this model.
For a male penguin (\(\text{sex}_i = 1\)), we have: \[ \begin{align*} \mathbb{E}[Y_i] &= \beta_0 + \beta_1 \times \text{sex}_i\\ &= \beta_0 + \beta_1 \times 1\\ &= \beta_0 + \beta_1 \end{align*} \]
Similarly, for a female penguin (\(\text{sex}_i = 0\)), we have: \[ \begin{align*} \mathbb{E}[Y_i] &= \beta_0 + \beta_1 \times \text{sex}_i\\ &= \beta_0 + \beta_1 \times 0\\ &= \beta_0 \end{align*} \]
\(\beta_0\) is the expected value of \(Y\) when the penguin is female, and \(\beta_1\) is the difference in expected values of \(Y\) between male and female penguins.
The model becomes: \[Y_i = \beta_0 + \beta_1 \text{sex}_i + \varepsilon_i\]
Let \(\mu_{\text{male}} = \mathbb{E}[Y_i]\) for a male penguin (\(\text{sex}_i=1\)), and \(\mu_{\text{female}} = \mathbb{E}[Y_i]\) for a female penguin (\(\text{sex}_i=0\)). Then: \[ \begin{align} \mu_{\text{male}} &= \beta_0 + \beta_1\\ \mu_{\text{female}} &= \beta_0 \end{align} \]
Therefore, \[\beta_1 = \mu_{\text{male}} - \mu_{\text{female}}\] is the difference in expected values of \(Y\) between male and female penguins.


body_mass_g of female penguins.body_mass_g between male and female penguins.
Note that there is no line in this case.
Sex cannot be 0.1 or 0.5. It can only be 0 or 1.
So, what is going on?
\[\begin{equation} \text{body_mass_g}_i = \begin{cases} \beta_0 + \beta_1 + \varepsilon_i& \text{if penguin $i$ is male}\\ \beta_0 + \varepsilon_i & \text{if penguin $i$ is female} \end{cases} \end{equation}\]
Remember that in regression, we model the mean given the value of a covariate.
So, in this case, we are modelling the mean of female penguins and the mean of male penguins;
Note that:
body_mass_g of female penguins.body_mass_g of male penguins.Population Regression: \[ Y_i = \beta_0 + \beta_1 x_i + \varepsilon_i \]
Sample Regression (estimated from the sample): \[ Y_i = \widehat{\beta}_0 + \widehat{\beta}_1 x_i + e_i \]
Since \(\beta_0\) and \(\beta_1\) are unknown population parameters, we estimate them based on a sample;
\(\widehat{\beta}_0\) and \(\widehat{\beta}_1\) are the estimators of \(\beta_0\) and \(\beta_1\).
But how do we find the “best” estimates for a given sample?
Simple Linear Regression Model: \[Y_i = \beta_0 + \beta_1 x_i + \varepsilon_i\] decomposes the response into a systematic mean line \[\mathbb{E}[Y_i] = \beta_0 + \beta_1 x_i\] and a random error \(\varepsilon_i \sim (0, \sigma^2)\) with zero mean, constant variance, and uncorrelated errors (\(\text{Cov}(\varepsilon_i, \varepsilon_j) = 0\)).
Parameter Interpretation:
The Range Problem (Extrapolation):
Categorical Covariates:
Population vs. Sample:
How do we choose the optimal line through our sample data?
In this module, we just relied on R to fit the model and estimate the parameters.
In Module 02, we formalize the Least Squares Criterion and derive the formulas for the estimators \(\widehat{\beta}_0\) and \(\widehat{\beta}_1\).
© 2025 Rodolfo Lourenzutti – Material Licensed under CC By-SA 4.0