简体   繁体   English

MATLAB-如何基于x和y计算二维最小二乘回归。 (回归面)

[英]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. 我有一组具有独立变量x和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. 您可以将我最喜欢的polyfitn用于线性或多项式模型。 If you would like a different model, please edit your question or add a comment. 如果您想要其他型号,请编辑您的问题或添加评论。 HTH! 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: 抱歉,我对此太感兴趣了,这是一个使用最小二乘与股票 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. 如果要执行线性回归,最好的工具是regress函数。 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 . 请注意,如果您要拟合形式为y(x1,x2) = b1.f(x1) + b2.g(x2) + b3的模型,则只要您知道函数fg

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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