Dear R Community- Is there a logical variable indicating the presence of a warning or error message after executing a command? I’m bootstrapping a logit model (1,000 iterations, relevant code posted below), but the model fails to converge on many iterations. (I’m guessing that a small sample size and the combination of sampling with replacement, high item missingness, and list-wise deletion leads to a sparse data problem for some iterations.) Even for the iterations that fail to converge, though, the program produces parameter estimates. I would like to remove these parameter estimates from N x k matrix containing the boostrapped parameter estimates. What I was thinking of is adding a line of code to check if there is a warning or error message present, recording the iteration numbers where there are error/warning messages, and replacing the parameters for those iterations with NA’s. I’ve checked the help pages for the “warning”, “getOption”, and other commands, but it’s not immediately apparent to me how to check for the presence of warning or error messages. Here’s the relevant portion of the code (I’ve omitted the parts that recover parameter and fit estimates for each iteration): ************************************* [Code Starts Here] #Create Objects N = 1000 #1000 iterations set.seed(7843) seed = sample(10000000, 1000) #1000 Iterations for (i in 1:N){ #Bootstap (Sampling with Replacement) set.seed(seed[i]) pre <- subset(cainit, time==1) sampt1 <- sort(sample(nrow(pre), nrow(pre), replace=T)) sampt1 <- pre[sampt1,] #T1 Model of Vote Intention try(T1A <- glm(prop1a ~ educ + age + inc + dem + appgov + appleg + budget + co1a + prop1b + prop1d,family=binomial(link="logit"), data=sampt1, na.action=na.exclude)) ##[NOTE: the “lrm” command is part of the “Design” package] try(T1a <- lrm(prop1a ~ educ + age + inc + dem + appgov + appleg + budget + co1a + prop1b + prop1d, data=sampt1)) } [Code Ends Here] ****************************************** The following set of warning/error messages is repeated about a dozen times at the end of the loop: Error in lrm(prop1a ~ educ + age + inc + dem + appgov + appleg + budget + : Unable to fit model using “lrm.fit” In addition: Warning messages: 1: glm.fit: fitted probabilities numerically 0 or 1 occurred 2: glm.fit: fitted probabilities numerically 0 or 1 occurred 3: glm.fit: algorithm did not converge 4: glm.fit: fitted probabilities numerically 0 or 1 occurred Any help would be greatly appreciated. Happy New Year! David ============================= David Crow Profesor-Investigador / Assistant Professor División de Estudios Internacionales Centro de Investigación y Docencia Económicas Carretera México-Toluca 3655 Col. Lomas de Santa Fe 01210 México, D.F. Tel.: (+011 52 55) 5727-9800, ext. 2152 ============================= [[alternative HTML version deleted]]
David Winsemius
2011-Jan-03 05:50 UTC
[R] Logical Indicator for Warning and Error Messages?
On Jan 2, 2011, at 11:41 PM, David Crow wrote:> Dear R Community- > > > > Is there a logical variable indicating the presence of a warning or > error > message after executing a command? I?m bootstrapping a logit model > (1,000 > iterations, relevant code posted below), but the model fails to > converge on > many iterations. (I?m guessing that a small sample size and the > combination > of sampling with replacement, high item missingness, and list-wise > deletion > leads to a sparse data problem for some iterations.) > > > > Even for the iterations that fail to converge, though, the program > produces > parameter estimates. I would like to remove these parameter > estimates from > N x k matrix containing the boostrapped parameter estimates. What I > was > thinking of is adding a line of code to check if there is a warning > or error > message present, recording the iteration numbers where there are > error/warning messages, and replacing the parameters for those > iterations > with NA?s. I?ve checked the help pages for the ?warning?, > ?getOption?, and > other commands, but it?s not immediately apparent to me how to check > for the > presence of warning or error messages.?options #says that if "warn" is >= 2 that warnings are turned into errors ?try # for handling of errors -- David.> > > > Here?s the relevant portion of the code (I?ve omitted the parts that > recover > parameter and fit estimates for each iteration): > > > > ************************************* > > [Code Starts Here] > > > > #Create Objects > > N = 1000 #1000 iterations > > set.seed(7843) > > seed = sample(10000000, 1000) > > > > #1000 Iterations > > for (i in 1:N){ > > > > #Bootstap (Sampling with Replacement) > > set.seed(seed[i]) > > pre <- subset(cainit, time==1) > > sampt1 <- sort(sample(nrow(pre), nrow(pre), replace=T)) > > sampt1 <- pre[sampt1,] > > > > > > #T1 Model of Vote Intention > > try(T1A <- glm(prop1a ~ educ + age + inc + dem + appgov + appleg + > budget + > co1a + > > prop1b + prop1d,family=binomial(link="logit"), > > data=sampt1, na.action=na.exclude)) > > > > ##[NOTE: the ?lrm? command is part of the ?Design? package] > > try(T1a <- lrm(prop1a ~ educ + age + inc + dem + appgov + appleg + > budget + > co1a + > > prop1b + prop1d, data=sampt1)) > > } > > > > [Code Ends Here] > > ****************************************** > > > > > > > > The following set of warning/error messages is repeated about a > dozen times > at the end of the loop: > > > > Error in lrm(prop1a ~ educ + age + inc + dem + appgov + appleg + > budget + : > > > Unable to fit model using ?lrm.fit? > > In addition: Warning messages: > > 1: glm.fit: fitted probabilities numerically 0 or 1 occurred > > 2: glm.fit: fitted probabilities numerically 0 or 1 occurred > > 3: glm.fit: algorithm did not converge > > 4: glm.fit: fitted probabilities numerically 0 or 1 occurred > > > > > > > > Any help would be greatly appreciated. > > > > Happy New Year! > > David > > > > =============================> > David Crow > > Profesor-Investigador / Assistant Professor > > Divisi?n de Estudios Internacionales > > Centro de Investigaci?n y Docencia Econ?micas > > > Carretera M?xico-Toluca 3655 > Col. Lomas de Santa Fe > 01210 M?xico, D.F. > Tel.: (+011 52 55) 5727-9800, ext. 2152 > > > > =============================> > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.David Winsemius, MD West Hartford, CT