Reputation: 3764
I wish to fit multiple nls functions using a group_by
from the dplyr
package but I am unsure how I can pass multiple starting values. Let's take a simpler example (see ?nls
for the inspiration).
DNase1 <- subset(DNase, Run == 1)
modelDNase1 <- DNase1 %>%
do(model = nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)),
data = .,
start = list(xmid = 0, scal = 1),
algorithm = "plinear"))
So here I am fitting a single model. But what if I want to extend this so I am fitting the following:
DNase$Run <- factor(DNase$Run)
modelDNase <- DNase %>%
group_by(Run) %>%
do(model = nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)),
data = .,
start = list(xmid = 0, scal = 1),
algorithm = "plinear"))
How would I pass on multiple start
parameters? Would the purrr
package be of any use?
Upvotes: 3
Views: 1302
Reputation: 145745
(Comment to answer.) My first guess was correct, the .$
syntax seems to work.
As a convenient way of picking starting values, create a table with the unique group values and the desired starting values in new columns. Knowing nothing about this data, I assigned them randomly:
starts = data.frame(Run = unique(DNase$Run),
xmid_start = rnorm(11, sd = 0.1),
scale_start = 1 + rnorm(11, sd = 0.1))
We can then join this to the data and proceed, pulling the first starting value from each grouping to give to the model:
mods = DNase %>%
left_join(starts) %>%
group_by(Run) %>%
do(model = nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)),
data = .,
start = list(xmid = first(.$xmid_start),
scal = first(.$scale_start)),
algorithm = "plinear"))
Upvotes: 4