[R] nls fitting & plotting to data subsets defined by combinations of categorical variables
DANIEL PRECIADO
danprec at hotmail.com
Sat Mar 11 16:37:29 CET 2017
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
More information about the R-help
mailing list