[R] Urgent: conditional formula for nls

Dennis Murphy djmuser at gmail.com
Sun May 1 20:57:46 CEST 2011


Hi:

Instead of using ifelse(), you can multiply the logical statement by
the rest of the expression. When the logical statement is false, its
numerical value is zero.

HTH,
Dennis

On Sun, May 1, 2011 at 9:52 AM, Andrew Coop <andrew.e.coop at gmail.com> wrote:
> I have data vectors x and y both with 179 observations.  I'm trying to
> fit a nonlinear model with five parameters using nls.  The formula is
> only defined within a range of x-values, it should be zero otherwise,
> thus my attempted use of ifelse:
>
>> df<-data.frame(x,y)
>> nlsfit<-nls(y~ifelse(x>m&x<m+abs(s),abs(a)*d*abs(s)*exp(-(1/2)*(g+d*log((x-m)/(-x+m+abs(s)))^2))/(sqrt(2*pi)*(x-m)*(-x+m+abs(s))),0),data=df,start=list(a=0.37,g=0,d=1,m=0.42,s=0.24))
> Error in nlsModel(formula, mf, start, wts) :
>  singular gradient matrix at initial parameter estimates
> In addition: Warning messages:
> 1: In log((x - m)/(-x + m + abs(s))) : NaNs produced
> 2: In log((x - m)/(-x + m + abs(s))) : NaNs produced
> 3: In log((x - m)/(-x + m + abs(s))) : NaNs produced
> 4: In log((x - m)/(-x + m + abs(s))) : NaNs produced
> 5: In log((x - m)/(-x + m + abs(s))) : NaNs produced
> 6: In log((x - m)/(-x + m + abs(s))) : NaNs produced
> 7: In log((x - m)/(-x + m + abs(s))) : NaNs produced
>
> What am I doing wrong?
>
> Thanks,
> Andy
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list