[R] an incredibly trivial question about nls

Rolf Turner r.turner at auckland.ac.nz
Tue Jul 1 23:29:55 CEST 2014

In direct contrast to what Bert says, I think this is a very reasonable 
(and non-trivial) question.

The problem results from Gurus structuring the functions that they write 
in such a way that they are totally opaque to anyone but the 
ultra-cognoscenti.  What is gained by not having things set up in a 
straightforward manner that is accessible to normal human beings is 
mysterious to me.

If you do look at stats:::print.nls (and you have to start with that 
"stats:::"; things just *have* to be hidden away so that normal human 
beings can't see them!) you are likely to be no more enlightened than 
you were previously, until you engage in a good long struggle.

It turns out that what happens is that, in order to print the residual 
sum of squares, print.nls() calls the function x$m$deviance (where "x" 
is the object returned by nls()).  This function simply returns the 
object "dev" which is stored in its environment.  Could one get more 
convoluted and obscure if one tried?

So, to get the residual sum of squares you could do:

	rss <- x$m$deviance()
	rss <- get("dev",envir=environment(x$m$deviance))

The actual residuals are hidden away as "resid" in the environment of 
the function x$m$resid, so you could also get the residual sum of 
squares via:

	rss <- sum(get("resid",envir=environment(x$m$resid))^2)
	rss <- sum(x$m$resid()^2)
	rss <- sum(resid(x)^2)

the last of which applies the (hidden) nls method for the residuals() 
function.  Happily, they all seem to give the same answer. :-)

On 02/07/14 08:40, Bert Gunter wrote:
> 1. Why? What do you think it tells you?

	That's *her* business.

(The number of parameters in a
> NONlinear model is probably not what you think it is).
> 2. ?deviance

	Not at all useful.
> 3. You've been posting all this time and still didn't try
> stats:::print.nls  ?? -- which is where you would find the answer.

	Chastising people for failing to see the invisible is not
	helpful.  And even when they manage to see the invisible, the
	result is still very obscure.



> Cheers,
> Bert
> Bert Gunter
> Genentech Nonclinical Biostatistics
> (650) 467-7374
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
> Clifford Stoll
> On Tue, Jul 1, 2014 at 1:27 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
>> Hello R People:
>> I'm having a forest/trees location problem with the output of nls.
>> If I save the output to an object, and print the object, it shows, amongst
>> other things, the residual sum of squares.  I would like to get that.
>> However, when I look at names or str of the object, I can't find the
>> residual sum of squares.
>> Any help would be much appreciated.
>> thanks,
>> Erin

More information about the R-help mailing list