# [R] Nonlinear fitting - reparametrization help

Gabor Grothendieck ggrothendieck at gmail.com
Thu Sep 28 06:15:46 CEST 2006

On 9/28/06, Suresh Krishna <ssk2031 at columbia.edu> wrote:
>
> Hi,
>
> I am trying to fit a function of the form:
>
> y = A0 + A1 * exp( -0.5* ( (X - Mu1) / Sigma1 )^2 ) - A2 * exp ( -0.5*
> ( (X-Mu2)/Sigma2 )^2 )
>
> i.e. a mean term (A0) + a difference between two gaussians.
>
> The constraints are A1,A2 >0, Sigma1,Sigma2>0, and usually Sigma2>Sigma1.
> The plot looks like a "Mexican Hat".
>
> I had trouble (poor fits) fitting this function to toy data in Matlab and
> now I am playing with R's nls and optim functions. I keep running into
> singular gradient errors with nls, even with very different starting
> values, and have not yet figured out how to interpret the trace results
> usefully.
>
> Can someone help ? Is there a correct parameterization for this problem ?
> I have appended some R code with sample data to fit.
>
> Thank you !!!
>
> Suresh; please cc ssk2031 at columbia.edu if possible.
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> x=seq(-10,10,length=1000)
> a0=0;
> ae=1;
> me=0;
> se=1;
> ai=0.5;
> mi=0;
> si=3;
> dogy <- function(x,a0,ae,me,se,ai,mi,si){
> y=a0+ae*exp(-0.5*(((x-me)/se)^2))-ai*exp(-0.5*(((x-mi)/si)^2))
> y}
> y=dogy(x,a0,ae,me,se,ai,mi,si)
> erval=rnorm(length(y),sd=0.02)
> y=y+erval
> #plot(x,y+erval)
>
> #fit=nls(y~ae*exp((x/se)^2)-ai*exp((x/si)^2),start=c(ae=.8,se=1.1,ai=.2,si=1),trace=TRUE)
> #here I tried to unsuccesfully reduce the model by eliminating A0, Mu1,
> and Mu2
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help