Skip to content

Fit non-linear regressor

3 messages · Bernardo Rangel Tura, Tony Plate, (Ted Harding)

#
Hi R masters,

Sorry for first mensage, this is orignal text...

y<-c(2.8150,3.5239,4.0980,4.5845,5.0709,5.4824,5.8427,6.3214,6.7349,7.3651)
x<-c(37,42,47,52,57,62,67,72,77,82)

I need fit R and A in y=f(x)=R*exp(A*x), with minimize sd= sqrt(SRR/(n-2)) where SRR is Sum of the Square of the Residuals 
and n is number of data points (in this case 10)

How do I make this?


Thanks in advance

Bernardo Rangel Tura, MD, MSc
National Institute of Cardiology Laranjeiras
Rio de Janeiro Brazil
#
It's reasonably straightforward to use nls() for this:

 > d <- 
data.frame(x=c(37,42,47,52,57,62,67,72,77,82),y=c(2.8150,3.5239,4.0980,4.5845,5.0709,5.4824,5.8427,6.3214,6.7349,7.3651))
 > fit <- nls(y~R*exp(x*A),start=list(R=2,A=0.1),data=d)
 > plot(x,y)
 > lines(x, coef(fit)[1]*exp(x*coef(fit)[2]))
 >

You might want to check that your objective does not have local optima (in 
which case the assumption that minimizing the  sum-squared residual will 
minimize your objective may be false).

hope this helps,

Tony Plate
At Friday 04:35 PM 1/9/2004 -0200, Bernardo Rangel Tura wrote:
Tony Plate   tplate at acm.org
#
On 09-Jan-04 Bernardo Rangel Tura wrote:
What is your objection, with these data, to a linear regression
of log(y) on x? This would give you log(R) as intercept
and A as slope, and you can get back to y by exponentiating;
though it would not quite minimise what you want, rather minimising
the SS of residuals of log(y).

Or indeed, now that I look more closely, even a simple linear
regression of y on x?

[Perhaps you need the fitted relationship to be "realistic" over
 the unobserved range (0,36) of x; but the lower end of this will
 be poorly estimated.]

  y<-c(2.8150,3.5239,4.0980,4.5845,5.0709,
       5.4824,5.8427,6.3214,6.7349,7.3651)
  x<-c(37,42,47,52,57,62,67,72,77,82)
  lm1<-lm(y~x)
  lm2<-lm(log(y)~x)
  plot(x,y,xlim=c(0,85),ylim=c(0,8))
  a<-lm1$coefficients[1];b<-lm1$coefficients[2]
  lR<-lm2$coefficients[1];A<-lm2$coefficients[2]
  u<-(0:85)
  lines(u,a+b*u,col="red")
  lines(u,exp(lR+A*u))

shows that the the fit lm(y~x) fits the data better than lm(log(y)~x),
over the range of X considered. The data do not match the curvature
of the exponentiated lm2 fit.

I tend to doubt that taking the trouble to do a non-linear fit which
minimises the residuals of y would make a meaningful improvement on
lm(log(y)~x) over this range!

More interesting, perhaps, is the apparent sinusoidal effect -- slight,
but clearly visible relative to the above fitted straight line ...

... what does "x" represent?

With best wishes,
Ted.