Problem: How do we model a non-linear relationship?
Left: Regression of wage onto
age.
Right: Logistic regression for classes
wage>250 and wage<250
\[Y = \beta_0 + \beta_1 f_1(X) + \beta_2 f_2(X) + \dots + \beta_d f_d(X) + \epsilon.\] - Fit this model through least-squares regression: \(f_j\)’s are nonlinear, model is linear!
Define a set of knots \(\xi_1< \xi_2 < \dots<\xi_K\).
We want the function \(f\) in \(Y= f(X) + \epsilon\) to:
It turns out, we can write \(f\) in terms of \(K+3\) basis functions:
\[f(X) = \beta_0 + \beta_1 X + \beta_2 X^2 + \beta_3 X^3 + \beta_4 h(X,\xi_1) + \dots + \beta_{K+3} h(X,\xi_K)\]
\[ h(x,\xi) = \begin{cases} (x-\xi)^3 \quad \text{if }x>\xi \\ 0 \quad \text{otherwise} \end{cases} \]
Spline which is linear instead of cubic for \(X<\xi_1\), \(X>\xi_K\).
The predictions are more stable for extreme values of \(X\).
The locations of the knots are typically quantiles of \(X\).
The number of knots, \(K\), is chosen by cross validation.
Splines can fit complex functions with few parameters.
Polynomials require high degree terms to be flexible.
High-degree polynomials can be unstable at the edges.
Find the function \(f\) which minimizes
\[\color{Blue}{\sum_{i=1}^n (y_i - f(x_i))^2} + \color{Red}{ \lambda \int f''(x)^2 dx}\]
The RSS when using \(f\) to predict.
A penalty for the roughness of the function.
The minimizer \(\hat f\) is a natural cubic spline, with knots at each sample point \(x_1,\dots,x_n\).
Obtaining \(\hat f\) is similar to a Ridge regression.
\[\int f''(x)^2 dx\]
is minimized by a natural cubic spline.
\[f(x) = \beta_0 + \beta_1 f_1(x) + \dots \beta_{n+3} f_{n+3}(x)\]
\[ (y - \mathbf N\beta)^T(y - \mathbf N\beta) + \lambda \beta^T \Omega_{\mathbf N}\beta, \]
where \(\Omega_{\mathbf N}(i,j) = \int N_i''(t) N_j''(t) dt\).
\[ (y - \mathbf N\beta)^T(y - \mathbf N\beta) + \lambda \beta^T \Omega_{\mathbf N}\beta, \]
are \(\hat \beta = (\mathbf N^T \mathbf N + \lambda \Omega_{\mathbf N})^{-1} \mathbf N^T y\).
\[\hat y = \underbrace{\mathbf N (\mathbf N^T \mathbf N + \lambda \Omega_{\mathbf N})^{-1} \mathbf N^T}_{\mathbf S_\lambda} y\]
\[\text{Trace}(\mathbf S_\lambda)= \mathbf S_\lambda(1,1) + \mathbf S_\lambda(2,2) + \cdots + \mathbf S_\lambda(n,n) \]
| Natural cubic splines | Smoothing splines |
| Fix the locations of \(K\) knots at quantiles of \(X\) and number of knots \(K<n\). | Put \(n\) knots at \(x_1,\dots,x_n\). |
| Find the natural cubic spline \(\hat f\) which minimizes the RSS:\(\sum_{i=1}^n (y_i - f(x_i) )^2\) with these knots. | Find the fitted values \(\hat f(x_1),\dots,\hat f(x_n)\) through an algorithm similar to Ridge regression. |
| Choose \(K\) by cross validation. | Choose smoothing parameter \(\lambda\) by cross validation. |
We typically choose \(\lambda\) through cross validation.
Fortunately, we can solve the problem for any \(\lambda\) with the same complexity of diagonalizing an \(n\times n\) matrix.
There is a shortcut for LOOCV: \[ \begin{aligned} RSS_\text{loocv}(\lambda) &= \sum_{i=1}^n (y_i - \hat f_\lambda^{(-i)}(x_i))^2 \\ &= \sum_{i=1}^n \left[\frac{y_i-\hat f_\lambda(x_i)}{1-\mathbf S_\lambda(i,i)}\right]^2 \end{aligned} \]
To predict the regression function \(f\) at an input \(x\):
\[\hat{\beta}(x) = \text{argmin}_{(\beta_0, \beta_1)} \sum_{i=i}^n K_i(x) (y_i - \beta_0 -\beta_1 x_i)^2.\]
Set \(K_i(x)=1\) if \(x_i\) is one of \(x\)’s \(k\) nearest neighbors.
Perform a regression with only an intercept; i.e. find \(\beta_0\) which minimizes
\[\hat{\beta}_0(x) = \text{argmin}_{\beta_0} \sum_{i=i}^n K_i(x)(y_i - \beta_0)^2.\]
\[K_i(x) = \exp(-\|x-x_i\|^2/2\lambda)\]
\[\;\;\mathtt{wage} = \beta_0 + \beta_1\times \mathtt{year} + \beta_2\times \mathtt{age} + \beta_3 \times\mathtt{education} +\epsilon\]
\[\longrightarrow \;\;\mathtt{wage} = \beta_0 + f_1(\mathtt{year}) + f_2(\mathtt{age}) + f_3(\mathtt{education}) +\epsilon\]
\[\mathtt{wage} = \beta_0 + f_1(\mathtt{year}) + f_2(\mathtt{age}) + f_3(\mathtt{education}) +\epsilon\]
This works for smoothing splines and local regression.
For smoothing splines this is a descent method, descending on convex loss …
Initialize \(\hat{\beta}^{(0)} = 0\) and, (say).
Given \(\hat{\beta}^{(T-1)}\), choose a coordinate \(0 \leq k(T) \leq p\) and find \[ \begin{aligned} \hat{\alpha}(T) &= \text{argmin}_{\alpha} \sum_{i=1}^n\left(Y_i - \hat{\beta}^{(T-1)}_0 - \sum_{j:j \neq k(T)} X_{ij} \hat{\beta}^{(T-1)}_j - \alpha X_{ik(T)}\right)^2 \\ &= \frac{\sum_{i=1}^n X_{ik(T)}\left(Y_i - \hat{\beta}^{(T-1)}_0 - \sum_{j: j \neq k(T)} X_{ij} \hat{\beta}^{(T-1)}_j\right)} {\sum_{i=1}^n X_{ik(T)}^2} \end{aligned} \]
Set \(\hat{\beta}^{(T)}=\hat{\beta}^{(T-1)}\) except \(k(T)\) entry which we set to \(\hat{\alpha}(T)\).
Iterate
glmnetGAMs are a step from linear regression toward a fully nonparametric method.
The only constraint is additivity. This can be partially addressed by adding key interaction variables \(X_iX_j\) (or tensor product of basis functions – e.g. polynomials of two variables).
We can report degrees of freedom for many non-linear functions.
As in linear regression, we can examine the significance of each of the variables.
Wageyear: natural spline with df=4.
age: natural spline with df=5.
education: factor.
Wageyear: smoothing spline with df=4.
age: smoothing spline with df=5.
education: step function
We can model the log-odds in a classification problem using a GAM:
\[\log \frac{P(Y=1\mid X)}{P(Y=0\mid X)} = \beta_0 + f_1(X_1) + \dots + f_p(X_p).\]
Again fit by backfitting …
Wage>250year: linear
age: smoothing spline with df=5
education: step function
Wage>250education == "<HS"