简体   繁体   中英

MATLAB - How to calculate 2D least squares regression based on both x and y. (regression surface)

I have a set of data with independent variable x and y. Now I'm trying to build a two dimensional regression model that has a regression surface cutting through my data points. However, I couldn't find a way to achieve this. Can anyone give me some assistance?

You could use my favorite, polyfitn for linear or polynomial models. If you would like a different model, please edit your question or add a comment. HTH!

EDIT

Also, take a look here under Multiple Regression, likely can help you as well.

EDIT AGAIN

Sorry, I'm having too much fun with this, here's an example of multivariate regression using least squares with stock Matlab:

t = (1:10)';
x = t;
y = exp(-t);
A = [ y x ];
z = 10*y + 0.5*x;
A\z
ans =

   10.0000
    0.5000

If you are performing linear regression, the best tool is the regress function. Note that, if you are fitting a model of the form y(x1,x2) = b1.f(x1) + b2.g(x2) + b3 this is still a linear regression, as long as you know the functions f and g .

Nsamp = 100;  %number of samples
X1 = randn(Nsamp,1);  %regressor 1 (could also be some computed f(x1) )
X2 = randn(Nsamp,1);  %regressor 2 (could also be some computed g(x2) )
Y  = X1 + X2 + randn(Nsamp,1);  %generate some data to be regressed

%now run the regression
[b,bint,r,rint,stats] = regress(Y,[X1 X2 ones(Nsamp,1)]);

% 'b' contains the coefficients, b1,b2,b3 of the fit; can be used to plot regression surface)
% 'r' contains residuals of the fit
% 'stats' contains the overall regression R^2, F stat, p-value and error variance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM