Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

using a function that is similar to polyfit but with two linear terms

Asked by Locks on 29 Apr 2013

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

1 Comment

Shashank Prasanna on 29 Apr 2013

What is z?

Are you doing a mixed effects fitting?

Locks

Products

No products are associated with this question.

2 Answers

Answer by Shashank Prasanna on 1 May 2013
Edited by Shashank Prasanna on 1 May 2013
Accepted answer

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 Comments

Locks on 3 May 2013

sorry, I made a mistake due to the fact that this comand is only working for columnvectors and the polyfoit worked also for rowvectors.

the only thing that is missing at the moment is that I would like to save the estimates, standarderror, tStat and pValue in seperate vectors or structures.

for the Rsquared it has worked, but for the other elements I haven't found anything in the documentation, do you have an idea?

the code I am using now is:

   Y= changePriceWithoutNaN'; %change option price
   x1=changeFWithoutNaN';     %change Futures times delta
   x2=changeSADadjustmentWithoutNaN'; %change adjusted term
X= [x1 x2];
LinearModel.fit(X,Y)
mdl = LinearModel.fit(X,Y);   
mdl.Coefficients.Estimate;
mdl;
RSQ=mdl.Rsquared
Shashank Prasanna on 3 May 2013

It is indeed in the documentation, please go through it closely. This will help you find things later yourself :

http://www.mathworks.com/help/stats/linearmodelclass.html

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.

Locks on 3 May 2013

I had some strange structure because I only used mdl_SAD.Coefficients.Estimate;

and I was not able to get the values saved in there, but no it's clear, thanks

Shashank Prasanna
Answer by Matt J on 29 Apr 2013

4 Comments

Matt J on 1 May 2013

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.

Locks on 1 May 2013

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

Matt J on 2 May 2013

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);
Matt J

Contact us