# Re: [R] bootstrap and nls

From: Spencer Graves <spencer.graves_at_pdf.com>
Date: Sun 18 Jul 2004 - 23:45:53 EST

1. Have you studied the documentation, including working the examples, for "nls" and "boot"? I don't see "data" as an argument to "nls", as I'm used to, and your use of "i" also seems disconcerting to me. Something like the following seems to me to be closer to the standard syntax and therefore safer:

nls(SolA~a/(1+b*Tps)^c,start=c(a=31,b=0.5,c=0.6), data=data[i,])

This may not help with this problem, but it might help you with others -- possibly even in understanding how R is structured more generally.

2. If this error occurs in only a very few cases and you can afford to ignore such cases, then you could use "try". I didn't remember exactly the way to do this, so I performed the following experiment:

> Er <- try(if(NA)2)
Error in if (NA) 2 : missing value where TRUE/FALSE needed  > class(Er)
 "try-error"

This leads me to suggest the following:

```dif.param<-function(data,i){
RaiesLossA.nls<-try(nls(SolA[i]~a/(1+b*Tps[i])^c,start=c(a=31,b=0.5,c=0.6)))
RaiesLossB.nls<-try(nls(Solb[i]~a/(1+b*Tps[i])^c,start=c(a=33,b=1.4,c=0.5)))
if((class(RaiesLossA.nls)=="try-error") |
(class(RaiesLossB.nls)=="try-error"))
return("whatever you want in this case")
```
else RaiesLossA.nls\$m\$getPars()-RaiesLossB.nls\$m\$getPars() }

# UNTRIED       3. What is the nature of your data and the physical context? Can the deviations from this model reasonably be considered to be independent normal with constant variance? If yes, then you have a reasonable model. Alternatively, if all numbers are positive, have you considered the following:

log(SolA) = ln.a + c*log(1+b*Tps)

If the deviations from this model seem close to being normally distributed, then you might try using this form with method="plinear". If you do this, you only need starting values for "b". Whether you use "plinear" or not, have you tried getting starting values as follows: Recall that log(1+x) = x+x^2/2+O(x^2). If b*Tps is usually small, then the following might work to produce reasonable starting values:

fit0 <- lm(SolA~Tps + I(Tps^2), data=data[i,])

Then coef(fit0) estimates log(a), c*b and c*b^2/2. Thus, b <- 2*coef(fit0)/coef(fit0), etc.

```
hope this helps.  spencer graves
```

Patrick Giraudoux wrote:

>Hi,
>
>I am trying to bootstrap the difference between each parameters among two non linear regression (distributed loss model) as
>following:
>
># data.frame
>
>
>>Raies[1:10,]
>>
>>
> Tps SolA Solb
>1 0 32.97 35.92
>2 0 32.01 31.35
>3 1 21.73 22.03
>4 1 23.73 18.53
>5 2 19.68 18.28
>6 2 18.56 16.79
>7 3 18.79 15.61
>8 3 17.60 13.43
>9 4 14.83 12.76
>10 4 17.33 14.91
>etc...
>
># non linear model (work well)
>
>RaiesLossA.nls<-nls(SolA~a/(1+b*Tps)^c,start=c(a=32,b=0.5,c=0.6))
>RaiesLossB.nls<-nls(Solb~a/(1+b*Tps)^c,start=c(a=33,b=1.5,c=0.5))
>
>
># bootstrap
>library(boot)
>
>dif.param<-function(data,i){
>RaiesLossA.nls<-nls(SolA[i]~a/(1+b*Tps[i])^c,start=c(a=31,b=0.5,c=0.6))
>RaiesLossB.nls<-nls(Solb[i]~a/(1+b*Tps[i])^c,start=c(a=33,b=1.4,c=0.5))
>RaiesLossA.nls\$m\$getPars()-RaiesLossB.nls\$m\$getPars()
>}
>
>
>
>> myboot<-boot(Raies,dif.param,R=1000)
>>
>>
>
>Error in numericDeriv(form[], names(ind), env) :
> Missing value or an Infinity produced when evaluating the model
>
>It seems that the init values (start=) may come not to be suitable while bootstraping. Data can be sent offline to whom wanted to
>try on the dataset.
>
>Any hint welcome!
>
>Best regards,
>
>Patrick Giraudoux
>
>
>University of Franche-Comté
>Department of Environmental Biology
>EA3184 af. INRA
>F-25030 Besançon Cedex
>
>tel.: +33 381 665 745
>fax.: +33 381 665 797
>http://lbe.univ-fcomte.fr
>
>______________________________________________
>R-help@stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help