[R] About nls.

PIKAL Petr petr.pikal at precheza.cz
Mon Aug 3 15:04:42 CEST 2015


Hi

Please keep conversation on list somebody can have better idea. Other see in line.


> -----Original Message-----
> From: Jianling Fan [mailto:fanjianling at gmail.com]
> Sent: Friday, July 31, 2015 4:46 PM
> To: PIKAL Petr
> Subject: Re: [R] About nls.
>
> Hello, Petr,
>
> Thanks for your help.
> That works but it change my model. And I think that's not the main
> problem.
> from my data, (den1/R1+den2+den3+den4+den5/R5) always <1, which makes
> (1/(den1/R1+den2+den3+den4+den5/R5)-1)>0.

It is not true unless I have different data from yours.

> with(dat,(den1/0.9+den2+den3+den4+den5/23))
 [1]  0.4655556  0.7472222  0.9755556  1.0733333  1.1100000  0.3800000
 [7]  0.4800000  0.8500000  0.8800000  1.0000000  0.4800000  0.8900000
[13]  0.9800000  0.9900000  1.0000000  0.2000000  0.3900000  0.6900000
[19]  0.9900000  1.0000000  6.0652174  9.9065217 13.3434783 16.5782609
[25] 18.8021739 19.8130435

> with(dat,(1/(den1/.9+den2+den3+den4+den5/23)-1)>0)
 [1]  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
[13]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE
>

> str(dat)
'data.frame':   26 obs. of  7 variables:
 $ Depth: int  20 40 60 80 100 15 30 45 60 120 ...
 $ lnd  : num  3 3.69 4.09 4.38 4.61 ...
 $ den1 : num  0.419 0.672 0.878 0.966 0.999 ...
 $ den2 : num  0 0 0 0 0 0.38 0.48 0.85 0.88 1 ...
 $ den3 : num  0 0 0 0 0 0 0 0 0 0 ...
 $ den4 : num  0 0 0 0 0 0 0 0 0 0 ...
 $ den5 : num  0 0 0 0 0 0 0 0 0 0 ...
>

> dput(dat)
structure(list(Depth = c(20L, 40L, 60L, 80L, 100L, 15L, 30L,
45L, 60L, 120L, 15L, 30L, 45L, 60L, 120L, 15L, 30L, 45L, 60L,
120L, 10L, 30L, 50L, 70L, 90L, 110L), lnd = c(2.995732, 3.688879,
4.094345, 4.382027, 4.60517, 2.70805, 3.401197, 3.806662, 4.094345,
4.787492, 2.70805, 3.401197, 3.806662, 4.094345, 4.787492, 2.70805,
3.401197, 3.806662, 4.094345, 4.787492, 2.302585, 3.401197, 3.912023,
4.248495, 4.49981, 4.70048), den1 = c(0.419, 0.6725, 0.878, 0.966,
0.999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0), den2 = c(0, 0, 0, 0, 0, 0.38, 0.48, 0.85, 0.88, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), den3 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.48, 0.89, 0.98, 0.99, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), den4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0.2, 0.39, 0.69, 0.99, 1, 0, 0, 0, 0, 0, 0),
    den5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 139.5, 227.85, 306.9, 381.3, 432.45, 455.7)), .Names = c("Depth",
"lnd", "den1", "den2", "den3", "den4", "den5"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26"))
>

You can check if this is the same as you have. That is why it is preferable to use dput for sending data.

Cheers
Petr


> So, the nls should work in this case. But I don't know why it does not.
>
> Thanks!
>
>  Regards,
>
> Julian
>
>
>
> On 31 July 2015 at 00:54, PIKAL Petr <petr.pikal at precheza.cz> wrote:
> > Hi
> >
> > I am not an expert but the problem seems to me that
> >
> > (den1/R1+den2+den3+den4+den5/R5)-1
> >
> > gives you sometimes value 0 and sometimes negative. In these cases
> the value of log(1/result) is NA or Inf and nls can not handle this.
> >
> > I do not search vhere is nls2 from so I used nls and removed -1 form
> your formula, which resulted to some final values.
> >
> >> fit1<-nls(lnd~log(1/(den1/R1+den2+den3+den4+den5/R5))/c+log(d50),
> > + start=c(R1=0.9, R5=23, c=-1.1, d50=10), data=test)
> >>
> >> coef(fit)
> >          A          B
> >  6.9720965 -0.0272203
> >> coef(fit1)
> >          R1          R5           c         d50
> >   0.9622249 416.1272498  -0.7178156  73.6017161
> >>
> > Cheers
> > Petr
> >
> >> -----Original Message-----
> >> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of
> >> Jianling Fan
> >> Sent: Thursday, July 30, 2015 9:51 PM
> >> To: r-help at r-project.org
> >> Subject: [R] About nls.
> >>
> >> Hello,
> >>
> >> I am trying to do a nls regression with R.  but I always get a error
> >> as "Error in numericDeriv(form[[3L]], names(ind), env) :  Missing
> >> value or an infinity produced when evaluating the model".  I googled
> >> it and found someone said it is because of the improper start value.
> >> I tried many times but can not solve it. Does anyone can help me?
> >>
> >> thanks a lot !
> >>
> >> my code is:
> >>
> >> fit1<-nls2(lnd~log(1/(den1/R1+den2+den3+den4+den5/R5)-1)/c+log(d50),
> >>               start=c(R1=0.9, R5=23, c=-1.1, d50=10), data=SWrt)
> >>
> >> data (SWrt) is:
> >>
> >>   Depth      lnd   den1 den2 den3 den4   den5
> >> 1     20 2.995732 0.4190 0.00 0.00 0.00   0.00
> >> 2     40 3.688879 0.6725 0.00 0.00 0.00   0.00
> >> 3     60 4.094345 0.8780 0.00 0.00 0.00   0.00
> >> 4     80 4.382027 0.9660 0.00 0.00 0.00   0.00
> >> 5    100 4.605170 0.9990 0.00 0.00 0.00   0.00
> >> 6     15 2.708050 0.0000 0.38 0.00 0.00   0.00
> >> 7     30 3.401197 0.0000 0.48 0.00 0.00   0.00
> >> 8     45 3.806662 0.0000 0.85 0.00 0.00   0.00
> >> 9     60 4.094345 0.0000 0.88 0.00 0.00   0.00
> >> 10   120 4.787492 0.0000 1.00 0.00 0.00   0.00
> >> 11    15 2.708050 0.0000 0.00 0.48 0.00   0.00
> >> 12    30 3.401197 0.0000 0.00 0.89 0.00   0.00
> >> 13    45 3.806662 0.0000 0.00 0.98 0.00   0.00
> >> 14    60 4.094345 0.0000 0.00 0.99 0.00   0.00
> >> 15   120 4.787492 0.0000 0.00 1.00 0.00   0.00
> >> 16    15 2.708050 0.0000 0.00 0.00 0.20   0.00
> >> 17    30 3.401197 0.0000 0.00 0.00 0.39   0.00
> >> 18    45 3.806662 0.0000 0.00 0.00 0.69   0.00
> >> 19    60 4.094345 0.0000 0.00 0.00 0.99   0.00
> >> 20   120 4.787492 0.0000 0.00 0.00 1.00   0.00
> >> 21    10 2.302585 0.0000 0.00 0.00 0.00 139.50
> >> 22    30 3.401197 0.0000 0.00 0.00 0.00 227.85
> >> 23    50 3.912023 0.0000 0.00 0.00 0.00 306.90
> >> 24    70 4.248495 0.0000 0.00 0.00 0.00 381.30
> >> 25    90 4.499810 0.0000 0.00 0.00 0.00 432.45
> >> 26   110 4.700480 0.0000 0.00 0.00 0.00 455.70
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> >

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.


More information about the R-help mailing list