After I updated my R from 2.2.0 to 2.3.1, it is working fine now. Thanks! Mike
On 8/23/06, Prof Brian Ripley <[EMAIL PROTECTED]> wrote: > > Your example works for me without any error messages in R 2.3.1 with the > current (uncredited) MASS package 7.2-27.1, including giving about 20 'ok > here.'. > > Did you heed the advice in the posting guide to update (as well as to tell > us the versions of things you were using)? Probably not (as you sent HTML > mail)! > > [One thing you are leaving out is > > > library(MASS) > > ] > > > On Wed, 23 Aug 2006, Mike Wolfgang wrote: > > > 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, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > > > > Mike Wolfgang asks: > > > > > > > > > > > From: [EMAIL PROTECTED] > > > [mailto:[EMAIL PROTECTED] 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. > > > > -- > Brian D. Ripley, [EMAIL PROTECTED] > 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 > [[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.