问题描述
我有这个 matlab 代码,用于带有一个独立变量的回归,但是如果我有两个独立变量(x1 和 x2)怎么办?我该如何修改这个多项式回归的代码?
I have this matlab code for regression with one indepenpent variable, but what if I have two independent variables(x1 and x2)? How should I modify this code of polynomial regression?
x = linspace(0,10,200)'; % independent variable
y = x + 1.5*sin(x) + randn(size(x,1),1); % dependent variable
A = [x.^0, x]; % construct a matrix of permutations
w = (A'*A)\(A'*y); % solve the normal equation
y2 = A*w; % restore the dependent variable
r = y-y1; % find the vector of regression residual
plot(x, [y y2]);
推荐答案
Matlab 具有多项式回归函数 polyfit
的工具.你试过了吗?
Matlab has facilities for polynomial regression function polyfit
. Have you tried that?
http://www.mathworks.com/help/techdoc/data_analysis/f1-8450.html
http://www.mathworks.com/help/toolbox/stats/bq_676m-2.html#bq_676m-3
但是,如果您想锻炼自己的公式,您可能应该查看教科书或一些有关回归的在线资源,例如
But if you want to workout your own formulation,you should probably look at textbook or some online resources on regression e.g.
http://www.edwardtufte.com/tufte/dapp/DAPP3a.pdf
这篇关于matlab中的回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!