From kyzyl at his.com  Wed May  6 18:18:12 2009
From: kyzyl at his.com (Terrence Ireland)
Date: Wed, 06 May 2009 12:18:12 -0400
Subject: [Rd] Scope question concerning calls within a user defined function
Message-ID: <4A01B844.2000400@his.com>

The following is a simple example with a poor solution that shows my
difficulties with scope.  The function /logit.test /has 3 arguments:  
/model.start,/
an initial model; /model.finish/, an all-inclusive model, /my.data/, a 
dataset, in
this case trivial. 

There are 2 function calls in l/ogit.test,/ first to /glm/ to get an 
initial fit (local variable
/logit/) using /model.start;/ then a call to /stepAIC/ using the initial 
fit.

I had thought that l/ogit/ would carry along the dataset, /test.data/ 
for use in
the call to /stepAIC/.  Instead it complains that it cannot find 
/my.data/.  It seems
to have found the name, not the value in the /call/ attribute. 

I fixed the problem by creating a global variable /my.data/ within the 
function,
naming it l/ogit.test.global,/ not a very good solution.

My question is:  How do I handle scope correctly?

Thanks,

Terry Ireland

Script of Experiment:

 > library(MASS)

 > test.data
  Approve Score OldScore
1     Yes     1       12
2     Yes     3       10
3     Yes     5        8
4      No     4        9
5      No     6        7
6      No     8        5
 > test.start
Approve ~ 1
 > test.finish
Approve ~ Score + OldScore
 > logit.test
function(model.start,model.finish,my.data)
  {
    logit <- glm(model.start,binomial(link = 
"logit"),my.data,control=glm.control(epsilon=0.0001),x=TRUE,y=TRUE);
    Table <- stepAIC(logit,scope=model.finish,direction="both",trace=1,k=2);
  }
 > logit.test.global
function(model.start,model.finish,my.data)
  {
    my.data <<- my.data
    logit <- glm(model.start,binomial(link = 
"logit"),my.data,control=glm.control(epsilon=0.0001),x=TRUE,y=TRUE);
    Table <- stepAIC(logit,scope=model.finish,direction="both",trace=1,k=2);
  }

 > logit.test(test.start,test.finish,test.data)
Start:  AIC=10.32
Approve ~ 1

Error in inherits(x, "data.frame") : object "my.data" not found
 > logit.test.global(test.start,test.finish,test.data)
Start:  AIC=10.32
Approve ~ 1

           Df Deviance     AIC
+ Score     1   4.8089  8.8089
+ OldScore  1   4.8089  8.8089
<none>          8.3178 10.3178

Step:  AIC=8.81
Approve ~ Score

        Df Deviance     AIC
<none>       4.8089  8.8089
- Score  1   8.3178 10.3178
 >



[Rd] Scope question concerning calls within a user defined function
Terrence Ireland