AMOD-5440
Trent University
2026-10-09
In order to add polynomial terms in R, we use the I() function:
We can also use the poly() function:
Include All Terms Up to Degree \(d\)
If you want to include the the order \(d\) term, you need to include all polynomial terms up to \(d\).
Think of this in the context of \(y = \beta_{0} + \beta_{1}x\): if we do not include the intercept term, then the slope estimate will not be correct.
Same idea as we increase the degree \(d\).
There’s really not much more to this idea:
Polynomials are actually a special case of something called basis functions.
The idea:
For example, if we have \(b_1(X)\), \(b_2(X)\), etc., then \[ y_i = \beta_0 + \beta_1 \cdot b_1(x_i) + \beta_2 \cdot b_2(x_i) + \cdots + \beta_p b_p(x_i) + \varepsilon_i. \] These basis functions are fixed, known, and pre-specified.
age = 50.age = 50, but the connection looks weird because it’s not smoothage = 50, and the first derivatives match.Derivations
The mathematical details on how the basis functions are defined are provided in the textbook: we won’t worry too much about them.
Let’s focus on how to use them!
Age=50 for both sections needs to be the same). This results in \(4 \times (K+1) - K = 3K + 4 = 7\) degrees of freedom.Note
In R, remove one further degree of freedom when using bs() or ns() because by default, we remove the intercept (it will be added back when performing regression).
Call:
smooth.spline(x = Wage$age, y = Wage$wage, cv = TRUE)
Smoothing Parameter spar= 0.6988943 lambda= 0.02792303 (12 iterations)
Equivalent Degrees of Freedom (Df): 6.794596
Penalized Criterion (RSS): 75215.9
PRESS(l.o.o. CV): 1593.383
In Week 2, we fit logistic regression models. Although the syntax looked very similar to linear regression, we had to specify
This told R that our response variable was binary rather than continuous.
It turns out that logistic regression is part of a much larger class of models called Generalized Linear Models (GLMs).
By specifying a family, we tell R:
Recall that ordinary linear regression assumes:
We had \[ Y = \beta_0+\beta_1x_1+\cdots+\beta_px_p + \varepsilon, \] which made
\[ \mu = E(Y) = \beta_0+\beta_1x_1+\cdots+\beta_px_p. \]
Another way to write this is
\[ g(\mu) = \beta_0+\beta_1x_1+\cdots+\beta_px_p, \]
where
\[ g(\mu)=\mu. \]
This is called the identity link function.
Ordinary linear regression is therefore a special case of a Generalized Linear Model.
GLMs extend linear regression to situations where the response variable is not normally distributed.
Rather than assuming
\[ \mu = \beta_0+\beta_1x_1+\cdots+\beta_px_p, \]
we instead assume
\[ g(\mu) = \beta_0+\beta_1x_1+\cdots+\beta_px_p. \]
The function \(g\) is called the link function because it links the mean response to a linear combination of the predictors.
Different choices of response distribution lead to different link functions.
Other commonly used GLMs include models based on the Gamma, inverse-Gaussian, and negative binomial distributions.
Regardless of the distribution we choose, the goal remains the same:
\[ g(\mu) = \beta_0+\beta_1x_1+\cdots+\beta_px_p. \]
GLMs allow us to keep the familiar regression framework while adapting it to many different types of response variables.
Consider a data set from the book called Bikeshare.
bikers: the number of hourly users of a bike sharing program in Washington, DCbikers using:
mnth (month of the year, factor)hr (hour of the day, factor (0-23))workingday (indicator variable, 1 if neither a weekend nor a holiday)temp (normalized temperature in \(^{\circ}\)C, numeric)weathersit (describes weather; clear, misty or cloudy, light rain or light snow, heavy rain or heavy snow, factor)bikers is integer valued and non-negativeIf \(Y\) follows the Poisson distribution, then \[
\text{P}[Y = k] = \frac{e^{-\lambda}\lambda^k}{k!}, \quad \text{ for } k=0, 1, \cdots
\] The parameter of a Poisson is \(\lambda\), the so-called rate. We will model bikers as a Poisson random variate by using a GLM (glm) framework in R.
The Poisson distribution is controlled by a single parameter, \(\lambda\), called the rate.
\[ E(Y)=\mathrm{Var}(Y)=\lambda \]
As \(\lambda\) increases, we expect larger counts to occur more frequently.
Suppose \(Y\) is the number of people using a bike in an hour.
Changing \(\lambda\) shifts the entire distribution of possible counts.
For the bike-sharing data, it would be unreasonable to assume that all observations have the same rate.
We would expect:
Instead of using a single value of \(\lambda\), we allow \(\lambda\) to change from observation to observation.
\[ \lambda_i = f(x_i) \]
where \(x_i\) represents the predictor values for observation \(i\).
Since \(\lambda_i\) must always be positive,
\[ \lambda_i > 0, \]
we cannot model it directly using a linear equation.
Instead, we model the logarithm of the rate:
\[ \log(\lambda_i) = \beta_0+\beta_1x_{i1}+\cdots+\beta_px_{ip}. \]
This is called the log link.
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.885009924 0.003230805 1202.489746 0.000000e+00
workingday -0.008888259 0.001943032 -4.574427 4.775243e-06
temp 2.129054163 0.004788834 444.587172 0.000000e+00
weathersitcloudy/misty -0.042218993 0.002131712 -19.805205 2.684687e-87
weathersitlight rain/snow -0.432089576 0.004022716 -107.412409 0.000000e+00
weathersitheavy rain/snow -0.760994643 0.166681086 -4.565573 4.981322e-06
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.885009924 0.003230805 1202.489746 0.000000e+00
workingday -0.008888259 0.001943032 -4.574427 4.775243e-06
temp 2.129054163 0.004788834 444.587172 0.000000e+00
weathersitcloudy/misty -0.042218993 0.002131712 -19.805205 2.684687e-87
weathersitlight rain/snow -0.432089576 0.004022716 -107.412409 0.000000e+00
weathersitheavy rain/snow -0.760994643 0.166681086 -4.565573 4.981322e-06
All predictors are statistically significant (\(p < 0.001\)).
Wage data, with wage versus age.Generalized Additive Models (GAMs for short) provide a framework for extending linear and generalized linear models by allowing non-linear functions of each predictor, while maintaining the linearity.
A natural way to extend \[ y_i = \beta_0 + \beta_1 x_{i1} + \cdots + \beta_p x_{ip} + \varepsilon_i \] in order to allow for non-linear relationships is to replace each component \(\beta_j x_{ij}\) with a smooth, non-linear function of \(x_{ij}\), \(f_j (x_{ij})\), giving \[ \begin{aligned} y_i &= \beta_0 + \sum_{j=1}^{p} f_j(x_{ij}) + \varepsilon_i \\ &= \beta_0 + f_1(x_{i1}) + f_2(x_{i2}) + \cdots + f_p(x_{ip}) + \varepsilon_i. \end{aligned} \] These were invented in the late 1980s by Trevor Hastie and Rob Tibshirani during their PhDs at Stanford; the book on them came out in the early 1990s.
All of the earlier methods: polynomials, cubic regression splines, natural cubic regression splines, and smoothing splines, can all be used as building blocks for fitting additive models.
Estimate Std. Error t value Pr(>|t|)
(Intercept) 46.949491 4.704182 9.9803733 4.255933e-23
ns(year, 4)1 8.624692 3.466060 2.4883275 1.288867e-02
ns(year, 4)2 3.762266 2.959187 1.2713851 2.036907e-01
ns(year, 4)3 8.126549 4.211446 1.9296340 5.374684e-02
ns(year, 4)4 6.806473 2.396969 2.8396166 4.547335e-03
ns(age, 5)1 45.170123 4.193491 10.7714834 1.435245e-26
ns(age, 5)2 38.449704 5.076174 7.5745439 4.775895e-14
ns(age, 5)3 34.239237 4.382517 7.8126879 7.692241e-15
ns(age, 5)4 48.677634 10.571609 4.6045623 4.306057e-06
ns(age, 5)5 6.557265 8.367147 0.7836919 4.332831e-01
education2. HS Grad 10.983419 2.430148 4.5196494 6.434935e-06
education3. Some College 23.472889 2.561672 9.1631133 9.124942e-20
education4. College Grad 38.313667 2.547137 15.0418574 2.397682e-49
education5. Advanced Degree 62.553971 2.761309 22.6537345 5.566864e-105
From these three panels, we see:
Our next topic is the first that could be considered “machine learning” by some people: decision trees! This will round out the first half of the course.
The material of this week is Chapters 7.1-7.5, 4.6, and 7.7 of ISLR2, discussing polynomial regression, basis functions and spline models, GLMs and GAMs.
Next week, we will continue into Chapter 8, and begin discussing decision trees. Readings are Chapters 8.1, 8.2
