Page 79 - MATLAB Recipes for Earth Sciences
P. 79
4.3 Classical Linear Regression Analysis and Prediction 71
Instead of using the equation for the regression line, we can also use the
function polyval to calculate the y-values.
plot(meters,age,'o'), hold
plot(meters,polyval(p,meters),'r')
Both, polyfit and polyval are incorporated in the MATLAB GUI func-
tion polytool.
polytool(meters,age)
The coeffi cients p(x) and the equation obtained by linear regression can
now be used to predict y-values for any given x-values. However, we can
only do this for the depth interval for which the linear model was fi tted,
i.e., between 0 and 20 meters. As an example, the age of the sediment at the
depth of 17 meters depth is given by
polyval(p,17)
ans =
96.8667
This result suggests that the sediment at 17 meters depth has an age of ca.
97 kyrs. The goodness-of-fit of the linear model can be determined by calcu-
lating error bounds. These are obtained by cloosing an additional output pa-
rameter for polyfit and by using this as an input parameter for polyval.
[p,s] = polyfit(meters,age,1);
[p_age,delta] = polyval(p,meters,s);
This code uses an interval of ±2s, which corresponds to a 95% confi dence
interval. polyfit returns the polynomial coeffi cients p, and a structure s
that polyval uses to calculate the error bounds. Structures are MATLAB
arrays with named data containers called fi elds. The fields of a structure can
contain any kind of data, such as text strings representing names. Another
might contain a scalar or a matrix. In our example, the structure s contains
fields for the statistics of the residuals that we use to compute the error
bounds. delta is an estimate of the standard deviation of the error in pre-
dicting a future observation at x by p(x). We plot the results.
plot(meters,age,'+',meters,p_age,'g-',...
meters,p_age + 2 * delta,'r--',meters,p_age - 2 * delta,'r--')
xlabel('meters'), ylabel('age')
Since the plot statement does not fi t on one line, we use an ellipsis (three
periods), ..., followed by return or enter to indicate that the statement