how to input constant value in polyfitn functions
2 views (last 30 days)
Show older comments
Oleksandr Oleksandras
on 20 Mar 2015
Commented: John D'Errico
on 9 Apr 2015
Hi, I have data for "x,y,z" independent variables which I have to fit to the reference "N" data.
p = polyfitn([x(:),y(:),z(:)], N(:), 'constant x(:), y(:), z(:)')
modelterms = 0 0 0; 1 0 0; 0 1 0; 0 0 1; Coefficients: [0.4112 1.6207 -1.8792 0.4623] VarNames: {'x(:)' 'y(:)' 'z(:)'}
Now I need to change the constant[000] value let's say from 0.4112 to 0.9812 and to find the rest of the coefficients? How to do this with polyfitn?
Thanks
Oles
0 Comments
Accepted Answer
John D'Errico
on 20 Mar 2015
So instead of a constant term to be estimated, you wish to fix it at some new given value, and then estimate the coefficients? While there is no way in polyfitn to explicitly fix a coefficient, you can do so by a simple subterfuge.
Just subtract off the given constant term from the dependent variable, and drop the constant term from your model. So this will suffice.
p = polyfitn([x(:),y(:),z(:)], N(:) - 0.9812 , 'x, y, z')
Of course, the resulting model will have no constant term in it. You could slip it back in using some afterwards sleight of hand though. So after the above fit, this should create a model with the desired (forced) constant term.
p.ModelTerms = [0 0 0;p.ModelTerms];
p.Coefficients = [0.9812,p.Coefficients];
You could now evaluate the model with that fixed constant term in it.
If you worry about things like R^2, then be careful, as models with no constant term, or those with a forced term as we have done here, tend to create screwy numbers for R^2. As I recall, this is why I included an adjusted R^2, which may be more meaningful in those cases.
8 Comments
John D'Errico
on 9 Apr 2015
I have no idea what you are doing wrong, since you don't actually show the complete code, in terms of where that ModelTermsNew variable came from. HOWEVER, this works with no problems.
modelterms = dec2base(0:124,5) - '0';
modelterms(1,:) = [];
modelterms(sum(modelterms,2) > 4,:) = [];
mdl = polyfitn(rand(100,3),rand(100,1),modelterms);
Now, as I said, I can also use that set of terms in a second call.
MT = mdl.ModelTerms;
mdl2 = polyfitn(rand(100,3),rand(100,1),MT);
More Answers (0)
See Also
Categories
Find more on Multivariate Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!