Matlab regressions
From LiteratePrograms
This program is under development.
Please help to debug it. When debugging
is complete, remove the {{develop}} tag.
Many people use MATLAB to perform regressions. Let's give some examples.
Contents |
Linear regression
Linear least squares regressions can be calculated using the \ operator:
B = x \ y
It is also possible to use the regress function from the MATLAB Statistics Toolbox:
[B, Bint, R, Rint, Stats] = regress(y, x)
Apparently nonlinear regressions
Simple example
Setting the question
Consider a question asked on the MATLAB newsgroup (Help to curve fitting):
I have some data (x,y)and one equation like y=(a*x)/(1+ax) and I need to plot the data and the curve [...]
When you have such a parametrized equation:
you can sometimes rewrite it in a:
so if you have some (x,y) distribution, you can plot them and use (with a define as in (2)). |
First generate a test set:
- f is defined as an anonymous function
- the real value of a is arbitrary chosen
- a noise coefficient s is chosen
- the domain of x is [0,10], the chosen resolution for x is 0.05
- y is the theoretical value of y
- ny the noisy (observed) values for y
<<generate test set>>= f = @(a,x)(a*x./(1+a*x)) a = sqrt(2) s = .05; x = (0:.1:10)'; y = f(a,x); ny = y + randn(size(y))*s; figure('Color',[0.9412 0.9412 0.9412 ]); h1 = subplot(1,2,1); plot(x,y,'-b',x,ny,'.k','linewidth',2);
Simple answer
Now we can get the implied values for a:
<<a implied values>>= g = @(x,y)(y./(x-x.*y)) ai = g(x,ny); h2 = subplot(1,2,2); boxplot(ai);
And a trivial estimator for a is the empirical expectation (the mean) of the implied a:
<<trivial estimation>>= ah = mean(ai(~isinf(ai))); title(sprintf('$\\hat a$ = %5.5f ; real a = %5.5f', ah, a), 'interpreter', 'latex'); axes(h1); hold on plot(x, f(ah, x), ':r', 'linewidth', 2); hold off legend({'theoretical', 'observed', 'estimation'});
Let's produce the whole code:
<<test_trivial_estimation.m>>= generate test set a implied values trivial estimation
(see the upper figure for the result).
But it's a not so simple answer, because from a statistical point of view, you have to master some of the properties of the estimator:
- is it efficient?
- is it biaised?
- etc
External links
- L-1 Linear Regression ("Least Absolute Error" regression, in MATLAB)
- Linear Regression in MATLAB (Least Squares)
- Logistic Regression in MATLAB
- Weighted Regression in MATLAB (Weighted Least Squares)
Download code |