Frequentist Regression Analysis Overview
Frequentist Regression Analysis Overview
1 INTRODUCTION
A regression equation is an equation that relates some dependent or response
variable y to some independent or explanatory variables X ¼ x1, …, xn, and
involves some unknown parameters θ ¼ θ1, …, θp. Regression analysis, which
involves estimating the unknown parameters and the quality of prediction of the
regression function, is a central topic in statistics and there is a large amount of
literature on the subject (for nonlinear regression see, e.g., Bates and Watts
(1988), Rhinehart (2016), or Seber and Wild (1989)). System models are in fact
regression models, so it is important to understand how such regression models
are treated in statistics.
In this chapter we discuss two frequentist approaches to parameter estima-
tion for regression models: least squares and maximum likelihood. We discuss
in detail the usual assumptions that the regression model is correctly specified
(defined rigorously below), and that all model errors have the same variance
and are independent. These assumptions are often not verified by system
models, so we discuss methods of dealing with situations where the
The second assumption, in Equation (4), says that the variance of model
error is the same for all the Xi. Finally, Equation (5) says that all the model errors
are independent. That is, knowing the model error for one individual in the sam-
ple gives us no information about model error for another individual. If each
individual in the sample is chosen at random in the population, then clearly
the model error for individual i0 6¼ i will have no relation to the model error
for individual i. Thus, this assumption is automatically fulfilled if we have a
simple random sample.
To illustrate many of the notions discussed in this chapter, we will use the
data and model given in Example 1. Example 2 illustrates what a large sample
from the target population of Example 1 would look like, if the three assump-
tions of Equations (3)–(5) are satisfied.
25
Grain weight (mg)
20
15
10
with σ 2 ¼ (0.84)2 ¼ 0.706. This implies that the model in Equation (7) is a correct
model because there are parameter values such that the expectation of ε is 0 for
all values of the explanatory variable. The assumption (Equation 4) of constant var-
iance is also satisfied, and all the model errors are independent (assumption in
Equation 5).
Figure 5.2 shows what a large sample from this target population would look like.
It is assumed that the sampling dates (values of DD) are fixed. As can be seen, the
model with the true parameter values goes through the middle of the points for each
value of DD (assumption 1). The spread around the model is the same for all values of
DD (assumption 2). By construction the model errors are independent, though this
cannot be seen from the figure.
30
25
Grain weight (mg)
20
15
10
X
n
SSE ¼ ½yi f ðXi ; θÞ2 (10)
i¼1
where n is the number of data points. We can say the same thing with the fol-
lowing equation:
( )
X
n
θ^OLS ¼ arg min ½ y i f ðXi ; θ Þ 2
(11)
θ i¼1
which says that the OLS parameters are the arguments of the model that min-
imize the sum of squared errors. This is called “ordinary” least squares because
it is a simple sum of squared errors, not a weighted sum, that is to be minimized.
When we discuss how to deal with violations of the assumptions in
Equations (4), (5), we will present other versions of the least squares approach,
namely weighted least squares (WLS) and generalized least squares (GLS).
Suppose that the regression model is linear in the parameters, so that it can
be written
y ¼ θ0 + θ1 x1 + ⋯ + θp xp + e (12)
Let Y ¼ y1, …, yn be the column vector of observed values. The explanatory var-
iables of the sample can be put in matrix form
0 1
1 x11 … x1p
X ¼ @⋮ ⋮ ⋱ ⋮ A
1 xn1 … xnp
where xij is the value of the jth explanatory variable for individual i. The first
column with 1s multiplies the intercept term θ0 in the regression equation. The
regression equation in matrix notation is then
Y ¼ Xθ + ε
where θ is the column vector of parameters and ε ¼ ε1, …, εn is the column
vector of model errors.
EXAMPLE 3 OLS Parameter Estimation for the Model and Data of Example 1
The R function nls was used to estimate the parameters of the logistic model of
Example 1, based on the data presented there. The OLS parameter values are
W^ OLS ¼ 28:17, B^OLS ¼ 4:160, and C^ OLS ¼ 0:01649. The estimated variance of model
error is σ^2 ¼ 0:8364. The variance-covariance matrix of θ^OLS is
OLS
W B C
W 0.1666 0.06338 3.0831e04
B 0.06338 0.1297 4.979e04
C 3.0831e-04 4.979e04 2.002e06
The fit of the model to the data and the model residuals (observed minus simu-
lated values) are shown in Figure 5.3.
Regression Analysis, Frequentist Chapter 5 167
Seed weight 25
20
15
10
0.5
Residulas
0.0
−0.5
−1.0
For a nonlinear model, OLS has similar properties but only asymptotically,
that is, as the size of the sample tends toward infinity (Seber and Wild, 1989,
p. 565). There are also additional constraints on the model, essentially that it
must be well behaved. The difference ^ θ θtrue has a normal distribution asymp-
totically, with
1
^
θ θtrue N 0, σ 2 F0 F (16)
One should neither be too impressed nor too unimpressed by the fact that these
properties only hold asymptotically for nonlinear models. One should not be too
impressed because samples are always finite in size, often quite small, and we
have promised no good qualities for OLS for nonlinear models for finite sample
size. On the other hand, asymptotic properties are far from useless. Imagine an
estimator that did not promise to converge to the true parameter values at least
asymptotically. We would have serious doubts about using such an estimator.
Example 4 illustrates some of the above properties using simulated data based
on the population described in Example 2. We drew repeated samples from the
population, and for each sample calculated the OLS estimates of the parameter
values and of model error variance. In this artificial example we know the true
values θtrue and σ 2, and so can compare the OLS estimates with the true values.
Because we know exactly the distribution of y in the population, we can also cal-
culate the mean squared error of prediction for any value of ^θOLS .
Figure 5.4 shows histograms of B^OLS for the different sample sizes. This again
shows that as sample size increases, there is less and less variability in the estimated
value around Btrue.
100
100
80 150
80
Frequency
60
Frequency
100
Frequency
60
40
40
50
20 20
0 0 0
3.0 4.0 5.0 6.0 3.0 4.0 5.0 6.0 3.0 4.0 5.0 6.0
B B B
FIGURE 5.4 Histograms of the estimated values of parameter B in different samples for
increasing sample size.
170 SECTION A Background
3 MAXIMUM LIKELIHOOD
There is a second major way of obtaining estimates of parameter values other
than least squares, called maximum likelihood (ML). This is an extremely gen-
eral procedure that can be used whenever one can make reasonable assumptions
about the distribution of model error. (Note that for least squares, we did not
need to make assumptions about the distribution of model error, other than con-
stant variance.)
The likelihood that θtrue ¼ θ for some parameter vector θ, written L(θ j Y), is
defined as the probability that Y would equal the observed Y, if θtrue ¼ θ. The ML
estimator of θtrue is then the value of θ that maximizes L(θ j Y). That is,
the estimator is the parameter vector that maximizes the probability of obtaining
the values that were in fact measured.
Consider the simple case, where the model errors are all independent and
normally distributed with the same variance:
yi ¼ f ðXi ; θtrue
Þ + εi
εi N 0, σ 2 (17)
iid
The assumptions about model error here are the same as in Equations (3)–(5),
but with the additional assumption of a normal distribution.
According to Equation (17), if θtrue ¼ θ, then the model errors are εi ¼ yi
f(Xi; θ). The probability of obtaining those error values is
Y
n
1
eðyi f ðXi , θÞÞ =ð2σ Þ
2 2
Probðε1 , …, εn Þ ¼ Lðθj Y Þ ¼
i¼1 ð2πσ 2 Þ1=2
X
1 ðyi f ðXi , θÞÞ2 =ð2σ 2 Þ
¼ e (18)
ð2πσ 2 Þn=2
where we have used the fact that for independent variables, the joint probability
is the product of the individual probabilities.
The ML estimator is the value of θ that maximizes the above expression. The
only part of the expression that involves θ is the exponent, and the expression is
maximized by minimizing
X
n
SSE ¼ ðyi f ðXi , θÞÞ2
i¼1
This is exactly the same expression as for the OLS estimator. In this simple case,
the OLS estimator and the ML estimator are identical.
It is also possible to show that the value of σ 2 that minimizes Equation (18) is
X
n
σ^2ML ¼ ðyi f ðXi , θÞÞ2 =n
i¼1
Regression Analysis, Frequentist Chapter 5 171
Continued
172 SECTION A Background
25
Seed weight
20
15
10
2
Residulas
−2
−4
minimize mean squared error of prediction (MSEP ) for the misspecified model,
asymptotically. White (1981) showed that in general the optimal parameter
values are not equal to the true parameter values. In fact, because the misspe-
cified model need not have the same functional form as the correct model, it
may have parameters that are defined completely differently than the correct
model. However, even if the misspecified model has parameters that are anal-
ogous to those of the correct model, the estimated values do not tend toward the
true parameter values. This is because the optimal parameter values are com-
pensating for errors in model formulation.
A second conclusion concerns MSEP for a misspecified model. White (1981)
showed that for a misspecified model, the OLS estimators tend toward the optimal
parameters that minimize MSEP as the amount of data tends toward infinity. On
the one hand, this is perhaps not so surprising. OLS minimizes the sum of squared
errors. If we do this for larger and larger samples, it is not surprising that we
approach the parameters that minimize mean squared error for the entire popula-
tion. On the other hand, this is a very powerful result. It says that even if the model
is misspecified, the OLS parameters will make the model into the best it can be.
Example 6 illustrates the above two effects of misspecification with an artificial
example. Two different misspecified models are shown. In the first, the functional
dependence on the explanatory variable x is wrong. The asymptotic parameter
values in this case are not comparable to the true parameter values; in the true
response, the parameters describe the exponential increase of y while in the model
the parameters are the coefficients of a linear and quadratic term. In the second
model, the dependence on x has the correct form, but one of the model parameters
is fixed at an incorrect value. This second model is also misspecified because there
is no value of the single estimated parameter that will make model error 0 for all x.
The estimator of the single parameter does not tend asymptotically to the true
value; it tends toward a value that compensates for the error in the fixed parameter.
yi ¼ 0:09
exp ð0:4x
Þ + εi (20)
ei N 0, 0:12
The distribution of x values in the target population is assumed to be x U(5, 15) (the
x values are drawn at random from a uniform distribution with lower and upper
limits of 5 and 15, respectively). All the errors and explanatory variables are mutually
independent. The correctly specified model is then
h1 ðx; θÞ ¼ θ1 + θ2 x 2
h2 ðx; θÞ ¼ θ1 exp ð0:3x Þ
Continued
174 SECTION A Background
Model ^
θOLS,1(θtrue50.09) ^
θOLS,2(θtrue50.4)
h1(x; θ) (wrong function) 7.298 0.1498
h2(x; θ) (wrong coefficient) 0.3464 Fixed at 0.3
True True
35 Model 35 Model
30 30
25 25
20 20
y
15 15
10 10
5 5
0 0
6 8 10 12 14 6 8 10 12 14
x x
FIGURE 5.6 Two misspecified models showing the true expectation of the response (solid line)
and simulated values using parameter values estimated by OLS on a sample of size 30,000 (dashed
line).
Regression Analysis, Frequentist Chapter 5 175
The results show that even though the true response is the same for all values of x,
the estimated parameters are different depending on which range of x is sampled.
This is a result of misspecification.
176 SECTION A Background
A Logistic L3
Standardized residual
–2
10 20
30 40 50
Predicted value
FIGURE 5.7 Plot of standardized residuals for data and model of Example 8. (From
Robert, N., Huet, S., Hennequet, C., Bouvier, A., 1999. Methodology for choosing a model
for wheat kernel growth. Agronomie, 19, 405–417.)
Thus, after the transformation, the residual variance is identical (and equal to 1)
for all xi. Now assumption 2 is satisfied and we can use OLS.
The sum of squared errors to be minimized is now
X
n X
n
SSE ¼ ½yi =σ i f ðxi ; θÞ=σ i 2 ¼ 1=σ 2i ½yi f ðxi ; θÞ2 (22)
i¼1 i¼1
The approach that consists of minimizing the above SSE is called weighted least
squares (WLS) because it is equivalent to weighting each term in OLS by 1/σ i2.
The R function nls allows one to specify a weight for each measurement, and
thus can be used for WLS.
According to Equation (22), we should give less weight to xi values with
large residual variance. This is logical. We should attach less importance to
xi values where there is more variability around f(xi; θ) because then the mea-
sured value contains less information about the true parameter values. If we do
not use different weights, then we give as much importance to points with little
information as to points with high information.
Standardized residual
2
2
1
0
0
–1
–2 –2
–3
10 20 30 40 50 10 20 30 40 50
Predicted value Predicted value
FIGURE 5.8 Residual errors without power transformation (left) and with power transforma-
tion with λ ¼ 1.29 (right). (From Robert, N., Huet, S., Hennequet, C., Bouvier, A., 1999. Method-
ology for choosing a model for wheat kernel growth. Agronomie, 19, 405–417.)
where the product is over the different responses (subscript j) and over measure-
ments of that response (subscript i).
The concentrated likelihood method proceeds in two stages. First, treating θ
as fixed, one calculates the variances of model error that maximize the likeli-
hood. This gives
X
nj
2
σ^2j ðθÞ ¼ 1=nj Yij fj ðXi ; θÞ (26)
i¼1
Equation (27) gives the ML estimator of θ. The minimization can be done, for
example, using the R function optim. The estimated variances of model error
can be calculated from Equation (26), by putting ^θ in place of θ.
The criterion to be minimized in Equation (27) is a product of squared
differences and not a sum, like OLS. Note that this criterion is invariant
to changes in units. Changing the units of any of the outputs would simply
multiply the criterion by a constant, which would not change the estimated
parameter values. This is what we want. We do not want the estimated
parameters to change because we express biomass in g/m2 instead of tons/
ha for example.
An example of the use of concentrated likelihood is the study by Wallach
et al. (2011). The data consisted of measurements of both LAI and aboveground
biomass in multiple fields. It was assumed that model variance was the same for
all observations of each response, but different for the two responses.
course of the season in each of several fields. The fields might be chosen at ran-
dom, but once the field is chosen, then multiple measurements are done in the
same field. The particularities of that field could then influence all the measure-
ments, which would lead to some relation between model errors within the
same field.
The variance-covariance matrix of model errors, noted Σ, is central to con-
sideration of nonindependence of model errors. This is a square matrix of size n
by n, where n is the number of measurements. This matrix has the variances of
the model errors on the diagonal and the covariances in the off-diagonal posi-
tions. If all model errors have the same variance and are uncorrelated (assump-
tions 2 and 3 are satisfied), then Σ has the form
0 1
σ2 0 … 0 0
B0 σ2 … 0 0C
B C
Σ¼B
B⋮ ⋮ ⋱ ⋮ ⋮C C (28)
@0 0 … σ2 0A
0 0 … 0 σ2
There are no off-diagonal terms because all model errors are independent and
therefore all the covariances are zero. All the elements on the diagonal are iden-
tical because we are assuming equal variances. If assumption 3 is violated, then
some of the off-diagonal elements of Σ are nonzero.
Continued
182 SECTION A Background
6
1986
4 1987
Seed weight (mg)
30 1988
Residual
2
20
0
1986 −2
10 1987
1988 −4
5
100 200 300 400 500 600 700 100 200 300 400 500 600 700
DD after anthesis DD after anthesis
FIGURE 5.9 Seed weight data for three years and logistic model fitted using OLS (left),
residuals (right).
the R function chol). Let Y be the vector of all measurements, so Y ¼ (y1, …, yn),
let F be the corresponding vector of simulated values, so F ¼ (f(x1; θ), …,
f(xn; θ)), and Ε the vector of residuals, so Ε ¼ (ε1, …, εn). At the second step,
the measured and simulated values are transformed as follows:
1 1
Y ∗ ¼ RT Y and F∗ ðθÞ ¼ RT FðθÞ (29)
The transformed residuals are Ε∗ ¼ Y∗ F∗(θ) ¼ (RT)1Ε. The variance-
covariance matrix of the transformed residuals is
1
1 1
varðΕ∗ Þ ¼ var RT Ε ¼ RT ΣR1 ¼ RT RT RR1 ¼ I
where I is the identity matrix (the diagonal elements are all 1, the off-diagonal
elements all 0). Thus after transformation, the variance covariance matrix of the
residuals has the form of Equation (28) (with σ 2 ¼ 1). This means that we can
now use OLS on the transformed data.
The SSE criterion to be minimized is
The first line of Equation (30) says that we can use OLS on the transformed
measurements and simulated values. The last line of Equation (30) says that
equivalently, we can work with the original measurements and simulated
values, but then the SSE to be minimized includes the inverse of the
variance-covariance matrix. Minimizing this last expression is called general-
ized least squares.
where the subscript ij refers to the jth measurement on individual i. The random
variables here are W, εij, and εij0 . The model errors have been assumed mutually
Regression Analysis, Frequentist Chapter 5 185
Nup
Prot ¼ P1 + P2
Yld
(
Rmin Napp < Nmax
Res ¼
Rmin + R Napp Nmax Napp Nmax
The model has 11 parameters. All model errors were assumed independent.
Parameter estimation was done in two ways. In the first approach, all parameters were
treated as fixed. Estimation then concerned the 11 fixed parameters and four variances
of model error (for N uptake, yield, grain protein content, and residual soil N). In the
second approach the nine parameters a1, C, Ymax, A, B, P1, P2, Rmin, and R were treated
Continued
186 SECTION A Background
ðiÞ
c. Calculate the ordinary least squares estimate of θ ^θ as in Equation (34),
but with the above replacements. This gives the next approximation,
ði + 1Þ ðiÞ
ð iÞ
^
θ ¼^
θ + θ ^θ .
d. Repeat steps b–c until convergence is achieved (i.e., until there is very little
change in the parameters or in the sum of squared errors).
The behavior of this algorithm is illustrated in Example 12, using the logistic
model and data of Example 1. Note that the first iteration leads to values that
are much worse (higher MSE) than the initial values. Both parameters after the
first iteration are negative. In this particular case, the model can still be evalu-
ated with negative parameter values, and the algorithm goes on to find the OLS
188 SECTION A Background
solution. With other models, negative parameter values might cause an error. To
avoid this problem, it is important to start with good initial estimates of the
parameter values. Often, one can roughly estimate some or all of the parameters
directly from the data. It is worthwhile doing so. If the algorithm does not con-
verge, one can try different starting values.
nls
0.03
0.02
start
67
0.01 5
4
10
C
20
500
30
0.00 50
40
100
3
300
−0.01
2
−0.02
−2 −1 0 1 2 3 4 5
B
FIGURE 5.10 Path of Gauss-Newton algorithm for estimation of two parameters of
logistic model.
As with the Gauss-Newton algorithm, the user must give initial estimates of
the parameter values. This algorithm then begins by creating a simplex, which is
a geometric figure with p + 1 vertices in p-dimensional space where p is the
number of parameters. For two parameters this is just a triangle. For three
parameters it is a tetrahedron.
The algorithm evaluates the SSE at each vertex of the simplex. Then the ver-
tex with the highest SSE (the worst point) is reflected through the centroid of the
other points (reflection). If this new point is better than the current best point,
the algorithm elongates the distance of reflection to go further (expansion). If
the new point is no better than the second worst point, then the distance of
reflection is reduced (contraction). If no new point better than the second worst
point is found, the original simplex is shrunk toward the best point (shrinkage).
0.0 0.0
C
start
−0.4 −0.4
2.8 3.0 3.2 3.4 2.8 3.0 3.2 3.4
B B
0.0 0.0
C
worst
−0.4 −0.4
2.8 3.0 3.2 3.4 2.8 3.0 3.2 3.4
B B
FIGURE 5.11 Steps in the Nelder-Mead algorithm.
190 SECTION A Background
Number of
Evaluations
Method W B C of the Model
Gauss-Newton 28.1713 4.16022 0.0164920 37
(nls)
Nelder-Mean 28.1712 4.15961 0.0164895 285
(optim)
There are many variants of the Gauss-Newton and simplex algorithms, and
also many other types of algorithms, some of which are available with nls or
optim as options. For example, an option with optim is to use a simulated
annealing algorithm, which is a global optimization algorithm (Goffe et al.,
1994). Global algorithms are less sensitive to initial values than Gauss-Newton
type algorithms and are more likely to converge to the optimal solution. An
important drawback of these algorithms is that they often require a long calcu-
lation time. An application of simulated annealing to a crop model is described
by Mavromatis et al. (2001).
The “singular gradient” message means that one of the derivatives is zero, and
so the algorithm cannot continue. This may be because the parameters are uni-
dentifiable or because the starting values are poor and lead the algorithm to
evaluate the model at parameter values that create errors. Nelder-Mead may
also fail to converge if the model is evaluated at a point that causes an error.
The second problem is that the algorithm may seem to converge, but the result
is not the true minimum value of the function but rather a local minimum. This is
more pernicious because one might not even suspect that there is a problem.
A protection here is to start the algorithm at several different initial parameter
values.
It is important to keep the ratio of the number of estimated parameters to the
number of observations relatively low. Many numeric problems result from
trying to estimate numerous parameters simultaneously from a small number
of data. This is in addition to the danger of obtaining very poor parameter
estimates.
The first three lines store the measured grain weight values in the vector obs, the
values of the explanatory variable DD in the vector DD, and then create a data
192 SECTION A Background
frame with two columns; the values of obs are in a column named y and the
values DD in a column named DD. The names of the columns are important.
They must be the same names that appear in formula. The fourth line creates
a list called start, with initial guesses of the value of each parameter. Here
again the names must be the same as the names that appear in formula.
The last line is the call to the nls function. The first argument, the formula,
says that “y is modeled as W/(1 + exp(B C*x).” The tilde indicates that this is a
formula. The R objects that appear in this expression are y, W, B, C, and x. Every
one of these objects must appear by name and be assigned a value in either the
second or third argument. If the name appears in the third argument (the object
start in our example) it is treated as a parameter to be estimated. In our case the
parameters are W, B, and C. The remaining names in formula are y and DD; they
are defined in the second argument (the object data in our example).
In fact, you do not really have to define every variable that is not a parameter
in data. If objects used in the formula are not found in data, then R will search
for these objects among the objects that have been defined previously. How-
ever, it is good programming practice to include all the variables in data to
insure that R is using the values you want.
In the above program, the result of executing nls is stored in the object
logistic. The basic results of nls can be printed by simply putting logistic
on a line by itself. The result is as follows, where the values given for W, B, and
C are their estimated values:
logistic #print the results of nls
Nonlinear regression model
model: y W/(1 + exp(B – C * x))
data: data
WBC
28.17126 4.16022 0.01649
residual sum-of-squares: 5.596
Number of iterations to convergence: 5
Achieved convergence tolerance: 3.181e-06
One can obtain more detailed information by using the instruction summary
(logistic):
summary(logistic,correlation=T)
Formula: y W/(1 + exp(B – C * x))
Parameters:
Estimate Std. Error t value Pr(>jtj)
W 28.171259 0.408285 69.00 2.17e-12 ***
B 4.160219 0.360084 11.55 2.86e-06 ***
C 0.016492 0.001415 11.65 2.68e-06 ***
Signif. codes : 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.8364 on 8 degrees of freedom
Regression Analysis, Frequentist Chapter 5 193
The function fitted operates on nls objects and returns the values calculated
using the calibrated model. Below we use this function to obtain a graph of
observed versus simulated values, to which we add the 1:1 line (Figure 5.12).
fitted(logistic)
[1] 7.888914 12.666716 17.252816 22.052749 25.116203 26.226212
27.527684 27.885441 28.045235 28.105961 28.146948
# graph observed versus predicted values and add 1:1 line
plot(DD,fitted(logistic))
abline(0,1)
The function residuals returns the differences between the observed and sim-
ulated values. Below we plot residuals versus simulated values, and add the line
at 0. We also calculate mean squared error (MSE) (Figure 5.13).
# graph residuals
[Link]()
plot(DD,residuals(logistic), xlab="growing DD since anthesis",
ylab="residulas")
abline(0,0)
194 SECTION A Background
Fitted ([Link]) 25
20
15
10
10 15 20 25
y
FIGURE 5.12 Observed seed weight values versus simulated values, using the fitted function.
0.5
Residulas
0.0
−0.5
−1.0
MSE<-mean(residuals([Link])^2)
MSE
[1] 0.5087239
The function predict uses the regression formula, with the estimated parame-
ters, to evaluate the model for new values of the explanatory variables. The first
argument is the name of the nls object. The second argument is a data frame or
list with the values of the new explanatory variables. In the following code, we
use the calibrated model to predict for each integral value of DD from 195 to
680. Then we plot the observed values and the predicted values (Figure 5.14).
# plot observed values and predicted response for each integer value
of DD
[Link]<-195:680
[Link]<-predict(logistic,list(x=[Link]))
plot(DD,obs, xlab="growing DD since anthesis",ylab="seed weight")
lines([Link],[Link])
The function AIC operates on an nls object and calculates the value of AIC (a
measure of model quality) for the model with the estimated parameters.
# AIC for logistic model with p= 4 parameters (W, B, C and residual
variance)
AIC(logistic)
[1] 31.7823
25
Seed weight
20
15
10
In the first argument of the nls function, the formula says that the model is eval-
uated in the function [Link], which has four arguments. Therefore, the
definition of [Link] must also show four arguments. The arguments are
passed by position (first argument in the call from nls is used as first argument
in [Link], etc.). The names do not need to be identical in the call from
nls and in the function definition, though in this case they are. As in the simple
case, all the object names that appear in the formula (in our case y, x, W, B, and
C) must be assigned values either in the second or third arguments to nls.
The wrapper function that calls the model to predict values of LAI60 is shown
below. It first sets the values of the parameters to the new values sent from nls,
for those parameters that are being estimated. Then there is a loop over sites. For
each value of i, the wrapper prepares the weather data for the ith site, then calls
the maize model and extracts from the results the calculated LAI on day 60 after
198 SECTION A Background
sowing. It returns a vector of predicted values in the same order as the observed
values.
# function called by nls to calculate LAI on day 60
[Link]<-function(nsites,param,weather,sdate,ldate,
LAImax,alpha)
{
# replace the default values of LAImax and alpha with the current values
from nls
param["LAImax"]<-LAImax
param["alpha"]<-alpha
for (i in 1:nsites)
{
# extract weather for current site, and give names to climate variables
[Link]<-[Link](weather[,c(1,4:8),i])
names([Link])<-c("site","year","day","I","Tmax","Tmin")
# Call the maize model for this site-year, and extract LAI on day 60
[Link]<-[Link](param,[Link],sdate,ldate)
LAI60[i]<-[Link]$LAI[[Link]$day==(sdate+60)]
}
# return the vector of predicted values
return(LAI60)
}
The following shows the nls instruction that calls the wrapper function, and the
results.
# nls calls the [Link] wrapper function
# lines that start with + are continuations of previous instruction
nls.LAI60 <-nls([Link][Link](nsites,param,weather,
sdate,ldate,LAImax,alpha),
+ list(nsites =nsites,param =param,weather=weather,sdate= sdate,
ldate=ldate),
+ list(LAImax =7,alpha=0.0024))
summary(nls.LAI60)
Formula: [Link] [Link](nsites, param, weather, sdate, ldate,
LAImax, alpha)
Parameters:
Estimate Std. Error t value Pr(>jtj)
LAImax 7.9410018 0.7813556 10.163 7.52e-06 ***
alpha 0.0021693 0.0002872 7.554 6.58e-05 ***
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.8102 on 8 degrees of freedom
Number of iterations to convergence: 7
Achieved convergence tolerance: 2.034e-06
Regression Analysis, Frequentist Chapter 5 199
$value
[1] 5.595963
$counts
function gradient
204 NA
$convergence
[1] 0
$message
NULL
The value of $value is the final best value of the quantity that is minimized, in
our case the sum of squared errors. $counts gives the number of times the model
function was called (204), and $convergence= 0 indicates successful conver-
gence. Note that the parameter values are in the object logistic$par.
The function that evaluates the model and that is called by optim cannot be
the same as the function that is called by nls because the quantities returned are
different in the two cases. nls requires that the model function return a vector of
predicted values while optim requires the sum of squared differences. That
means, unfortunately, that one cannot simply try nls first and then just replace
nls by optim if nls does not converge. One must replace the function or the
wrapper used with nls by one adapted to optim.
The optim function is not as convenient for OLS parameter estimation as
nls. It does not produce information about the accuracy of the parameter esti-
mates as nls does. Furthermore, the summary function, the predict function,
and other useful functions that take nls objects as arguments do not accept
optim objects as arguments.
There is thus a real advantage to using nls if possible in preference to optim.
If, however, nls does not converge and one uses optim, one can still obtain the
additional information that nls provides, though with additional effort. One
possibility is to use the parameter values estimated by optim as initial guesses
for nls. Perhaps with these presumably very good initial guesses, nls will con-
verge and one can take advantage of the information it provides. If not, one can
write R code to obtain the additional information. We show below how to cal-
culate several of the quantities of interest.
For these calculations, we need to have a function that evaluates the model
for all the contexts for which we have data and returns the simulated values. For
our example, that means that the function must evaluate the model for each
value of DD for which we have data.
# function that evaluates the model
[Link]<-function(params,x)
{
W <-params[1]
B <-params[2]
C <-params[3]
pred<-W/(1+exp(B-C*x))
return(pred)
}
We can calculate the vector of residuals and the estimated residual variance and
standard deviation as follows:
# residuals. logistic is the optim object with results of OLS
# obs is observed grain weights. DD is degree days
residualsOptim <-[Link](logistic$par,DD)
# Calculate estimated residual variance and residual standard deviation
sig2<-sum(residualsOptim^2)/(length(DD)-3)
sig2[1]
0.6994954
sig<-sqrt(sig2)
sig
[1] 0.8363584
The estimated standard deviations of the parameter estimators, and their corre-
lations, are given by Equation (16). This involves numerical derivatives for
which we use the R package numDeriv. This package has a function called
jacobian, which calculates the matrix of derivatives we need. This function
has three arguments of interest to us. The first is the name of the function that cal-
culates the model predictions, in our case [Link]. The second is the
vector of parameter values. The derivatives will be calculated with respect to each
parameter at the point represented by the values of the vector. In our case, this sec-
ond argument is logistic$par, which contains the OLS estimates provided by
optim. The third argument (three commas later because we are skipping two argu-
ments) has the other arguments required by the function [Link]. In
our case there is only one, namely DD. If there were other additional arguments,
they would appear in order, separated by commas. The R program is then
library(numDeriv)
# X is a matrix of derivatives. Size n (data points) x p (parameters)
X <-jacobian([Link],logistic$par,DD)
202 SECTION A Background
The results here are identical to those furnished by the summary function nls.
EXERCISES
Easy
1. In the simple maize model of Chapter 1, the equation for the daily change in
biomass up to flowering is ΔB(d) ¼ B(d + 1) B(d) ¼ RUE[1 eKLAI(d ))]
I(d). The two explanatory variables are leaf area index on day d,
LAI(d), and solar radiation on day d, I(d). What exactly does it mean to
say that this model for ΔB(d) is correctly specified for some particular target
population?
2. Consider the graph of Figure 5.15 for residuals of some model as a function
of the values simulated by the model.
a. Which of the assumptions underlying OLS (Table 5.1) seems to be
satisfied?
b. Which assumptions do not seem to be satisfied?
c. What action would you suggest? Explain.
3. Consider the graph of Figure 5.16 for residuals of some model as a function
of the values simulated by the model.
a. Which of the assumptions underlying OLS (Table 5.1) seems to be
satisfied?
b. Which assumptions do not seem to be satisfied?
c. What action would you suggest? Explain.
Moderate
4. Consider the graph of Figure 5.16 for residuals of some model for four dif-
ferent individuals with 10 measurements per field.
a. What would be the form of the variance-covariance matrix here?
b. Can you use the determinant criterion here? If not, why not?
Regression Analysis, Frequentist Chapter 5 203
1.5
1.0
0.5
eps
0.0
−0.5
−1.0
6 8 10 12 14 16 18
yhat
FIGURE 5.15 Graph of residuals of a model versus simulated values.
4 Individual 1
Individual 2
Individual 3
Individual 4
2
eps
−2
−4
6 8 10 12 14 16 18
yhat
FIGURE 5.16 Residuals for multiple measurements for each of four individuals.
5. Suppose that in the sugar cane model described below, the observed yields
are noted yi , i ¼ 1, …, 6 and the corresponding model predictions are noted
f(xi; α, κ, x0).
a. What is the quantity to be minimized for OLS parameter estimation?
204 SECTION A Background
b. Assuming that the assumptions of Table 5.1 are satisfied and that the
residuals have a normal distribution, what quantity does the maximum
likelihood estimator minimize?
6. The following model describes the relation between sugar cane yield y and
applied Nitrogen x using a Mitscherlich equation:
y ¼ αf1 exp ½κ ðx x0Þg
where α is the asymptotic limit of yield for large x and x0 is applied N for
zero yield. The table below shows data for yield for various N doses (from
Schabenberger and Pierce, 2002, p. 238).
a. Use the nls function to estimate the parameters α, κ, and x0. (Reasonable
starting values are around α ¼ 200, κ ¼ 0.01, x0 ¼ 20.) Use at least two
different starting vectors. What are the OLS parameter estimates?
b. What is the estimated value of residual variance?
c. Create a graph with simulated values every day and observed values as
points.
d. Create a graph of observed versus simulated values. Add the diagonal
line with intercept 0 and slope of 1.
e. Create a graph of residuals. Add the horizontal line for residuals ¼ 0.
REFERENCES
Bates, D.M., Watts, D.G., 1988. Non Linear Regression Analysis and Its Applications. Wiley, New
York.
Box, G.E.P., Cox, D.R., 1964. The analysis of transformations. J. R. Stat. Soc. Ser. B (Stat
Methodol.) 26, 211–252.
Casella, G., Berger, R.L., 1990. Statistical Inference. Wadsworth and Brooks, Pacific Grove, CA.
Darroch, B.A., Baker, R.J., 1990. Grain filling in three spring wheat genotypes: statistical analysis.
Crop Sci. 30 (3), 525–529.
Goffe, W.L., Ferrier, G.D., Rogers, J., 1994. Global optimization of statistical functions with sim-
ulated annealing. J. Econ. 60, 65–99.
Graybill, F.A., 1976. Theory and Application of the Linear Model. Wadsworth and Brooks,
Belmont.
Makowski, D., Wallach, D., 2002. It pays to base parameter estimation on a realistic description of
model errors. Agronomie 22, 179–189.
Regression Analysis, Frequentist Chapter 5 205
Mavromatis, T., Boote, K.J., Jones, J.W., Irmak, A., Shinde, D., Hoogenboom, G., 2001. Developing
genetic coefficients for crop simulation models with data from crop performance trials. Crop
Sci. 41, 40–51.
Rhinehart, R.R., 2016. Nonlinear Regression Modeling for Engineering Applications. John Wiley
and Sons, Ltd., Chichester, UK. [Link]
Robert, N., Huet, S., Hennequet, C., Bouvier, A., 1999. Methodology for choosing a model for wheat
kernel growth. Agronomie 19, 405–417.
Schabenberger, O., Pierce, F.J., 2002. Contemporary Statistical Models for the Plant and Soil
Sciences. CRC Press, Boca Raton.
Seber, G.A.F., Wild, C.J., 1989. Nonlinear Regression. Wiley, New York.
Wallach, D., Buis, S., Lecharpentier, P., Bourges, J., Clastre, P., Launay, M., et al., 2011. A package
of parameter estimation methods and implementation for the STICS crop-soil model. Environ.
Model. Softw. 26, 386–394.
White, H., 1981. Consequences and detection of misspecified nonlinear regression models. J. Am.
Stat. Assoc. (374), 419–433.