[R] Nonlinear Regression

Spencer Graves spencer.graves at pdf.com
Mon Jun 11 02:24:18 CEST 2007


      Have you worked through the examples in the 'nls' help file, 
especially the following: 

     DNase1 <- subset(DNase, Run == 1)
     fm3DNase1 <- nls(density ~ Asym/(1 + exp((xmid - log(conc))/scal)),
                      data = DNase1,
                      start = list(Asym = 3, xmid = 0, scal = 1),
                      trace = TRUE)

           Treated <- Puromycin[Puromycin$state == "treated", ]
     weighted.MM <- function(resp, conc, Vm, K)
     {
         ## Purpose: exactly as white book p. 451 -- RHS for nls()
         ##  Weighted version of Michaelis-Menten model
         ## ----------------------------------------------------------
         ## Arguments: 'y', 'x' and the two parameters (see book)
         ## ----------------------------------------------------------
         ## Author: Martin Maechler, Date: 23 Mar 2001

         pred <- (Vm * conc)/(K + conc)
         (resp - pred) / sqrt(pred)
     }

     Pur.wt <- nls( ~ weighted.MM(rate, conc, Vm, K), data = Treated,
                   start = list(Vm = 200, K = 0.1),
                   trace = TRUE)
112.5978 :  200.0   0.1
17.33824 :  205.67588840   0.04692873
14.6097 :  206.33087396   0.05387279
14.59694 :  206.79883508   0.05457132
14.59690 :  206.83291286   0.05460917
14.59690 :  206.83468191   0.05461109

# In the call to 'nls' here, 'Vm' and 'K' are in 'start' and must 
therefore be parameters to be estimated. 
# The other names passed to the global 'weighted.MM' must be columns of 
'data = Treated'. 

# To get the residual sum of squares, first note that it is printed as 
the first column in the trace output. 

# To get that from Pur.wt, I first tried 'class(Pur.wt)'. 
# This told me it was of class 'nls'. 
# I then tried "method(class='nls')". 
# One of the functions listed was 'residuals.nls'.  That gave me the 
residuals. 
# I then tried 'sum(residuals(Pur.wt)^2)', which returned 14.59690. 

      Hope this helps. 
      Spencer Graves
p.s.  Did this answer your question?  Your example did not seem to me to 
be self contained, which makes it more difficult for me to know if I'm 
misinterpreting your question.  If the example had been self contained, 
I might have replied a couple of days ago. 

tronter wrote:
> Hello
>
> I followed the example in page 59, chapter 11 of the 'Introduction to R'
> manual. I entered my own x,y data. I used the least squares. My function has
> 5 parameters: p[1], p[2], p[3], p[4], p[5]. I plotted the x-y data. Then I
> used lines(spline(xfit,yfit)) to overlay best curves on the data while
> changing the parameters. My question is how do I calculate the residual sum
> of squares. In the example they have the following:
>
> df <- data.frame( x=x, y=y)
>
> fit <- nls(y ~SSmicmen(s, Vm, K), df)
>
> fit
>
>
> In the second line how would I input my function? Would it be:
>
> fit <- nls(y ~ myfunction(p[1], p[2], p[3], p[4], p[5]), df) where
> myfunction is the actual function? My function doesnt have a name, so should
> I just enter it?
>
> Thanks
>
>



More information about the R-help mailing list