## ggplot(df, aes(x = Time, y = Result, group = Group)) + geom_line()
df_p <-
df %>%
group_by(Group) %>%
#nest the values for modeling
nest() %>%
## init vals are all the same, but this shows how to make them different
mutate(start = list(
list(a = -3, b = 85, c = 4, d = 10),
list(a = -3, b = 85, c = 4, d = 10),
list(a = -3, b = 85, c = 4, d = 10)),
# model using nls and start values defined above
model = map2(data, start,
~ nls(Result ~ a+(b-a)/(1+(Time/c)^d),
data = .x, start = .y)),
# model quality measurements
glance = map(model, broom::glance),
# contains parameters from model
tidy = map(model, broom::tidy),
# estimates and errors
augment = map(model, broom::augment)
)
# create tibble with 250 points along "Time" for each model
lineplot = tibble(Group = rep(df_p$Group, each=250), x = rep(seq(min(df$Result), max(df$Result), len=250), length(df_p$Group)))
# pull out the model parameter estimates and spread them wide for computing
ModelParams <-
unnest(df_p, tidy) %>%
select(1:3) %>%
spread(term, estimate)
# compute model estimates for plotting
Modelline <-
lineplot %>%
full_join(ModelParams, .) %>%
mutate(y = (a+(b-a)/(1+x/c^d)))
# plot and facet with model
ggplot() +
# original points
geom_point(data = df, aes(x = Time, y = Result)) +
# model lines for plotting
geom_line(data = Modelline, aes(x = x, y = y)) +
facet_grid(~Group)