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]]
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. >
Check out the subset= argument to nls, e.g. to regress Sepal.Length on Sepal.Width separately for each Species using the built in iris data set: f <- function(s) nls(Sepal.Length ~ a*Sepal.Width, data = iris, start = c(a = 1), subset = iris$Species == s) sapply(levels(iris$Species), f, simplify = FALSE) On 9/23/07, Aleksi Lehtonen <lehtonen.aleksi at gmail.com> 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. >
Aleksi Lehtonen <lehtonen.aleksi <at> gmail.com> writes:> 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....Why not nlsList from the nlme package. It would look something like this: mod <- nlsList(y ~ a*x^b | group, start=c(a=1, b=1)) you might have to add a data argument, too, depending on where your data is. HTH, Ken
Dear Aleksi, there are other approaches you could consider: using nls or gnls (in the package nlme): m1 <- nls(y ~ a[group]*x^b[group], start=list(a=c(1, ..., 1), b=c(1, ..., 1))) or m2 <- gnls(rate ~ a*x^b, params=list(a~group-1, b~group-1), start=list(a=c(1, ..., 1), b=c(1, ..., 1))) In both cases there should be specified as many initial values of a and b as there are groups (levels in the factor group). Both approaches can also be used to specified simplified models where some parameter is assumed to be in common for all groups. Christian