Re: [R] glm inside one self-defined function

From: Mike Wolfgang <mikewolfgang_at_gmail.com>
Date: Thu 24 Aug 2006 - 01:08:52 EST

Thanks Bill and Simon. I wrote a simpler function to test and found out it was stepAIC which causes error, and I still don't know how to solve it. Check out this simple function:

myfun<-function(k){
  xx<-mvrnorm(100,rep(0,10),diag(1,10),empirical=TRUE)   colnames(xx)<-paste("x",1:10,sep='')
  py<-exp(sum(xx))/(1+exp(sum(xx)))
  for (i in 1:k){
    y<-rbinom(100,1,py)
    mydata<-data.frame(cbind(y,xx))
    y.glm<-glm(y~.,binomial,mydata)
    cat("ok here.\n")
    y.step<-stepAIC(y.glm,direction='both',trace=0)     cat("ok here.\n")
    print(summary(y.step))
  }
}

myfun(10)

only one "ok here" is printed.

Mike

On 8/22/06, Bill.Venables@csiro.au <Bill.Venables@csiro.au> wrote:
>
> Mike Wolfgang asks:
>
> >
> > From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Mike Wolfgang
> > Sent: Wednesday, 23 August 2006 1:31 PM
> > To: R-help list
> > Subject: [R] glm inside one self-defined function
> >
> > Hi list,
> >
> > I've searched in R-help and found some related discussions but still
> could
> > not understand this type of error. My own function is pretty complex,
> so I
> > would not put it here, but the basic algorithm is like this:
> > myfun<-function(k){
> > mydata<-...#by someway I create a data frame
> > mymodel<-glm(y~.,family=binomial(),data=mydata)
> > ...#some other stuff
> > }
>
> I think you are leaving out something. Here is a test of what you
> claim gives a problem (R 2.3.1, Windows):
>
> > myfun <- function(n) {
> + z <- rnorm(n)
> + mydata <- data.frame(x = z,
> + y = rbinom(n, size = 1, prob = exp(z)/(1+exp(z))))
> + fm <- glm(y ~ x, binomial, mydata)
> + fm
> + }
> >
> > myfun(100)
>
> Call: glm(formula = y ~ x, family = binomial, data = mydata)
>
> Coefficients:
> (Intercept) x
> 0.1587 1.0223
>
> Degrees of Freedom: 99 Total (i.e. Null); 98 Residual
> Null Deviance: 137.6
> Residual Deviance: 118.3 AIC: 122.3
>
> Not even a murmur of complaint. (This also works in S-PLUS 7.0 but
> earlier versions of S-PLUS gave a problem rather like the one you note,
> curiously.)
>
> Look again at your code and see if the abstract version you give
> really matches what you did, may I suggest?
>
> >
> > as I execute this function, it gives error like this
> > Error in inherits(x, "data.frame") : object "mydata" not found
> >
> > So I guess glm here tries to find "mydata" in the parent environment.
> Why
> > doesn't it take "mydata" inside the function? How to let glm correctly
> > locate it? Is this (scope/environment) mentioned in R manual? Thanks,
> >
> > Mike
>

        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu Aug 24 06:56:42 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 24 Aug 2006 - 08:28:35 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.