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
No products are associated with this question.
There are several multi-dimensional polynomial fitters on the FEX, e.g.,
http://www.mathworks.com/matlabcentral/fileexchange/34765-polyfitn
I don't see the problem. Both FEX files handle two-dimensional polynomials of order N. There's no reason why can't choose N=1.
I am not fully sure if I get the description there right but there problem that I have is that there are two linear components which I need to cover. in addition I am not sure how excatly I need to do that. can I just download one of them in instead this code:
x= dataT(:,2);
%is the implied volatility
y=dataT(:,10);p = polyfit(x,y,2)
using something like that:
x= dataT(:,2); %is the implied volatility y=dataT(:,10); z=dataT(:,15); p = polyfitn(x,y,z,3)
is that way the the z not quadratic? As said, the regression must stay linear, I do not want any quadratic components
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.
Locks,
1) Regress takes the response first and then the predictors. Here is the documentation for regress:
http://www.mathworks.com/help/stats/regress.html
>> p=regress(Y,X)
2) Statistics Toolbox is required for Financial Toolbox, therefore you will have it already installed. You can check the output of 'VER' to verify it.
3) Use backslash '\' not forwardslash '/', they are different in MATLAB in what they do. http://www.mathworks.com/help/matlab/ref/mldivide.html
>> X\Y
4) You can also use LinearModel.fit function (recommended over regress) It does the same thing but has the added advantage of obtaining the fit statistics automatically: http://www.mathworks.com/help/stats/linearmodel.fit.html
>> X= [x1 x2]; >> LinearModel.fit(x,y) %Ones not required, automatically added.
thanks, this is working, I get the following result:
Number of observations: 1804, Error degrees of freedom: 1801 Root Mean Squared Error: 1.69 R-squared: 0.97, Adjusted R-Squared 0.97 F-statistic vs. constant model: 2.9e+04, p-value = 0
how can I show the r^2 and r^2 adjusted numbers with 4 fractional digits?
IS there a way to see the coefficients b0, b1 and b2 I am also looking for?
1 Comment
Direct link to this comment:
http://www.mathworks.com/matlabcentral/answers/74020#comment_146214
What is z?
Are you doing a mixed effects fitting?