general linear regression
determine the parameters p_j (j=1,2,...,m) such that the function f(x) = sum_(j=1,...,m) p_j*f_j(x) is the best fit to the given values y_i by f(x_i) for i=1,...,n, i.e. minimize sum_(i=1,...,n)(y_i-sum_(j=1,...,m) p_j*f_j(x_i))^2 with respect to p_j
parameters:
return values:
To estimate the variance of the difference between future y values and fitted y values use the sum of e_var and fit_var
Caution: do NOT request fit_var for large data sets, as a n by n matrix is generated
See also: ols,gls,regress,leasqr,nonlin_curvefit,polyfit,wpolyfit,expfit.
The following code
n = 50;
x = sort (rand (n,1) * 5 - 1);
y = 1 + 0.05 * x + 0.1 * randn (size (x));
F = [ones(n, 1), x(:)];
[p, e_var, r, p_var, fit_var] = LinearRegression (F, y);
yFit = F * p;
figure ()
plot(x, y, '+b', x, yFit, '-g',...
x, yFit + 1.96 * sqrt (e_var), '--r',...
x, yFit + 1.96 * sqrt (fit_var), '--k',...
x, yFit - 1.96 * sqrt (e_var), '--r',...
x, yFit - 1.96 * sqrt (fit_var), '--k')
title ('straight line fit by linear regression')
legend ('data','fit','+/-95% y values','+/- 95% fitted values','location','northwest')
grid on
Produces the following figure
| Figure 1 |
|---|
![]() |
The following code
n = 100;
x = sort (rand (n, 1) * 5 - 1);
y = 1 + 0.5 * sin (x) + 0.1 * randn (size (x));
F = [ones(n, 1), sin(x(:))];
[p, e_var, r, p_var, fit_var] = LinearRegression (F, y);
yFit = F * p;
figure ()
plot (x, y, '+b', x, yFit, '-g', x, yFit + 1.96 * sqrt (fit_var),
'--r', x, yFit - 1.96 * sqrt (fit_var), '--r')
title ('y = p1 + p2 * sin (x) by linear regression')
legend ('data', 'fit', '+/-95% fitted values')
grid on
Produces the following figure
| Figure 1 |
|---|
![]() |
The following code
n = 50;
x = sort (rand (n, 1) * 5 - 1);
y = 1 + 0.5 * x;
dy = 1 ./ y; # constant relative error is assumed
y = y + 0.1 * randn (size (x)) .* y; # straight line with relative size noise
F = [ones(n, 1), x(:)];
[p, e_var, r, p_var, fit_var] = LinearRegression (F, y, dy); # weighted regression
fitted_parameters_and_StdErr = [p, sqrt(p_var)]
yFit = F * p;
figure ()
plot(x, y, '+b', x, yFit, '-g',...
x, yFit + 1.96 * sqrt (e_var), '--r',...
x, yFit + 1.96 * sqrt (fit_var), '--k',...
x, yFit - 1.96 * sqrt (e_var), '--r',...
x, yFit - 1.96 * sqrt (fit_var), '--k')
title ('straight line by weighted linear regression')
legend ('data','fit','+/-95% y values','+/- 95% fitted values','location','northwest')
grid on
Produces the following output
fitted_parameters_and_StdErr = 1.007108 0.013903 0.489976 0.013351
and the following figure
| Figure 1 |
|---|
![]() |
Package: optim