AMOD-5440
Trent University
2026-09-18
In a past course, you have likely seen some form of regression, which is a short-hand for the general class of linear models where the response is related to a linear combination of predictors via equality: \[ Y = f(X) = \beta_0 + \sum_{j=1}^{p} \beta_j x_j + \varepsilon \]
This model allows \(p\) predictors \(X\) to be combined via addition, and scalar multiplication, to model an observed \(Y\), with additive noise \(\varepsilon\). In simpest form, this is simple linear regression, where \[ y = \beta_0 + \beta_1 x + \varepsilon. \]
A new piece of notation that will use through the rest of the semester: \[ Y \approx \beta_0 + \beta_1 X. \] This is used in the form of “approximately modelled as”. In R, this is represented as the \(\sim\) operator, e.g.,
From such an equation model, we obtain two estimates of unknown constants: \(\beta_0\) estimated by \(\hat{\beta}_{0}\) and \(\beta_1\) estimated by \(\hat{\beta}_{1}\). These are known as the model coefficients.
Note: the use of “hats” indicates estimates, so \(\hat{\beta}_{0}\) is our estimate for the parameter (unknown constant) \(\beta_0\).
We don’t know \(\beta_0\) or \(\beta_1\). We want to estimate them, so we can use the results to predict things. Accordingly, we need to use the available data to make an educated guess as to their values: estimation!
The data consists of pairs: \[
(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)
\] and then our goal is to obtain coefficient estimates \(\hat{\beta}_{0}\) and \(\hat{\beta}_{1}\) such that the linear model fits the data “well” - that is, so \(y_i \approx \hat{\beta}_{0} + \hat{\beta}_{1} x_i\) for all
\(i=1,2,\ldots,n\).
We do this by the method of least squares: minimize the sum of the squared errors (differences) between the observed \(y_i\) and the estimated \(\hat{\beta}_{0} + \hat{\beta}_{1}x_i\).
There’s some math here (calculus derivations) … but in practice, we use a computer.
Recall from last week, we briefly discussed accuracy in our estimates. The model \[ Y = \beta_0 + \beta_1 X + \varepsilon \] may only be an approximation for \(f(\cdot)\), the true, underlying, relationship between \(Y\) and \(X\); that is, \(Y = f(X)\).
This approximation is the best one possible using a linear relationship to approximate the true relationship: while we don’t truly believe the relationship is linear, this is the simplest and easiest working model.
Non-Linear TRUE relationship
If the true relationship is not linear, then what are the implications for us? How might we handle that situation?
A simulated data set, with \(f(X) = 2 + 3 X\) - the red line represents this true result.


If we use the sample mean, \(\bar{X}\), to estimate the population mean \(\mu\), then this estimate is said to be unbiased: on average, we expect \(\bar{X}\) to equal \(\mu\). But what does this actually mean?
We can think of this as saying: using one set of observations, our estimate might over-estimate \(\mu\); using a different set, we might under-estimate it. But if you consider a huge number of estimates (so \(\bar{X}_1\), \(\bar{X}_2\), and so on), then the average of these estimates will equal exactly \(\mu\). In probabilistic terms,
\[ E[\bar{X}] = \mu \]
The property of unbiasedness holds for the least squares coefficients, so \(\hat{\beta}_0\) and \(\hat{\beta}_1\) are unbiased - on average, we can expect them to equal the true, underlying \(\beta_0\) and \(\beta_1\).
That earlier simulation plot shows this: the spread of the lines around the true (red) line is fairly symmetric, so if we averaged across the different blue lines, we’d get the red line: on average, they are exactly correct.
These are our estimated intercepts, with the average coloured red. Remember that we generated the data with \(\beta_0 = 2\). The mean estimate is 2.001.
These are our estimated intercepts, with the average coloured red. Remember that we generated the data with \(\beta_1 = 3\). The mean estimate is 2.982.
The SD of an estimator’s sampling distribution is called the standard error.
For \(\bar{X}\), this is
\[ SE[\bar{X}] = \frac{\sigma}{\sqrt{n}} \]
This is used to compute confidence intervals, and also p-values in some cases.
As the estimators \(\hat{\beta}_0\) and \(\hat{\beta}_1\) are random variables, just like \(\bar{X}\), that means they have standard errors that we can compute. If we take the first generated dataset for our example, we can use this SE to visualize the uncertainty in the estimates (and therefore fitted line).
We usually leave the calculation to a computer. All that matters for us is knowing that we can read the output from a regression table in \(\textsf{R}\).
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.691591 0.2926536 5.780182 8.868766e-08
x 2.842585 0.3206358 8.865463 3.497142e-14
Notice the second column. These are the standard errors for each of the (Intercept) and x; (or the variable name) estimators.
We can compute confidence intervals for the parameters using these standard errors if we wanted to, using the standard approach of treating these as statistics, and using qt(). But that takes work, and is manual …
We can perform hypothesis tests for these estimators, too. Typically, the one we are interested in for simple linear regression is:
\[ H_{0}: \beta_{1} = 0 \quad \text{vs.} \quad H_{1}: \beta_{1} \neq 0 \]
which corresponds to the cases of “no linear relationship” and “some linear relationship”.
Practically speaking, while we could manually compute and set up these tests… that isn’t really a good use of our time.
We can use the coefficient table from earlier and look at the fourth (far right) column, where the p-value for this test is found.
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.691591 0.2926536 5.780182 8.868766e-08
x 2.842585 0.3206358 8.865463 3.497142e-14
There are three ways we assess how well the model fit (after ensuring the model fit in a way that made sense - that is, that the p-values are significant and the results aren’t silly).
\[ \begin{align} H_{0}: &\ \beta_{1} = \beta_{2} = \ldots = \beta_{k} = 0 \\ & \quad \text{vs.} \\ H_{A}:&\ \text{at least one} \neq 0 \end{align} \]
Call:
lm(formula = y ~ x, data = firstsim)
Residuals:
Min 1Q Median 3Q Max
-5.7220 -2.0505 -0.2625 1.7419 9.8712
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.6916 0.2927 5.780 8.87e-08 ***
x 2.8426 0.3206 8.865 3.50e-14 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 2.912 on 98 degrees of freedom
Multiple R-squared: 0.4451, Adjusted R-squared: 0.4394
F-statistic: 78.6 on 1 and 98 DF, p-value: 3.497e-14
Simple linear regression is … simple. The algorithm goes as follows, approximately:
lm() in R.We will consider TV advertising spending as the predictor, and Sales as the response, from the Advertising example data set considered in the previous lab.
The data look to have a fairly linear relationship overall, so it is plausible that using simple linear regression will give us something meaningful.
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.03259355 0.457842940 15.36028 1.40630e-35
TV 0.04753664 0.002690607 17.66763 1.46739e-42
We have highly significant p-values, especially for the predictor TV, which is: the slope of the line. The estimate is fairly small, meaning a small slope, but the standard error is even smaller.
We have a significant p-value, the plot looks good, the slope makes sense. Let’s check the model accuracy metrics.
Call:
lm(formula = sales ~ TV, data = Advertising)
Residuals:
Min 1Q Median 3Q Max
-8.3860 -1.9545 -0.1913 2.0671 7.2124
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.032594 0.457843 15.36 <2e-16 ***
TV 0.047537 0.002691 17.67 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.259 on 198 degrees of freedom
Multiple R-squared: 0.6119, Adjusted R-squared: 0.6099
F-statistic: 312.1 on 1 and 198 DF, p-value: < 2.2e-16
There is a more involved check we can do, which involves looking specifically at the way the model fit interacts with the data:
You’ve likely seen these in a previous course, and we will discuss them in the coming weeks.
Suppose we have \(p\) distinct predictor variables, then the multiple linear regression model takes the form:
\[ Y = \beta_0 + \beta_1 X_1 + \dots + \beta_p X_p + \varepsilon, \]
where \(X_j\) represents the \(j\)th predictor, and \(\beta_j\) quantifies the association between that predictor and the response.
We interpret each coefficient \(\beta_j\) as the average effect on \(Y\) of a one-unit increase in \(X_j\), holding all other predictors fixed.
The solution to the multiple linear regression problem is again done using Least Squares, but instead of being a line of best fit under minimum squared error, the solution becomes a higher-dimensional linear object (a plane, or a hyperplane) in a \(p + 1\) dimensional space.
Figure 3.4 in ISLR2
When we perform a multiple linear regression, there are some questions we are usually aiming to answer:
In the multiple regression setting with \(p\) predictors, we need to ask whether all of the regression coefficients are zero, i.e., whether \(\beta_1 = \beta_2 = \dots = \beta_p = 0\). As in the simple linear regression setting, we use a hypothesis test to answer this question. We test the null hypothesis:
\[ H_0: \beta_1 = \beta_2 = \dots = \beta_p = 0 \] versus the alternative
\[ H_{A}: \text{at least one } \beta_{j} \neq 0. \]
This hypothesis test is performed by computing the F-statistic, which can be found in the bottom of the summary for lm().
In Chapter 6 (a few weeks) we will examine some of the better ways we can select variables from a large set. For now, we will use only backwards selection.
Terminology
The textbook uses “Forward Selection”, “Backward Selection”, and “Mixed Selection”.
“Backward Selection” is usually referred to as “Backward Elimination”.
All three of these approaches are types of Stepwise Regression.
Algorithm:
Recall from earlier:
Once we have a fit, predicting the response, \(Y\), on the basis of a set of predictors is simple - and in R, we can use the predict() function to do so.
There will be three sources of uncertainty:
Let’s fit the advertising example, using all three kinds of advertising.
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.938889369 0.311908236 9.4222884 1.267295e-17
TV 0.045764645 0.001394897 32.8086244 1.509960e-81
radio 0.188530017 0.008611234 21.8934961 1.505339e-54
newspaper -0.001037493 0.005871010 -0.1767146 8.599151e-01
In the previous slide, we see the multiple regression coefficient estimates when TV, radio, and newspaper advertising budgets are used to predict product sales using the Advertising data. We interpret these results as follows:
Estimate Std. Error t value Pr(>|t|)
4.576465e-02 1.394897e-03 3.280862e+01 1.509960e-81
Estimate Std. Error t value Pr(>|t|)
1.885300e-01 8.611234e-03 2.189350e+01 1.505339e-54
Estimate Std. Error t value Pr(>|t|)
-0.001037493 0.005871010 -0.176714587 0.859915050
Does it make sense for the multiple regression to suggest no relationship between sales and newspaper while the simple linear regression implies the opposite? In fact it does.
Consider the correlation matrix for the three predictor variables and response variable:
TV radio newspaper
TV 1.00000000 0.05480866 0.05664787
radio 0.05480866 1.00000000 0.35410375
newspaper 0.05664787 0.35410375 1.00000000
Notice that the correlation between radio and newspaper is 0.35.
\[ \text{newspaper } \uparrow \quad \Rightarrow \quad \text{radio } \uparrow \]
Hence, in a simple linear regression which only examines sales versus newspaper, we will observe that higher values of newspaper tend to be associated with higher values of sales, even though newspaper advertising is not directly associated with sales.
So newspaper advertising is a surrogate for radio advertising; newspaper gets “credit” for the association between radio on sales.
Warning
When two predictors are highly correlated, we will begin to see issues when fitting the model. This is known as multicollinearity.
Citizen, 0 for NotCitizen); sex (1 for Female, 0 for NotFemale); employment status (1 for Employed, 0 for Unemployed), and so on.If a qualitative predictor (also known as a factor) only has two levels (categories) or possible values, then incorporating it into a regression model is very simple: we create an indicator (or “dummy”) variable that takes on two possible numerical values.
This dummy variable is then included as per usual, replacing the original variable.
Imagine that the markets that provide the data for the Advertising data also could have classification as Urban or Rural. How might this modify the results in terms of spending budgets?
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.85436420 0.525156071 13.0520517 1.829642e-28
TV 0.04770608 0.002705104 17.6355788 2.165366e-42
Type 0.32276638 0.463906929 0.6957568 4.874011e-01
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.85436420 0.525156071 13.0520517 1.829642e-28
TV 0.04770608 0.002705104 17.6355788 2.165366e-42
Type 0.32276638 0.463906929 0.6957568 4.874011e-01
How do we interpret this? We have an additional variable now, called Type, which is made up! But we can pretend it is the Urban/Rural tracking we said it was. Then when Type = 1, the market is Urban; when Type = 0, the market is Rural. And the coefficient was 0.3227664.
Interpretation: When the market is Urban (Type = 1), on average sales are 0.32 units higher than when the market is Rural (and Type = 0). There are interesting things we can do with these factor variables and we’ll see more of this soon!
R is Smart
R will automatically create the necessary dummy variables for you as long as the categories are strings ("blue", "red", "green", … ).
If the categories are numeric, as in the case for a Likert scale (1, 2, 3, 4, 5), then you will need to convert them to factors.
It is a good idea to always convert your categorical variables to factors.
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.88436527 0.571611577 12.0437821 2.304523e-25
TV 0.04813654 0.002728304 17.6433945 2.432002e-42
TypeRural 0.59730497 0.579806893 1.0301791 3.041955e-01
TypeTown -0.32008492 0.544216168 -0.5881577 5.571035e-01
We will discuss factors more in detail in the coming weeks, especially as we discuss the use of ANOVA and related methods.
One level is defined as the Baseline level (or reference level) - this will be absorbed into the (Intercept) term. So in the previous example, where the estimates were
(Intercept) TV TypeRural TypeTown
6.88436527 0.04813654 0.59730497 -0.32008492
we have baseline sales of 6.88 units, with Rural being associated with an increase of 0.6 units, and Town being associated with a decrease of -0.32 units, relative to City.
The two primary assumptions of linear models are that the relationship between the predictors and response are both additive and linear.
The additivity assumption says that the association between a given predictor and the response does not depend on the values of the other predictors. This can be relaxed somewhat.
Fitting a Model vs. Inference
You can fit a linear model to any data set. The conditions of additivity and linearity are not necessary for fitting a model.
However, if you want to make inferences about the model, then these assumptions are necessary, along with constant variance of the residuals and some kind of distributional assumption for the residuals (often the normal distribution is assumed).
Consider the standard linear regression model with two variables,
\[ Y = \beta_0 + \beta_1 X_1 + \beta_2 X_2 + \varepsilon. \]
\[ Y = \beta_0 + \beta_1 X_1 + \beta_2 X_2 + \beta_3 X_1 X_2 + \varepsilon. \]
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.750220203 0.2478713699 27.232755 1.541461e-68
TV 0.019101074 0.0015041455 12.698953 2.363605e-27
radio 0.028860340 0.0089052729 3.240815 1.400461e-03
TV:radio 0.001086495 0.0000524204 20.726564 2.757681e-51
One of the points of the metrics we discussed earlier was comparing two models. Let’s compare the two models using \(R^{2}\):
radio units.