[R] nls fits by groups

Aleksi Lehtonen lehtonen.aleksi at gmail.com
Mon Sep 24 09:46:11 CEST 2007


Dear Katharine,

that for loop solved all my problems, I just added subset=group==i to 
the nls statement.

thank you, Aleksi

Katharine Mullen wrote:
> It is not clear from your post what changes per-group.  If only the
> starting values change (but the data and the model structure are the
> same), then you can just store the starting values you want to use for
> each group in a list, and then index into this list in your call to nls.
> 
> e.g., modifying an example in the help page for nls:
> 
> x <- 1:10
> y <- 2*x + 3                            # perfect fit
> yeps <- y + rnorm(length(y), sd = 0.01) # added noise
> 
> startlist <- list(
>          list(a = 0.12345, b = 0.54321), ##group 1 start val
>          list(a = 0.12, b = 0.54) ## group 2 start val.
> 	         )
> reslist <- list() ## filling this with results from different start val
> for(i in 1:length(startlist)) {
>      reslist[[i]]  <-  nls(yeps ~ a + b*x, start = startlist[[i]],
>          trace = TRUE)
> }
> 
> On Sun, 23 Sep 2007, Aleksi Lehtonen wrote:
> 
>> Dear Colleagues,
>>
>> I am trying to estimate several non-linear models simultaneously. I don't
>> want to use non-linear mixed model, but non-linear model with same form, but
>> it should be estimated separately according to variable group (I have lots
>> of groups that have lots of observations....). I would like to have unique
>> parameters for each group.
>>
>> e.g. something like this
>>
>> mod <- nls(y ~ a*x^b, start=c(a=1, b=1), group=group)
>>
>> but knowing that group option does not work. If someone has an idea (or has
>> done it already) how to implement this either using just nls statement or by
>> building a simple function in R, I would be very grateful for hints....
>>
>> regards, Aleksi Lehtonen
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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