[R] Non linear regression with 2 explanatory variables

Katharine Mullen kate at few.vu.nl
Thu Jan 17 00:21:11 CET 2008


Dear Rolf,

One thing that sometimes makes nls easier to apply is using the 'formula'
argument like you would use the 'fn' argument of optim.  That is, if you
have a residual function that has arguments x, y, a, b and you need to
optimize a and b, you would make a call like

nls(~resid(x,y,a=astart, b=bstart), control = nls.control(warnOnly =
           TRUE, printEval = TRUE), start = list(a=astart, b=bstart))

This did not work easily before R-2.6.0, but does now.  The Puromycin
analysis from the help files is an example of this useage and below is
another.

Or do you already use nls this way and still have problems?

# get data as a sum of exponentials
dataSumOfExp <- function(rates = seq(.05, .005, length=3),
                         times = 1:100,
                         amps = rep(1, length(rates))) {
  tfun <- function(t,r) exp(-r*t)
  ## get C with tfun
  C <- mapply(tfun, r=rates, MoreArgs=list(t=times))
  ## add the columns of C with relative amplitudes 1, and add noise
  C %*% amps + rnorm( nrow(C) )  * max(C) * .00001
}

# residual function
resFun <- function(rates, amps, measured, times = 1:100) {
  tfun <- function(t,r) exp(-r*t)
  CEst <- mapply(tfun, r=rates, MoreArgs=list(t=times))
  measured - CEst %*% amps
}

# get data
measured <- dataSumOfExp()

# optimize rates of exponentials and their relative amplitudes
res <- nls(~resFun(rates = rates, measured = measured, amps = amps),
           control = nls.control(warnOnly = TRUE, printEval = TRUE),
           start = list(rates = c(.04, .1, .001),
             amps = rep(1,3)), trace = TRUE)

summary(res)

On Thu, 17 Jan 2008, Rolf Turner wrote:

>
> I have never had much success in using nls().  If you scan the archives
> you will find one or two postings from me on this topic.  I have
> received
> no useful responses to these postings.
>
> I have found that anything that I tried (and failed) to do using nls()
> could be done quite easily using optim().
>
> 	cheers,
>
> 		Rolf Turner
>
>
> On 17/01/2008, at 3:56 AM, Gavin Simpson wrote:
>
> > hits=-2.6 tests=BAYES_00
> > X-USF-Spam-Flag: NO
> >
> > On Wed, 2008-01-16 at 11:02 +0100, Janice Kielbassa wrote:
> >> Hello!
> >>
> >> I want to do a non-linear regression with 2 explanatory variables
> >> (something like : length ~ a * time * exp( b* temperature)), having a
> >> data set (length, time, temperature). Which function could I use (I
> >> tried nls but I think it doesn't work)
> >
> > Janice, I'll start by saying I can't help you as I have never used
> > nls()
> > myself and I am not familiar with this type of analysis.
> >
> > Why do you think that nls() "doesn't work"? It is a widely used
> > part of
> > R and thus probably very well tested.
> >
> > My understanding of these things is that nls is a sophisticated tool
> > that requires some effort on the part of the user, such as selecting
> > appropriate starting values.
>
>
> ######################################################################
> Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>




More information about the R-help mailing list