[R] use of step.gam (from package 'gam') and superassignment inside functions

Megan Ferguson Megan.Ferguson at noaa.gov
Thu Feb 28 20:55:00 CET 2008


Hello,

I am using the function step.gam() from the 'gam' package (header info 
from library(help=gam) included below) and have come across some 
behavior that I cannot understand.  In short, I have written a function 
that 1) creates a dataframe, 2) calls gam() to create a gam object, then 
3) calls step.gam() to run stepwise selection on the output from gam(). 
  When I do this, gam() can successfully build the model, but step.gam() 
is unable to see the dataframe that was created in my personal function 
and upon which my gam model was built.  I've included stand-alone code 
below that will recreate the error.  My code shows that this behavior 
doesn't occur with step().  If I use the superassignment operator "<<-" 
to define the dataframe "gam.jack" in the example below, I do not get an 
error.  It seems like gam() finds gam.jack in the local environment of 
fxn, but step.gam() is looking only in the global environment for 
gam.jack.  If anyone has further insight into what is going on here, I'd 
really appreciate it!

***Header info from library(help=gam)***

                 Information on package 'gam'

Description:

Package:       gam
Title:         Generalized Additive Models
Date:          2006-07-11
Version:       0.98
Author:        Trevor Hastie
Description:   Functions for fitting and working with generalized 
additive models, as described in chapter 7
                of "Statistical Models in S" (Chambers and Hastie (eds), 
1991), and "Generalized Additive
                Models" (Hastie and Tibshirani, 1990).
Maintainer:    Trevor Hastie <hastie at stanford.edu>
Depends:       R (>= 2.0), stats, splines
Suggests:      akima
License:       GPL2.0
Packaged:      Wed Dec 27 10:24:21 2006; hornik
Built:         R 2.6.0; i386-pc-mingw32; 2007-10-05 17:00:44; windows
***End of header for library gam***


***Code that produces the error inserted below***

library(gam)

rm(list = ls())
data(gam.data)
fxn <- function() {
     gam.jack <- gam.data[sample(1:100, 50), ]
     print(summary(gam.jack))
     gam.object <- gam(y~x+z, data=gam.jack)
     print(summary(gam.object))
     step.object <- step.gam(gam.object, 
scope=list("x"=~1+x+s(x,4)+s(x,6)+s(x,12),"z"=~1+z+s(z,4)))
}
fxn()
print(summary(gam.jack))


rm(list= ls())
data(swiss)
fxn <- function() {
   swiss.jack <- swiss[sample(1:nrow(swiss), 20), ]
   lm1 <- lm(Fertility ~ ., data = swiss.jack)
   slm1 <- step(lm1)
}
fxn()

***End of code***

-- 
Megan C. Ferguson
Southwest Fisheries Science Center
8604 La Jolla Shores Dr.
La Jolla, CA 92037
(858)546-7118 W
(858)546-7003 Fax
Megan.Ferguson at noaa.gov



More information about the R-help mailing list