using a function that is similar to polyfit but with two linear terms
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I am looking for a matlab function that is working similar to polyfit, but where I can use two different input function but instead of having just one linear term, I need two. At the moment the regression looks as follows:
y=b0+b1*x+error
and the code to compute R^2 is the following:
x= changePriceWithoutNaN;
y=changeFWithoutNaN;
p = polyfit(x,y,1);
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
yfit = polyval(p,x);
yfit = p(1) * x + p(2);
yresid = y - yfit;
SSresid = sum(yresid.^2);
SStotal = (length(y)-1) * var(y);
rsq_full = 1 - SSresid/SStotal
Instead of having just one linear term, the term I am looking for is the following:
y=b0+b1*x+b2*z+error
Is there anybody how knows a function that is solving a least squared optimazation in the way to coe above does? Importat is that I do not look for a quadratic solution and therefore from what I can see polyfit(x,y,2) is not an option
采纳的回答
Shashank Prasanna
2013-5-1
编辑:Shashank Prasanna
2013-5-1
Locks, it seems like you are interested in multiple linear regression. If you have the stats toolbox you can use the REGRESS function to do that. If you don't then you can use a simple '\' as follows:
x = dataT(:,2);
%is the implied volatility
y = dataT(:,10);
z = dataT(:,15);
p = [x z ones(length(dataT))]\y
p will have the 3 coeff you desire. We are essentially solving a linear system in a least square sense.
11 个评论
Shashank Prasanna
2013-5-3
It is indeed in the documentation, please go through it closely. This will help you find things later yourself :
and the answer is already in your code, I encourage you to take a closer look.
Hint:
tstat = mdl.Coefficients.tStat
Once again, please go through the documentation, fundamentally, all properties can be accessed with the '.' dot notation from your model.
更多回答(1 个)
Matt J
2013-4-29
There are several multi-dimensional polynomial fitters on the FEX, e.g.,
4 个评论
Matt J
2013-5-2
As said, the regression must stay linear, I do not want any quadratic components
And as I keep telling you, if you want to have only linear terms, then tell that to polyfitn:
p = polyfitn([x,y],z,1);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!