[R] Nonlinear regression and categories

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sat Nov 9 20:35:06 CET 2002


Jonathan Greenberg <greenberg at ucdavis.edu> writes:

> Hi there:
> 
>     I'm trying to run a large number of nonlinear regressions on a time
> series dataset, where the data will be formatted something to the effect of:
> 
> ObservationID,time,dependentvar
> 
> I'll have a number of time and dependentvars for each observation, and I
> want to apply a nonlinear regression to one ObservationID at a time, and I
> want to have a dataset that is the parameter estimates of the equation for
> each observation (as opposed to running them one at a time -- I have several
> million I need to do).  Is there a way to get R to run a nonlinear
> regression based on the above or similar data structure, and return a
> database of ObservationID, parameter1, parameter2, parameter3 for each
> observation?

Have a look at nlsList in the nlme package. You might want to have the
book by Pinheiro+Bates handy to see how the pieces fit together.

In general, R is a programming language, so it's fairly easy to do
this kind of thing even when there is no canned implememntation.
Notice in particular the use of implicit loops and their associated
collection of results. For instance, if you want to avoid nlsList's
habit of storing all the fitted models, you could use by()
constructions instead.

Stuff like

example(nlsList)
do.call("rbind",lapply(fm1,coef)) 

# or, using the same data and model

lst <- by(CO2, CO2$Plant, function(x)coef(nls(uptake~SSasympOff(conc,
          Asym, lrc, c0),data=x, start = c(Asym = 30, lrc = -4.5, c0 = 52)))) 
do.call("rbind", lst)

(if the nls() call can fail, you may need a try() construct)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list