[R] Error messages using nonlinear regression function (nls)

Martin Maechler maechler at stat.math.ethz.ch
Fri Oct 20 17:02:59 CEST 2017


>>>>> PIKAL Petr <petr.pikal at precheza.cz>
>>>>>     on Fri, 20 Oct 2017 13:35:01 +0000 writes:

    > Thank you Martin.
    > If I understand correctly, OP could do

    > wheat.list <- nlsList(Prop ~ SSfpl(end, A, B, xmid, scal), data=wlg)

(which is really a different model with one parameter more, and
 given the relatively small sample size per group, it may not be
 the best idea)

    > or add some small value to all zeroes

    > wlg$prop <- wlg$Prop+1e-7
    > wheat.list <- nlsList(prop ~ SSlogis(end,Asym, xmid, scal), data=wlg)

    > which gives fairly reasonable results.
    > plot(augPred(wheat.list))

    > Am I correct?

Yes, indeed, Petr,  I forgot to mention that obvious workaround.

(Interestingly, he could also _subtract_ a small positive value
 which is less intuitive, but work too given the code I showed.)

Martin



    > Cheers
    > Petr

    >> -----Original Message-----
    >> From: Martin Maechler [mailto:maechler at stat.math.ethz.ch]
    >> Sent: Friday, October 20, 2017 1:04 PM
    >> To: PIKAL Petr <petr.pikal at precheza.cz>
    >> Cc: Wall, Wade A ERDC-RDE-CERL-IL CIV <Wade.A.Wall at erdc.dren.mil>; r-
    >> help at r-project.org
    >> Subject: Re: [R] Error messages using nonlinear regression function (nls)
    >> 
    >> >>>>> PIKAL Petr <petr.pikal at precheza.cz>
    >> >>>>>     on Fri, 20 Oct 2017 06:33:36 +0000 writes:
    >> 
    >> > Hi
    >> > Keep your messages in the list, you increase your chance to get some
    >> answer.
    >> 
    >> > I changed your data to groupedData object (see below), but I did not find
    >> any problem in it.
    >> 
    >> > plot(wlg)
    >> > gives reasonable picture and I am not such expert to see any problem with
    >> data. Seems to me, that something has to be wrong with nlsList function.
    >> 
    >> >> wheat.list <- nlsList(Prop ~ SSlogis(end,Asym, xmid, scal), data=wlg)
    >> > Warning message:
    >> > 6 times caught the same error in lm.fit(x, y, offset = offset, singular.ok =
    >> singular.ok, ...): NA/NaN/Inf in 'x'
    >> 
    >> > produces empty list. So maybe others could find some problem.
    >> 
    >> > Cheers
    >> > Petr
    >> 
    >> Thank you, Petr,  for the reproducible example.
    >> 
    >> This indeed can be traced back to a bug in SSlogis() that has been there since
    >> Doug Bates added the 'nls' package on Martin's day 1999 (==> before R version
    >> 1.0.0 !) to R (svn rev 6455).
    >> 
    >> It's this part which does contain a thinko (by whomever, only Doug may be able
    >> to remember/recall, but I guess it would have been José Pinheiro, then the grad
    >> student doing the nitty gritty), I have added 2 '----------' below to make the 2
    >> important lines stand out :
    >> 
    >> z <- xy[["y"]]
    >> ---------------------------------------------------------------------
    >> if (min(z) <= 0) { z <- z - 1.05 * min(z) } # avoid zeroes
    >> z <- z/(1.05 * max(z))             # scale to within unit height
    >> ---------------------------------------------------------------------
    >> xy[["z"]] <- log(z/(1 - z))                # logit transformation
    >> aux <- coef(lm(x ~ z, xy))
    >> 
    >> the first of the 2 lines,  commented   "avoid zeroes"
    >> is obviously *not* avoiding zeroes in the case min(z) == 0 , and even though the
    >> 2 lines  should rescale an interval  [0, M] to [eps, 1 - eps] they don't in this case.
    >> 
    >> Consequently, the next line  log(z/(1 - z))  transforms the 0s into -Inf  and these
    >> lead to the warning (or error in nls())
    >> 
    >> " NA/NaN/Inf in 'x' "
    >> 
    >> One could fix this up by  replacing  min(z)  by  min(z, -1e-7) which may be best
    >> for pure back compatibility, but I really don't like it either :
    >> 
    >> The famous  logit - transform   log( z / (1-z))
    >> is really anti-symmetric around z = 1/2 , in particular should treat 0 and 1
    >> "symmetrically" and I find it ugly that the previous fixup (our two ominous
    >> lines) is not at all symmetric wrt 1/2, notably the 2nd transformation is made
    >> unconditionally but the first one not.
    >> 
    >> Fortunately, the same source file, <R>/src/library/stats/R/zzzModels.R
    >> also defines  the SSfpl()  == 4-parameter logistic model
    >> 
    >> and there, the 'init' function needs to do the same scaling to (0, 1)  and does it
    >> much nicer, indeed (anti)symmetrically.
    >> 
    >> I'm looking into using that in SSlogis() as well, fixing this bug.
    >> 
    >> Martin Maechler
    >> ETH Zurich and R Core Team

    > ________________________________
    > 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