I'm trying to fit raw data to curves, which works well on an individual basis. However, I'd like to "share" parameters (sometimes referred as nested parameters) across more than one data series. Is there a way to do this in R?

  • 1
    $\begingroup$ Please give more details. Some example of individual curve fitting would clarify things a lot. $\endgroup$ – mpiktas Nov 23 '12 at 7:44
  • 1
    $\begingroup$ ( pexp(r1*x) + (1-p)*(exp(-r2*x)) ) / ( pexp(r1*x) + (1-p) ) is the equation I'm trying to fit. Basically, I am varying x in my experiment, and recording the response, y. I am doing this under two different conditions in which I expect the parameter "p" to change, but not the parameters r1, r2; i.e. r1 r2 should be fit "globally" across the two datasets, whereas p should be fit individually, to each datset. $\endgroup$ – asker123 Nov 24 '12 at 5:37
  • $\begingroup$ very good description of global curve fitting: hearne.co.nz/attachments/RegressionBook.pdf See pg. 67..... of course I'd like to be able to do this in R! $\endgroup$ – asker123 Nov 24 '12 at 5:44

If the error variance is also common across data series, the usual way to do such a thing is to "stack" the y's and set up predictors (modified versions of x) so that the parameters that are not in common 'zero out'/remove the effect of the parameters that don't apply to the particular subset.

Here's an example of fitting a model of the form $y = a +$ $\exp$$(b + c x) + e$ to two data sets, and then in a combined fit with a common $c$ parameter.

# create data:
a1 <- -0.82e-2; b1 <-  3.8e-3; c1 <- 9.e-2
a2 <-  2.20e-2; b2 <- -1.3e-3; c2 <- c1

x1 <-  1:10
x2 <-  6:14

n1 <- length(x1)
n2 <- length(x2)

e1 <- c(0.109, 0.511, 1.243, 0.978, -0.584, 1.377, 0.292, -0.897, -0.411, -0.878)
e2 <- c(-0.343, 0.818, -0.059, -0.471, -0.194, -0.398, -1.535,  1.093, -0.721)
sig <- 2.e-3

y1 <-  a1+exp(b1+c1*x1)+sig*e1
y2 <-  a2+exp(b2+c2*x2)+sig*e2

#plot data

# separate fits:  
nls(y1 ~ a1 + exp(b1+c1*x1), start=list(a1=0,b1=4e-3,c1=1e-1))
nls(y2 ~ a2 + exp(b2+c2*x2), start=list(a2=0,b2=-1e-3,c2=1e-1))

#set up stacked variables:
y <- c(y1,y2); x <- c(x1,x2)

lcon1 <- rep(c(1,0), c(n1,n2))
lcon2 <- rep(c(0,1), c(n1,n2))
mcon1 <- lcon1
mcon2 <- lcon2

# combined fit with common 'c' parameter, other parameters separate
nls(y ~ a1*lcon1 + a2*lcon2 + exp(b1*mcon1 + b2*mcon2 + cc * x),
       start = list(a1=0,a2=0,b1=4e-3,b2=-1e-3,cc=1e-1))
| cite | improve this answer | |
  • $\begingroup$ i think i understand your suggestion. Stupid question: how do I get R's nls to fit two y values? $\endgroup$ – asker123 Nov 24 '12 at 4:51
  • $\begingroup$ It looks to me like you didn't quite follow. My suggestion results in a single y to be fitted, composed of first one set of y's and then the other, resulting from the 'stacking' I mentioned. $\endgroup$ – Glen_b Nov 24 '12 at 6:10
  • 1
    $\begingroup$ can you describe in a bit more detail how this can be accomplished? $\endgroup$ – asker123 Nov 24 '12 at 6:55
  • $\begingroup$ Answer has been edited to include an example $\endgroup$ – Glen_b Nov 24 '12 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.