Trying to answer someones question on the ggplot2 mailing list and I can't figure it out: https://groups.google.com/forum/#!topic/ggplot2/YgCqQX8JbPM
OP wants to apply different start parameters to subsets of his data for the nls model. My thoughts were that he should read up on dplyr and purrr, but after a few hours of trying myself I've hit a wall. Unsure if it's a bug or my lack of experience with purrr.
library(tidyverse)
# input dataset
df <- data.frame(Group = c(rep("A", 7), rep("B", 7), rep("C", 7)),
Time = c(rep(c(1:7), 3)),
Result = c(100, 96.9, 85.1, 62.0, 30.7, 15.2, 9.6,
10.2, 14.8, 32.26, 45.85, 56.25, 70.1, 100,
100, 55.61, 3.26, -4.77, -7.21, -3.2, -5.6))
# nest the datasets for computing models
df_p <-
df %>%
group_by(Group) %>%
nest
# add model parameters as rows/columns
df_p$starta = c(-3, 4,-3)
df_p$startb = c(85, 85, 85)
df_p$startc = c(4, 4, 4)
df_p$startd = c(10,10,10)
# compute models using nls
df_p %>%
mutate(model2 = map(data, ~nls(Result ~ a+(b-a)/(1+(Time/c)^d), data = ., start = c(a = starta, b = startb, c = startc, d = startd)))
)
#Error in mutate_impl(.data, dots) :
# parameters without starting value in 'data': a, b, d
Feels related to this bug, but this has been fixed for a while now... https://github.com/hadley/dplyr/issues/1447
From what I can tell, it's looking for the variables in the scope of the nested tibble, but I want it to be in the scope of the mutate call. I don't know if there is a way around this.
The example data is tricky because Group B basically has time in reverse. Finding good initial values for that is not my problem. So I made up new data for Group B. Here's how to set up a data frame in order to apply
nls()
inside ofmap2()
.