필터 지우기
필터 지우기

Nonlinear fit with constraints in R2012b

조회 수: 14 (최근 30일)
Niles Martinsen
Niles Martinsen 2012년 9월 22일
답변: Bhavin Khatri 2019년 10월 9일
Hi
I am running R2012b and doing nonlinear weighted regression as explained here: http://www.mathworks.se/help/stats/examples/weighted-nonlinear-regression.html. In other words, I am using NonLinearModel.fit.
However is there a way to constrain one of the fit parameters to be positive?
Best, Niles.

답변 (3개)

Star Strider
Star Strider 2012년 9월 22일
편집: Star Strider 2012년 9월 22일
The Statistics Toolbox functions (such as you referenced in the link) won't let you constrain the parameters but the Optimization Toolbox functions (such as lsqcurvefit) will.
You can do weighted nonlinear regression with lsqcurvefit, using an interim anonymous function. So if the model you have coded as a function and want to fit is called ‘nonlinmodel’, you would normally call it from lsqcurvefit this way:
x = lsqcurvefit(@nonlinmodel,x0,xdata,ydata,lb,ub);
To do weighted nonlinear regression with it, you need to incorporate your weight vector into an anonymous function, and then call the anonymous fiunction from lsqcurvefit:
Weights = 1./(N.*SE.^2);
nonlinmodelW = @(B,t) Weights .* nonlinearmodel(B,t);
x = lsqcurvefit(nonlinmodelW,x0,xdata,ydata,lb,ub);
For ‘Weights’, I used the typical standard errors usually provided, with N being the number of observations used to calculate each of them. This produces inverse-variance weights, the conventional scheme. Note that ‘Weights’ is a vector of the same dimensions as the vector ‘nonlinearmodel’ returns.
When you simulate your function with your estimated parameters to plot with your data, remember to use the anonymous function. It (here ‘nonlinmodelW’) is the function you fitted, not your original ‘nonlinmodel’.
This anonymous function approach will work with both lsqcurvefit and nlinfit. Before weights were added to nlinfit in 2012b, this was the standard way of doing weighted nonlinear regression with both. I've done weighted nonlinear regression using this approach many times with lsqcurvefit in order to take advantage of the parameter constraints.
  댓글 수: 4
Zhaorong Wang
Zhaorong Wang 2013년 11월 8일
Thanks Star Strider for providing a nice trick to do weighted fitting using the most powerful lsqcurvefit! However, one thing has to be corrected, if you weight the fitting function "nonlinmodel", you have to weight the ydata as well. Because you are minimizing
sum((nonlinmodelW(B,xdata) - ydata).^2),
readers better deal with this carefully, like writing some wrapper function lsqcurvefit_weighted() for lsqcurvefit()
Sarah Garre
Sarah Garre 2013년 12월 20일
Dear Zhaorong Wang, thanks for putting our attention to this important detail. How do you suggest to make a 'wrapper'?

댓글을 달려면 로그인하십시오.


Sarah Garre
Sarah Garre 2013년 12월 20일
if I understand it well, it should be enough to manage it like this:
Weights = 1./(N.*SE.^2);
nonlinmodelW = @(B,t) Weights .* nonlinearmodel(B,t);
x = lsqcurvefit(nonlinmodelW,x0,xdata,Weights .* ydata,lb,ub);
Am I right?
  댓글 수: 2
Richard Andersson
Richard Andersson 2014년 4월 15일
Thanks everyone for your insight on this problem. I would just ask that you clarify exactly what the inputs "B" and "t" are.
Star Strider
Star Strider 2014년 4월 15일
Sarah — That looks correct. I haven’t tested it to be sure.
Richeard — The argument B is the vector of parameters (usually termed β in the literature), and t is the independent variable vector, probably time in this example.
The same objective function format works for both lsqcurvefit and nlinfit, but the arguments must be in the (B,t) order.
In the last few versions, nlinfit has the option of a weighted regression. I haven’t needed to use it recently, so I refer you to the documentation.

댓글을 달려면 로그인하십시오.


Bhavin Khatri
Bhavin Khatri 2019년 10월 9일
I think by usual maximum likelihood arguments the weights should be
weights = 1./se.^2
not
weights 1./(N.*se.^2).
Intuitively, it doesn't make sense either, since according to the latter, the more observations you have for a particular data point the smaller the weight!
(Of course you can choose whatever weights you want, but assuming Gaussian errors etc..., if you want the maximum likelihood estimate the weights should be the inverse of the standard error.)

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by