[R] nls fitting & plotting to data subsets defined by combinations of categorical variables

J C Nash profjcnash at gmail.com
Sat Mar 11 16:47:58 CET 2017

Given that nls() often gives failures of the "singular gradient" type, you may want to give package nlsr a try with the 
nlxb() function. It should be able to use analytic gradients on this expression, has bounds, and uses a Marquardt 
stabilized solver. It can still fail, but is more robust than nls().

I suspect that explicit looping is a safer way to proceed than trying to use by() within with(). Looping is not the slow 
part of this set of computations.


On 2017-03-11 10:37 AM, DANIEL PRECIADO wrote:
> Dear list,
> I want to apply the same nls function to different subsets of a larger
> dataset. These subsets are defined as unique combinations of two
> (categorical) variables, each one with two levels, so I should obtain 4
> sets of parameters after fitting.
> I have managed to do it in a loop, creating different datasets for each
> one of the sub-groups, and then applying the function to each one
> independently and finally just merging all parameters in a single
> dataset, but this seems pretty inefficient.
> I tried to use by and with, but they don't produce the expected result.
> Rather, I get 4 sets of exactly the same parameters (?), so I know that
> with/by are not actually doing anything, and the function is applied tothe dataset as a whole.
> Here is the call I tried to use:
> test <- with(Data, by(Data, list(Type, Phase), function(x) nls(Response
> ~ k*exp(-((Duration-mu)^2)/(2*sigma^2)), start=c(mu=0,sigma=150,k=0.9),
> upper=c(Inf, Inf, 1), algorithm="port", trace=T,
> control=CSJ_FitControl)))
> Also, I would like to plot the fitted distributions for each sub-group
> in the same plot to be able to directly compare them. I figured that,
> since I have the base nls function and the resulting parameters for
> each subset (stored in a data frame), I should be able to enter these
> on a ggplot call to get the 4 regressions lines plotted along with the
> data, but I can't get that to work either. Or is it necessary to plot
> this at the fitting stage (i.e. with the original data)?
> Thanks for any suggestion
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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