我希望使用group_by
包中的dplyr
来适应多个nls函数,但我不确定如何传递多个起始值。让我们举一个更简单的例子(见?nls
的灵感)。
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"))
所以我在这里拟合一个模型。但是,如果我想扩展这一点,那么我将适合以下内容:
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"))
我如何传递多个start
参数? purrr
包可以用吗?
答案 0 :(得分:3)
(评论回答。)我的第一个猜测是正确的,.$
语法似乎有效。
作为选择起始值的便捷方式,请在新列中创建一个包含唯一组值和所需起始值的表。我对这些数据一无所知,我随机分配了这些数据:
starts = data.frame(Run = unique(DNase$Run),
xmid_start = rnorm(11, sd = 0.1),
scale_start = 1 + rnorm(11, sd = 0.1))
然后我们可以将其加入到数据中并继续,从每个分组中提取第一个起始值以提供给模型:
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"))