# [R] Non-Linear Regression Problem

Spencer Graves spencer.graves at pdf.com
Thu Apr 15 17:04:32 CEST 2004

```      Have you considered "try"?

When I do this, I prefer to parameterize the problem to avoid
situations where the function is undefined and provide alternative
algorithms were a function will have numerical difficulties.  For
example, suppose I want (sin(x)/x), and I get NA when x = 0.  I might do
something like the following:

sinx.x <- sin(x)/x
x0 <- (abs(x)<1e-9)
if(any(x0))
sinx.x[x0] <- 1

Sometimes, I'll program a 2- or 3-term Taylor expansion for the
exceptional case and use it when (abs(x)<1e-4), for example.

hope this helps.
spencer graves

Christian Mora wrote:

>Working on the same idea, Ive generated a data grid with 4 vars, two of
>them with its own sequence and two with fixed values. As Spencer pointed
>out one option is to get the values from a simple loop. My question is:
>How can jump from one set of starting values to the next (on the data
>grid) in case they cause an invalid value in the model Im evaluating? In
>my example, Ive 12 possible combinations of the 4 vars and in the 6th
>combination I get an error and the loop is terminated so what Im looking
>for is to skip this problem and continue from the 7th to 12th
>combination of starting points
>
>Ill appreciate any comment
>
>Code:
>
>data<-expand.grid(alpha=100,delta=4,beta=seq(1,2,by=0.5),gamma=seq(.1,.4
>,by=.1))
>for(i in 1:12){
>fit<-nls(y~delta+(alpha-delta)/(1+exp(beta*log(rate/gamma))),data=base,s
>tart=c(alpha=data\$alpha[i],delta=data\$delta[i],beta=data\$beta[i],gamma=d
>ata\$gamma[i]),trace=T)
>}
>
>Thanks
>CMora
>
>
>
>-----Original Message-----
>From: r-help-bounces at stat.math.ethz.ch
>[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Spencer Graves
>Sent: Wednesday, April 14, 2004 12:27 PM
>To: WilDscOp
>Cc: r-help at stat.math.ethz.ch
>Subject: Re: [R] Non-Linear Regression Problem
>
>
>1.  For the equation you mentioned, have you considered the following:
>
>DF <- data.frame(t.=c(1, 4, 16), Y=c(.8, .45, .04))
># I do NOT use "t" as a name, as it
># may conflict with the matrix transpose function.
>fit0 <- lm(log(Y)~t.-1, DF)
>fit0
>Call:
>lm(formula = log(Y) ~ t. - 1, data = DF)
>
>Coefficients:
>     t.
>-0.2012
>################
>
>      If this is the problem you really wanted to solve AND you honestly
>
>need NONLINEAR least squares, I would expect that (-0.2) should provide
>a reasonable starting value for nls:
>
> > fit1 <- nls(Y~exp(-THETA*t.), data=DF, start=c(THETA=-0.2))
> > fit1
>Nonlinear regression model
>  model:  Y ~ exp(-THETA * t.)
>   data:  DF
>    THETA
>0.2034489
> residual sum-of-squares:  0.0003018337
> >
>
>      2.  Alternatively, you could compute the sum of squares for all
>values of THETA = seq(0, .01, 100) in a loop, then find the minimum by
>eye.
>
>      3.  If this is just a toy example, and your real problem has
>several parameters, "expand.grid" will produce a grid, and you can
>compute the value of your function and the sum of squares of residuals
>at every point in the grid in a single loop, etc.
>
>      hope this helps.  spencer graves
>
>WilDscOp wrote:
>
>
>
>>Dear all,
>>
>>    I was wondering if there is any way i could do a "Grid Search" on
>>a parameter space using R (as SAS 6.12 and higher can do it) to start
>>the Newton-Gauss Linearization least squares method when i have NO
>>prior information about the parameter.
>>W. N. Venables and B. D. Ripley (2002) "Modern Applied Statistics with
>>
>>
>
>
>
>>S", 4 th ed., page 216-7  has a topic "Self-starting non-linear
>>regressions" using negexp.SSival - but i can not solve my hypothetical
>>
>>
>
>
>
>>problem using that - my problem is :
>>
>>Y = EXP(-(THETA * t)) with data below for estimating THETA:
>>
>>t        Y
>>1    0.80
>>4     0.45
>>16     0.04
>>
>>Whatever i could do, is in
>>http://www.angelfire.com/ab5/get5/nonlinear.PDF
>>
>>Any response / help / comment / suggestion / idea / web-link / replies
>>
>>
>
>
>
>>will be greatly appreciated.
>>
>>
>>_______________________
>>
>>Mohammad Ehsanul Karim <wildscop at yahoo.com>
>>Institute of Statistical Research and Training
>>University of Dhaka, Dhaka- 1000, Bangladesh
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>>http://www.R-project.org/posting-guide.html
>>
>>
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>http://www.R-project.org/posting-guide.html
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

```