[R] Modifying glm.fit() / execution path

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Aug 1 08:31:28 CEST 2010


On Sun, 1 Aug 2010, Benjamin Ridenhour wrote:

> Hello all,
> I'm sure I'm missing something simple here, but I can't figure out how to
> modify the glm.fit() function and then get R to use it (sort of). I'm doing
> something along the lines of:
>
> glm.fit<-edit(glm.fit) # add something trivial to the top of the glm.fit
> function like: print("Hello world!")
>
> #now have a modified glm.fit in position 1/.GlobalEnv
>
> x<-rnorm(20)
> y<-rnomr(20)
>
> glm(y~x)  # I don't get the trivial bit of glm.fit back (i.e. "Hello world!"
> doesn't print)
>
> #but
>
> glm(y~x,method=glm.fit) # works! I get "Hello world!"
>
> My understanding is that glm() should call glm.fit() by default but it seems
> to be calling the glm.fit() in "package:stats" and not ".GlobalEnv". Why
> isn't the function in ".GlobalEnv" superseding the version in
> "package:stats"?

Because that is the way scoping works in R -- you need to understad 
the concept of name spaces.

This is not the way to do what you seem to want: rename your fitter 
function and use

   method: the method to be used in fitting the model.  The default
           method ‘"glm.fit"’ uses iteratively reweighted least squares
           (IWLS), whereas ‘"model.frame"’ which returns the model frame
           and does no fitting.  User-supplied fitting functions can be
           supplied either as a function or a character string naming a
           function, with a function which takes the same arguments as
           ‘glm.fit’.

to specify it.

>
> Thanks,
> Ben

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list