# Nonlinear fit to multiple data sets with shared parameters

Hello all,

I need to fit a nonlinear model to several data sets simultaneously. The model has the same functional form for all sets, and the values of some model parameters are the same for all sets (in the following example, r and e), but the values of at least one parameter is different for each set (in the following example, a).
For example, three decay curves might have the same decay constant (r) and asymptotic value (e) but a different amplitude for each data set. The global fit to all three curves would produce one decay constant (r) and asymptotic value (e) and three amplitudes.

What is the best way to do this in R? (Apologizing in advance for a novice question!)

library(tidyverse)
a <- seq(90, 30, length = 3)
r <- .2
e <- 10
# model: y ~ a*(1-r)^(x-1)+e
x <- c(1:30)
d1 <- tibble(x) %>%
mutate(a = a[1], y = a*(1-r)^(x-1)+e, ynorm = rnorm(length(x), y, 1))
d2 <- tibble(x) %>%
mutate(a = a[2], y = a*(1-r)^(x-1)+e, ynorm = rnorm(length(x), y, 1))
d3 <- tibble(x) %>%
mutate(a = a[3], y = a*(1-r)^(x-1)+e, ynorm = rnorm(length(x), y, 1))
d <- rbind(d1, d2, d3)

library(ggplot2)
ggplot(d, aes(x = x, y = y, colour = as.character(a))) +
geom_line() +
geom_point(aes(y = ynorm, colour = as.character(a)))


This is a matter of putting together an object, obj_i with a function, f(x,r,e,a), which requires lining up r, e and a within obj_i with the arguments to f.

Doing it explicitly is straightforward. To iterate over obj_i ... obj_n in a large obj, there are a couple of choices, one involving {purrr}::map and the other a for loop. Usually, the work is in pulling the individual objects and the output.

If using for, initialize a list of a size to hold the expected result and set the counter i outside the loop and increment it within.

Come back with further questions.

\epsilon is inherent in the data, inserting a random term in a model to create noise isn't necessarily helpful, and \epsilon manifests itself in the CI around the test statistic.
Start with a model of a data set, find the appropriate function, such as lm, etc. and generalize it into a function that will take any similarly structured data set as its argument. After that, the number of data sets will suggest whether to do runs seriatim or programmatically.