Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Recap: Linear Regression

We have so far studied (multiple) linear regression models. The linear model is written as:

y=Xβ+ϵ\begin{align*} y = X \beta + \epsilon \end{align*}

where the data is represented by the n×1n \times 1 vector yy and the n×(m+1)n \times (m+1) matrix XX whose first column is the column of ones. The unknown parameter vector is the (m+1)×1(m+1) \times 1 vector β\beta and the error vector ϵ\epsilon has i.i.d N(0,σ2)N(0, \sigma^2) components. Equivalently, we can write ϵ∼Nn(0,σ2In)\epsilon \sim N_n(0, \sigma^2 I_n) (this is the multivariate normal distribution with mean vector 0 and covariance matrix σ2In\sigma^2 I_n).

In the last lab, we considered the problem of fitting a cubic trend function to the US GDP time series dataset. If yty_t denotes the US GDP corresponding to time tt, we are fitting the model:

yt=β0+β1t+β2t2+β3t3+ϵt\begin{align*} y_t = \beta_0 + \beta_1 t + \beta_2 t^2 + \beta_3 t^3 + \epsilon_t \end{align*}

This is a special case of multiple linear regression with

y=(y1y2⋅⋅⋅yn)   X=(1111122223133233⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅1nn2n3)   β=(β0β1β2β3)   ϵ=(ϵ1ϵ2⋅⋅⋅ϵn)y = \begin{pmatrix} y_1\\ y_2 \\ \cdot \\ \cdot \\ \cdot \\ y_n \end{pmatrix} ~~~ X = \begin{pmatrix} 1 & 1 & 1 & 1 \\ 1 & 2 & 2^2 & 2^3 \\ 1 & 3 & 3^2 & 3^3 \\ \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot \\ 1 & n & n^2 & n^3 \end{pmatrix} ~~~ \beta = \begin{pmatrix} \beta_0 \\ \beta_1 \\ \beta_2 \\ \beta_3 \end{pmatrix} ~ ~ ~ \epsilon = \begin{pmatrix} \epsilon_1 \\ \epsilon_2 \\ \cdot \\ \cdot \\ \cdot \\ \epsilon_n \end{pmatrix}

We fit this model in statsmodels using the OLS function.

  observation_date      GDP
0       1947-01-01  243.164
1       1947-04-01  245.968
2       1947-07-01  249.585
3       1947-10-01  259.745
4       1948-01-01  265.742
5       1948-04-01  272.567
6       1948-07-01  279.196
7       1948-10-01  280.366
8       1949-01-01  275.034
9       1949-04-01  271.351
<Figure size 640x480 with 1 Axes>
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                    GDP   R-squared:                       0.995
Model:                            OLS   Adj. R-squared:                  0.995
Method:                 Least Squares   F-statistic:                 2.000e+04
Date:                Mon, 14 Sep 2026   Prob (F-statistic):               0.00
Time:                        22:19:25   Log-Likelihood:                -2402.2
No. Observations:                 311   AIC:                             4812.
Df Residuals:                     307   BIC:                             4827.
Df Model:                           3                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const        292.1841    126.498      2.310      0.022      43.271     541.097
x1            -2.5806      3.506     -0.736      0.462      -9.479       4.317
x2             0.0759      0.026      2.910      0.004       0.025       0.127
x3             0.0007    5.5e-05     12.093      0.000       0.001       0.001
==============================================================================
Omnibus:                       74.600   Durbin-Watson:                   0.095
Prob(Omnibus):                  0.000   Jarque-Bera (JB):              746.989
Skew:                           0.635   Prob(JB):                    6.21e-163
Kurtosis:                      10.485   Cond. No.                     4.63e+07
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 4.63e+07. This might indicate that there are
strong multicollinearity or other numerical problems.

Residual Sum of Squares (RSS) in linear regression

One of the most important quantities in linear regression is the Residual Sum of Squares (RSS). This is defined as the sum of the squares of residuals. The residuals are defined by:

ei=yi−y^i=yi−β^0−β^1xi1−⋯−β^mxim.\begin{align*} e_i = y_i - \hat{y}_i = y_i - \hat{\beta}_0 - \hat{\beta}_1 x_{i1} - \dots - \hat{\beta}_m x_{im}. \end{align*}

In vector form, the residual vector with components e1,…,ene_1, \dots, e_n is given by

e=y−y^\begin{align*} e = y - \hat{y} \end{align*}

where y^=Xβ^\hat{y} = X \hat{\beta} is the vector of fitted values.

The Residual Sum of Squares (RSS) is defined as

RSS=∑i=1nei2=∥e∥2=∥y−Xβ^∥2.\begin{align*} RSS = \sum_{i=1}^n e_i^2 = \|e\|^2 = \|y - X \hat{\beta}\|^2. \end{align*}

In class, we used the notation S(β)S(\beta) for the squares function:

S(β)=∥y−Xβ∥2.\begin{align*} S(\beta) = \|y - X \beta\|^2. \end{align*}

We can then see that RSS is the smallest possible value of S(β)S(\beta). It is also the value of S(β)S(\beta) evaluated at β=β^\beta = \hat{\beta} where β^\hat{\beta} is the least squares estimate:

RSS=inf⁡βS(β)=S(β^).\begin{align*} RSS = \inf_{\beta} S(\beta) = S(\hat{\beta}). \end{align*}

RSS can be used as a measure of the goodness of fit of a linear regression model. However, one needs to be careful in comparing RSS values across models with different numbers of parameters (in that case, the RSS of a bigger model i.e., the model with more parameters will usually be lower).

The RSS can be computed using the OLS output in the following way.

93203230.55790283
93203230.55790283

Nonlinear Regression Models

Next we shall study models where some parameters are appearing in a nonlinear fashion. A simple example is given by:

yt=β0+β1t+β2(t−c)++ϵt.\begin{align*} y_t = \beta_0 + \beta_1 t + \beta_2 (t - c)_+ + \epsilon_t. \end{align*}

As a motivating example for this model, we consider the following dataset on annual U.S. corn yields from 1866 to 1955, measured in bushels per acre (this dataset is taken from USDA NASS, Crop Production Historical Track Records).

   year  yield_bushels_per_acre
0  1866                    24.3
1  1867                    24.7
2  1868                    26.2
3  1869                    21.8
4  1870                    29.3
5  1871                    27.2
6  1872                    29.4
7  1873                    22.9
8  1874                    22.2
9  1875                    27.7
    year  yield_bushels_per_acre
80  1946                    37.2
81  1947                    28.6
82  1948                    43.0
83  1949                    38.2
84  1950                    38.2
85  1951                    36.9
86  1952                    41.8
87  1953                    40.7
88  1954                    39.4
89  1955                    42.0
<Figure size 640x480 with 1 Axes>

We work with corn yields in their original units, bushels per acre. Thus, a slope measures the annual increase in yield in bushels per acre per year. We do not take logarithms for this example.

If we fit a linear regression model to this data, we will get an estimate of the overall annual increase in corn yield:

yt=β0+β1t+ϵt\begin{align*} y_t = \beta_0 + \beta_1 t + \epsilon_t \end{align*}

Here t=1t = 1 corresponds to 1866.

                              OLS Regression Results                              
==================================================================================
Dep. Variable:     yield_bushels_per_acre   R-squared:                       0.260
Model:                                OLS   Adj. R-squared:                  0.252
Method:                     Least Squares   F-statistic:                     30.98
Date:                    Mon, 14 Sep 2026   Prob (F-statistic):           2.78e-07
Time:                            22:25:23   Log-Likelihood:                -261.63
No. Observations:                      90   AIC:                             527.3
Df Residuals:                          88   BIC:                             532.3
Df Model:                               1                                         
Covariance Type:                nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         23.1776      0.952     24.343      0.000      21.285      25.070
x1             0.1012      0.018      5.566      0.000       0.065       0.137
==============================================================================
Omnibus:                        0.945   Durbin-Watson:                   0.957
Prob(Omnibus):                  0.624   Jarque-Bera (JB):                0.448
Skew:                          -0.064   Prob(JB):                        0.800
Kurtosis:                       3.321   Cond. No.                         106.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
<Figure size 640x480 with 1 Axes>

The fitted slope coefficient is approximately 0.1012. Its interpretation is that corn yield increases, on average over this whole period, by about 0.10 bushels per acre each year.

The simple linear regression model does not describe the change from nearly constant yields to increasing yields. The quality of the fit can be assessed using the Residual Sum of Squares (RSS).

1765.1484468041324

The simple linear regression model uses the same annual increase throughout the period. The plot suggests that yields were approximately constant in the earlier years, but began increasing in the later years. We can instead consider the following model:

yt=β0+β1t+β2(t−c)++ϵty_t = \beta_0 + \beta_1 t + \beta_2 (t - c)_+ + \epsilon_t

This model uses β1\beta_1 for the slope before cc, and β1+β2\beta_1 + \beta_2 for the slope after cc.

If cc is known, then this is again linear regression (but now it is multiple linear regression as opposed to simple linear regression), and we can fit this model as follows. In this code, we are hard-coding the value of cc.

                              OLS Regression Results                              
==================================================================================
Dep. Variable:     yield_bushels_per_acre   R-squared:                       0.314
Model:                                OLS   Adj. R-squared:                  0.298
Method:                     Least Squares   F-statistic:                     19.88
Date:                    Mon, 14 Sep 2026   Prob (F-statistic):           7.74e-08
Time:                            22:26:23   Log-Likelihood:                -258.27
No. Observations:                      90   AIC:                             522.5
Df Residuals:                          87   BIC:                             530.0
Df Model:                               2                                         
Covariance Type:                nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         26.6509      1.624     16.412      0.000      23.423      29.878
x1            -0.1111      0.084     -1.330      0.187      -0.277       0.055
x2             0.2594      0.100      2.599      0.011       0.061       0.458
==============================================================================
Omnibus:                        1.890   Durbin-Watson:                   1.031
Prob(Omnibus):                  0.389   Jarque-Bera (JB):                1.299
Skew:                          -0.250   Prob(JB):                        0.522
Kurtosis:                       3.309   Cond. No.                         218.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
<Figure size 640x480 with 1 Axes>

To assess the quality of fit of this model, we again look at the residual sum of squares.

1637.9835634183632 1765.1484468041324

The RSS decreases from about 1765.15 to 1637.98. While this is an improvement, it is quite possible that a different value of cc would give an even smaller value of RSS. Below we try a different value of cc.

874.8665318763375

This RSS value is much smaller compared to the previous ones. It is natural here to vary cc over all its possible values, and then to find the cc which gives the smallest value of RSS. This will be our method of estimating cc. More precisely, we estimate cc by the following method:

  1. For each fixed value of cc, calculate RSS

  2. Use the value of cc with the smallest RSS as the estimate

The following function calculates the value of RSS for each fixed value of cc.

We compute RSS(c)RSS(c) on a grid of 1000 candidate values between 1 and nn, as follows. The grid includes noninteger values, so the estimated change point can fall between two observation years.

<Figure size 640x480 with 1 Axes>

The estimate c^\hat{c} is obtained by minimizing RSS(c)RSS(c) as follows.

70.75675675675676
1935.7567567567567
772.9049497954343 1765.1484468041324 874.8665318763375

Note that the value of RSS at c^\hat{c} is much smaller than the RSS at the previously tried cc values.

If we run a linear regression with cc fixed at c^\hat{c}, the fitted values will approximate the data much better.

<Figure size 640x480 with 1 Axes>

In the nonlinear regression model, we also have the parameters β0,β1,β2\beta_0, \beta_1, \beta_2. These can be estimated by simply fixing c=c^c = \hat{c}, and then running linear regression. This is done as follows:

const    26.282432
x1       -0.010014
x2        0.902100
dtype: float64

The estimated change year (corresponding to c^\hat{c}) is approximately 1935.76. The estimate of β^1\hat{\beta}_1 is about -0.010, and the estimate of β^2\hat{\beta}_2 is about 0.902. Thus, the fitted annual increase in corn yield is approximately

−0.010bushels per acre per year before the change,−0.010+0.902=0.892bushels per acre per year after the change.\begin{align*} -0.010 \quad &\text{bushels per acre per year before the change},\\ -0.010 + 0.902 = 0.892 \quad &\text{bushels per acre per year after the change}. \end{align*}

In other words, the fitted trend is nearly flat before about 1936, and yield subsequently increases by approximately 0.89 bushels per acre each year.

This timing is consistent with the historical adoption of hybrid corn (read about this).

For the linear regression model, we have studied inference (uncertainty quantification) for the parameters β\beta. In this nonlinear regression model, one can ask for uncertainty quantification for the parameter cc also. This will be covered in lectures this week.

Another Nonlinear Regression Model

The wtloss dataset in the R package MASS contains 52 measurements of one person’s body weight over an eight-month weight rehabilitation programme. Weight is recorded in kilograms and time in elapsed days. The data were supplied by Dr T. Davies, Adelaide.

    Days  Weight
0      0  184.35
1      4  182.51
2      7  180.45
3      7  179.91
4     11  177.91
5     18  175.81
6     24  173.11
7     30  170.06
8     32  169.31
9     43  165.10
10    46  163.11
11    60  158.30
12    64  155.80
13    70  154.31
14    71  153.86
15    71  154.20
16    73  152.20
17    74  152.80
18    84  150.30
19    88  147.80
20    95  146.10
21   102  145.60
22   106  142.50
23   109  142.30
24   115  139.40
25   122  137.90
26   133  133.70
27   137  133.70
28   140  133.30
29   143  131.20
30   147  133.00
31   148  132.20
32   149  130.80
33   150  131.30
34   153  129.00
35   156  127.90
36   161  126.90
37   164  127.70
38   165  129.50
39   165  128.40
40   170  125.40
41   176  124.90
42   179  124.90
43   198  118.20
44   214  118.20
45   218  115.30
46   221  115.70
47   225  116.00
48   233  115.50
49   238  112.60
50   241  114.00
51   246  112.60
<Figure size 640x480 with 1 Axes>

This is an example of a time series dataset where the time is not regular i.e., the gaps between the observations is not uniform. So each data point here comes with a specific time stamp (the time stamp is also part of the dataset).

Let us first fit a simple linear regression to this dataset.

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 Weight   R-squared:                       0.971
Model:                            OLS   Adj. R-squared:                  0.970
Method:                 Least Squares   F-statistic:                     1665.
Date:                Mon, 14 Sep 2026   Prob (F-statistic):           4.72e-40
Time:                        23:04:35   Log-Likelihood:                -139.93
No. Observations:                  52   AIC:                             283.9
Df Residuals:                      50   BIC:                             287.8
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const        176.8902      0.988    179.103      0.000     174.907     178.874
x1            -0.2907      0.007    -40.804      0.000      -0.305      -0.276
==============================================================================
Omnibus:                        7.380   Durbin-Watson:                   0.142
Prob(Omnibus):                  0.025   Jarque-Bera (JB):                6.121
Skew:                           0.738   Prob(JB):                       0.0469
Kurtosis:                       2.196   Cond. No.                         271.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
<Figure size 640x480 with 1 Axes>

Here is the RSS for this model:

662.1403861707598

From the dataset, it is clear that the weight loss is initially steep but it is gradually flattening over time. This behaviour is better captured by the model:

yt=β0+β1e−ct+ϵt.\begin{align*} y_t=\beta_0+\beta_1 e^{-ct}+\epsilon_t. \end{align*}

Here t=0t=0 is the beginning of the record.

  • β0\beta_0 is the horizontal asymptote of the fitted curve.

  • β0+β1\beta_0+\beta_1 is the fitted initial weight.

  • cc controls how quickly the curve approaches its asymptote. For positive β1\beta_1 and cc, the curve decreases and gradually flattens.

For fixed cc, this is linear regression with explanatory variable e−cte^{-ct}. We first hard-code c=0.002c=0.002 per day and then fit the linear regression.

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 Weight   R-squared:                       0.989
Model:                            OLS   Adj. R-squared:                  0.989
Method:                 Least Squares   F-statistic:                     4460.
Date:                Mon, 14 Sep 2026   Prob (F-statistic):           1.49e-50
Time:                        23:08:02   Log-Likelihood:                -114.79
No. Observations:                  52   AIC:                             233.6
Df Residuals:                      50   BIC:                             237.5
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         -4.7507      2.223     -2.137      0.038      -9.216      -0.286
x1           184.6981      2.766     66.785      0.000     179.143     190.253
==============================================================================
Omnibus:                        5.690   Durbin-Watson:                   0.333
Prob(Omnibus):                  0.058   Jarque-Bera (JB):                4.736
Skew:                           0.637   Prob(JB):                       0.0936
Kurtosis:                       2.250   Cond. No.                         14.6
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
<Figure size 640x480 with 1 Axes>

Here is the RSS for this fixed cc model, compared to the RSS for the simple linear trend model.

251.77772269274698 662.1403861707598

With c=0.002c=0.002, RSS is about 251.78, compared with 662.14 for the straight line. Let us now try a different value of cc: c=0.01c=0.01 to see how changing the nonlinear parameter changes the fit.

585.3691417017087

Now the RSS has become worse (compared to c=0.002c = 0.002) which means that the fit must be worse also.

<Figure size 640x480 with 1 Axes>

Search for the value of cc with the smallest RSS

We use exactly the same method as for the corn-yield data:

  1. For each fixed value of cc, fit the linear coefficients and calculate RSS.

  2. Choose the value of cc with the smallest RSS.

The function below retains the structure of rss(c) in the original lab. The data vector y, its length n, and the actual observation times x have already been defined.

We evaluate 1000 candidate rates from 0.0001 to 0.03 per day. We exclude c=0c=0, where the exponential column would equal the intercept column.

The fitted rate should be inside the search interval. If a minimum occurs at an endpoint, the interval needs to be reconsidered.

Below we plot the RSS as cc varies over this range.

<Figure size 640x480 with 1 Axes>

The estimate c^\hat c is obtained by minimizing RSS(c)RSS(c) over the grid.

0.004888788788788789
39.24516434939978 662.1403861707598 585.3691417017087

The best grid value is c^≈0.004889\hat c\approx 0.004889, which is inside the search interval. Its RSS is about 39.25. We now fix c=c^c=\hat c and fit the linear coefficients again.

<Figure size 640x480 with 1 Axes>

The estimates of β0\beta_0 and β1\beta_1 are obtained from this OLS fit.

const     81.427616
x1       102.636219
dtype: float64
Fitted value at time 0: 184.0638348838226

Plotting the residuals for the nonlinear regression model

A smaller RSS does not by itself establish that a model is adequate. Below, we plot the residuals against the actual measurement times to look for systematic departures from the fitted curve.

<Figure size 640x480 with 1 Axes>

The residuals are mostly small, within a few kilograms, and do not show the broad curvature missed by a straight line. This supports the exponential curve as a description of the observed trend. It does not prove independence or normality of the errors.

Cross Validation

Below we compare this nonlinear regression model with linear regression for prediction. We use the first 42 observations, through day 176, for fitting and the remaining 10 observations, days 179–246, for checking predictions.

Note now that cc has to be estimated only on the training dataset.

c estimated using only the earlier observations: 0.004888788788788789

Predictions are obtained for the future observations in the following way.

   Day  Observed  Nonlinear prediction  Linear prediction
0  179     124.9                124.26             120.52
1  198     118.2                120.47             114.21
2  214     118.2                117.54             108.91
3  218     115.3                116.84             107.58
4  221     115.7                116.33             106.59
5  225     116.0                115.65             105.26
6  233     115.5                114.34             102.61
7  238     112.6                113.55             100.95
8  241     114.0                113.08              99.96
9  246     112.6                112.32              98.30
Nonlinear test RMSE: 1.0969728252201651
Linear test RMSE: 10.406896668707475
<Figure size 640x480 with 1 Axes>

Clearly the nonlinear model gives much better predictions compared to the linear model.