Hello R Users, I am having difficulty deleting the last warning message in a loop so that the only warning that is produced is that from the most recent line of code. I have tried options(warn=1), rm(last.warning), and resetting the last.warning using something like:> warning("Resetting warning message")This problem has been addressed in a previous listserve string, however I do not follow the advice given. See the below web link. Any help would be greatly appreciated. Thanks! Aaron Wells https://stat.ethz.ch/pipermail/r-help/2008-October/176765.html A general example is first, followed by an example with the loop. --------------------------------------------------------------------------------------------------------------------------------------------- Example 1:> demo.glm<-glm(test.data[,1]~c(1:38)+I(c(1:38)^2),family=binomial) ### Generalized linear model run on the first column of my example data> warnings() ### no warnings reportedNULL> demo.glm<-glm(test.data[,9]~c(1:38)+I(c(1:38)^2),family=binomial) ### Generalized linear model run on the 9th column of my example dataWarning messages: 1: In glm.fit(x = X, y = Y, weights = weights, start = start, etastart = etastart, : algorithm did not converge 2: In glm.fit(x = X, y = Y, weights = weights, start = start, etastart = etastart, : fitted probabilities numerically 0 or 1 occurred> warnings() ### the model with column 9 as data produces warningsWarning messages: 1: In glm.fit(x = X, y = Y, weights = weights, start = start, ... : algorithm did not converge 2: In glm.fit(x = X, y = Y, weights = weights, start = start, ... : fitted probabilities numerically 0 or 1 occurred> demo.glm<-glm(test.data[,1]~c(1:38)+I(c(1:38)^2),family=binomial) ### Re-run the model with column 1 as data> warnings() ### reports the same warnings from the column 9 model, ideally it would report the actual warning message for the column 1 model ("NULL") as aboveWarning messages: 1: In glm.fit(x = X, y = Y, weights = weights, start = start, ... : algorithm did not converge 2: In glm.fit(x = X, y = Y, weights = weights, start = start, ... : fitted probabilities numerically 0 or 1 occurred ---------------------------------------------------------------------------------------------------------------------------------------------- Example 2: Loop ###In the below example I have reset warnings() before each iteration by using warning("Resetting warning message"). I would like the warnings to somehow be consolidated into a list that I could later examine to determine which model iterations ran with and without warnings. The below code doesn't work because the functions are being run in the loop environment, and not the base environment.> test.warn<-rep(0,ncol(test.data));test.warn<-as.list(test.warn) > > > for (i in 1:ncol(test.data)) {warn.reset<-warning("Resetting warning message")+ demo.glm<-glm(test.data[,i]~c(1:38)+I(c(1:38)^2),family=binomial) + warn.new<-warnings() + cbind.warn<-cbind(warn.reset,warn.new) + test.warn[[i]]<-cbind.warn + test.warn + } There were 38 warnings (use warnings() to see them)> test.warn[[1]] warn.reset warn.new Resetting warning message "Resetting warning message" NULL [[2]] warn.reset warn.new Resetting warning message "Resetting warning message" NULL . . . Aaron F. Wells, PhD Senior Scientist ABR, Inc. 2842 Goldstream Road Fairbanks, AK 99709 _________________________________________________________________ [[elided Hotmail spam]] plorer 8. [[elided Hotmail spam]] [[alternative HTML version deleted]]
William, The function keepWarnings that you wrote did the trick. Thanks for the help! Aaron> Subject: Re: [R] deleting/removing previous warning message in loop > Date: Fri, 27 Mar 2009 13:33:51 -0700 > From: wdunlap@tibco.com > To: awells10@hotmail.com > > You try a using function like the following (based > on suppressWarnings): > keepWarnings <- function(expr) { > localWarnings <- list() > value <- withCallingHandlers(expr, > warning = function(w) { > localWarnings[[length(localWarnings)+1]] <<- w > invokeRestart("muffleWarning") > }) > list(value=value, warnings=localWarnings) > } > It returns a 2-element list, the first being the value > of the expression given to it and the second being a > list of all the warnings. Your code can look through > the list of warnings and decide which to omit. E.g., > > > d<-data.frame(x=1:10, y=rep(c(FALSE,TRUE),c(4,6))) > > z <- keepWarnings(glm(y~x, data=d, family=binomial)) > > z$value > > Call: glm(formula = y ~ x, family = binomial, data = d) > > Coefficients: > (Intercept) x > -200.37 44.52 > > Degrees of Freedom: 9 Total (i.e. Null); 8 Residual > Null Deviance: 13.46 > Residual Deviance: 8.604e-10 AIC: 4 > > z$warnings > [[1]] > <simpleWarning in glm.fit(x = X, y = Y, weights = weights, start > start, etastart = etastart, mustart = mustart, offset = offset, > family = family, control = control, intercept = attr(mt, > "intercept") > 0): algorithm did not converge> > > [[2]] > <simpleWarning in glm.fit(x = X, y = Y, weights = weights, start > start, etastart = etastart, mustart = mustart, offset = offset, > family = family, control = control, intercept = attr(mt, > "intercept") > 0): fitted probabilities numerically 0 or 1 occurred> > > > str(z$warnings[[1]]) > List of 2 > $ message: chr "algorithm did not converge" > $ call : language glm.fit(x = X, y = Y, weights = weights, start > start, etastart = etastart, mustart = mustart, offset = offset, > family = family, control = control, ... > - attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition" > > sapply(z$warnings, function(w)w$message) > [1] "algorithm did not converge" > [2] "fitted probabilities numerically 0 or 1 occurred" > > You can filter out the ones you don't want to hear about > and recall warning() with the interesting ones or present > them in some other way. > > > Bill Dunlap > TIBCO Software Inc - Spotfire Division > wdunlap tibco.com > > ------------------------------- > I am having difficulty deleting the last warning message in a loop so > that the only warning that is produced is that from the most recent line > of code. I have tried options(warn=1), rm(last.warning), and resetting > the last.warning using something like: ..._________________________________________________________________ [[elided Hotmail spam]] plorer 8. [[elided Hotmail spam]] [[alternative HTML version deleted]]