Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Please give more details. Some example of individual curve fitting would clarify things a lot. – mpiktas Nov 23 '12 at 7:44
( 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. – asker123 Nov 24 '12 at 5:37
very good description of global curve fitting: See pg. 67..... of course I'd like to be able to do this in R! – asker123 Nov 24 '12 at 5:44
up vote 0 down vote accepted

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))
share|improve this answer
i think i understand your suggestion. Stupid question: how do I get R's nls to fit two y values? – asker123 Nov 24 '12 at 4:51
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. – Glen_b Nov 24 '12 at 6:10
can you describe in a bit more detail how this can be accomplished? – asker123 Nov 24 '12 at 6:55
Answer has been edited to include an example – Glen_b Nov 24 '12 at 11:17
thanks a lot dude, makes a lot of sense now!! – asker123 Nov 25 '12 at 5:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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