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

PIKAL Petr petr.pikal at precheza.cz
Fri Oct 20 08:33:36 CEST 2017


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

> dput(wlg)
structure(list(temp = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("40", "10", "16", "22", "28",
"34"), class = c("ordered", "factor")), species = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("mungbean",
"rice", "wheat"), class = "factor"), start = c(0, 1, 2, 3, 4,
5, 6, 7, 9, 10, 11, 12, 13, 14, 16, 17, 0, 2, 3, 4, 5, 6, 7,
9, 10, 11, 12, 13, 14, 16, 17, 0, 2, 3, 4, 5, 6, 7, 9, 10, 11,
12, 13, 14, 16, 17, 0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13,
14, 16, 17, 0, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 16, 17,
0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 16, 17), end = c(1,
2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 16, 17, 18, 2, 3, 4,
5, 6, 7, 9, 10, 11, 12, 13, 14, 16, 17, 18, 2, 3, 4, 5, 6, 7,
9, 10, 11, 12, 13, 14, 16, 17, 18, 1, 2, 3, 4, 5, 6, 7, 9, 10,
11, 12, 13, 14, 16, 17, 18, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12,
13, 14, 16, 17, 18, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14,
16, 17, 18), germinated = c(0L, 0L, 0L, 0L, 0L, 4L, 6L, 8L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 11L, 7L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 12L, 3L, 0L, 3L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 8L, 0L, 8L, 3L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 5L, 11L, 1L, 1L, 0L, 0L, 0L,
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
2L, 0L, 0L, 0L, 0L, 0L, 0L), TotSeeds = c(20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20), TotGerminated = c(0, 0, 0, 0,
0, 4, 10, 18, 18, 18, 18, 18, 18, 18, 18, 18, 0, 0, 11, 18, 18,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 0, 1, 13, 16, 16, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 0, 0, 0, 8, 8, 16, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 0, 5, 16, 17, 18, 18, 18, 18,
18, 19, 19, 19, 19, 19, 19, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 3,
3, 3, 3, 3, 3), Prop = c(0, 0, 0, 0, 0, 0.2, 0.5, 0.9, 0.9, 0.9,
0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0, 0, 0.55, 0.9, 0.9, 0.95, 0.95,
0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0, 0.05, 0.65,
0.8, 0.8, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95,
0.95, 0, 0, 0, 0.4, 0.4, 0.8, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95,
0.95, 0.95, 0.95, 0.95, 0, 0.25, 0.8, 0.85, 0.9, 0.9, 0.9, 0.9,
0.9, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0, 0, 0, 0, 0, 0, 0,
0, 0.05, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15)), .Names = c("temp",
"species", "start", "end", "germinated", "TotSeeds", "TotGerminated",
"Prop"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 95L, 96L, 97L,
98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L,
109L, 110L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L,
136L, 137L, 138L, 139L, 140L, 141L, 159L, 160L, 161L, 162L, 163L,
164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L
), class = c("nfnGroupedData", "nfGroupedData", "groupedData",
"data.frame"), formula = Prop ~ end | temp, FUN = function (x)
max(x, na.rm = TRUE), order.groups = TRUE)
>

> sessionInfo()
R Under development (unstable) (2017-07-31 r73003)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

Matrix products: default

locale:
[1] LC_COLLATE=Czech_Czech Republic.1250  LC_CTYPE=Czech_Czech Republic.1250
[3] LC_MONETARY=Czech_Czech Republic.1250 LC_NUMERIC=C
[5] LC_TIME=Czech_Czech Republic.1250

attached base packages:
[1] stats     datasets  utils     grDevices graphics  methods   base

other attached packages:
[1] nlme_3.1-131    lattice_0.20-35 fun_0.1

loaded via a namespace (and not attached):
 [1] compiler_3.5.0   colorspace_1.3-2 scales_0.4.1     lazyeval_0.2.0
 [5] plyr_1.8.4       tools_3.5.0      gtable_0.2.0     tibble_1.3.3
 [9] Rcpp_0.12.12     ggplot2_2.2.1    grid_3.5.0       rlang_0.1.1
[13] munsell_0.4.3
>

> -----Original Message-----
> From: Wall, Wade A ERDC-RDE-CERL-IL CIV
> [mailto:Wade.A.Wall at erdc.dren.mil]
> Sent: Thursday, October 19, 2017 3:18 PM
> To: PIKAL Petr <petr.pikal at precheza.cz>
> Subject: RE: Error messages using nonlinear regression function (nls)
>
> "Prop" is the proportion of seeds that have germinated from the total. Here is
> the code I used to transform the germination data set. When graphed, the
> logistic curve provides a reasonable fit.
>
> data(germination)
> germination$TotSeeds = 20
> germination$TotGerminated = 0
> tmpCount = 0
>
> for(i in 2:nrow(germination)){
>   if((germination$temp[i]!=germination$temp[i-1]) | (germination$species[i] !=
> germination$species[i-1])) {tmpCount = 0}
>   if((germination$temp[i]==germination$temp[i-1]) & (germination$species[i]
> == germination$species[i-1]))
>   {tmpCount = tmpCount + germination$germinated[i];
> germination$TotGerminated[i]=tmpCount}
> }
> germination$Prop = germination$TotGerminated/germination$TotSeeds
> germination = germination[germination$end != Inf,]
>
> Wade
>
> -----Original Message-----
> From: PIKAL Petr [mailto:petr.pikal at precheza.cz]
> Sent: Thursday, October 19, 2017 1:21 AM
> To: Wall, Wade A ERDC-RDE-CERL-IL CIV <Wade.A.Wall at erdc.dren.mil>; r-
> help at r-project.org
> Subject: RE: Error messages using nonlinear regression function (nls)
>
> Hi
>
> Thanks for the code but where is Prop?
>
> It is not a variable in germination data set so we do not know how you did the
> computation.
>
> My wild guess is, that your Prop do not follow logistic curve and therefore no
> results from nlsList
>
> Cheers
> Petr
>
>
> > -----Original Message-----
> > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Wall,
> > Wade A ERDC-RDE-CERL-IL CIV
> > Sent: Wednesday, October 18, 2017 5:58 PM
> > To: r-help at r-project.org
> > Subject: [R] Error messages using nonlinear regression function (nls)
> >
> > Hi all,
> >
> > I am trying to use nonlinear regression (nls) to analyze some seed
> > germination data, but am having problems with error codes.
> >
> > The data that I have closely matches the germination dataset included
> > in the drc package.
> >
> > Here is the head of the data
> >
> >   temp species start end germinated TotSeeds TotGerminated Prop
> > 1   10   wheat     0   1          0       20             0  0.0
> > 2   10   wheat     1   2          0       20             0  0.0
> > 3   10   wheat     2   3          0       20             0  0.0
> > 4   10   wheat     3   4          0       20             0  0.0
> > 5   10   wheat     4   5          0       20             0  0.0
> > 6   10   wheat     5   6          4       20             4  0.2
> >
> > temp is the temperature under which the seeds were germinated, species
> > denotes the species (wheat, mungbean, or rice) Start and end denote
> > the beginning and end of a period of time and germinated denotes how
> > many seeds germinated during that period of time. Prop represents the
> > proportion of seeds that have germinated.
> >
> > I have attempted to mimic Fox and Weisberg's appendix to Nonlinear
> > Regression found here.
> > Blockedhttps://socserv.socsci.mcmaster.ca/jfox/Books/Companion/appendi
> > x/AppendixBlocked
> > -Nonlinear-Regression.pdf
> >
> > My first step is to look at a single species, wheat, and use nls on
> > the individual temperatures.
> >
> > I have tried to use both the nlsList function and to attempt to
> > estimate the parameters using lm(), but I receive error messages on
> > both. Here is the code.
> >
> > library(drc) ### for germination dataset
> > data(germination)
> >
> > wheat = germination[germination$species == "wheat",] ### subset by
> > wheat scatterplot(Prop ~ end|temp,data=wheat,box=FALSE,reg=FALSE) ###
> > view the data wheat$temp = as.factor(wheat$temp) ### convert to factor
> >
> > ### First, try to use nlsList
> > wheat.list <- nlsList(Prop ~ SSlogis(end,phi1,phi2,phi3)|
> > temp,pool=FALSE,data=wheat) ###
> >
> > ### next, try to use lm to estimate starting parameters.
> > wheat.list = list()
> >
> > for (i in 1:length(levels(wheat$temp))){
> >   tmpDat = wheat[wheat$temp == levels(wheat$temp)[i],]
> >   tmp.lm <- lm(Prop ~ end,data = tmpDat)
> >   tmp.nls <- nls(Prop ~ theta1 / (1 + exp(-(theta2 + theta3*end))),
> >       start = list(theta1 = .5,theta2=coef(tmp.lm)[1],theta3 = coef(tmp.lm)[2]),
> >       data = tmpDat,trace=TRUE)
> >   tmp2.nls <- nls(Prop ~ SSlogis(end,phi1,phi2,phi3),data=tmpDat)
> >   wheat.list[i] <- tmp.nls
> > }
> > #### End code
> > nlsList just returns an empty list.
> >
> > When I try to loop through the individual temperatures, for the first
> > temperature, nls converges when I provide starting values, but when I
> > try to use SSlogis(), I get the error messsage
> >
> > Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
> >   NA/NaN/Inf in 'x'
> >
> > For the second temperature (16), I get the following error message
> > when provided initial values using lm:
> >
> > Error in nls(Prop ~ theta1/(1 + exp(-(theta2 + theta3 * end))), start
> > = list(theta1 = 0.5,  :
> >   singular gradient
> >
> > I have tried to read through posts, but none of them seem to apply to this
> case.
> > The data seem relatively simply and
> > I am not sure what I am doing wrong. Any help would be appreciated.
> >
> > Wade
> >

________________________________
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