Ravi Varadhan
2011-Feb-18 16:28 UTC
[R] How to flag those iterations which yield a warning?
Hi, I am running a simulation study with the survival::coxph. Some of the simulations result in problematic fits due to flat partial likelihood. So, you get the warning message: Warning message: In fitter(X, Y, strats, offset, init, control, weights = weights, ... : Loglik converged before variable 2 ; beta may be infinite. How can I keep track of the simulations which yield any kind of warning message, so that I can appropriately handle them in the post-processing of simulation results? I know that `try' can be used to capture errors, but I would like to capture and detect warnings. Any pointers would be appreciated. Thanks, Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu
Ravi Varadhan
2011-Feb-18 16:35 UTC
[R] How to flag those iterations which yield a warning?
May be I could do: for (i in 1:nsim) { last.warning <- NULL # do model fitting if(!is.null(last.warning)) # discard simulation result } I think this might work. Any other ideas? Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: Ravi Varadhan <rvaradhan at jhmi.edu> Date: Friday, February 18, 2011 11:29 am Subject: [R] How to flag those iterations which yield a warning? To: r-help at r-project.org> Hi, > > I am running a simulation study with the survival::coxph. Some of > the simulations result in problematic fits due to flat partial > likelihood. So, you get the warning message: > > Warning message: > In fitter(X, Y, strats, offset, init, control, weights = weights, > ... : > Loglik converged before variable 2 ; beta may be infinite. > > How can I keep track of the simulations which yield any kind of > warning message, so that I can appropriately handle them in the > post-processing of simulation results? > > I know that `try' can be used to capture errors, but I would like to > capture and detect warnings. > > Any pointers would be appreciated. > > Thanks, > Ravi. > ____________________________________________________________________ > > Ravi Varadhan, Ph.D. > Assistant Professor, > Division of Geriatric Medicine and Gerontology > School of Medicine > Johns Hopkins University > > Ph. (410) 502-2619 > email: rvaradhan at jhmi.edu > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
William Dunlap
2011-Feb-18 16:57 UTC
[R] How to flag those iterations which yield a warning?
If you have a function that may produce warnings you can use withCallingHandlers() to make a new function that does what the original does but adds some contextual information to the usual warning report. E.g., addWarningReporting <- function(FUN) { force(FUN) function(args) { withCallingHandlers( FUN(args), warning=function(w){ cat("WARNING: ") print(w) cat(" args=", deparse(args), "\n") invokeRestart("muffleWarning")} ) } }> for(i in -2:2) log(i)Warning messages: 1: In log(i) : NaNs produced 2: In log(i) : NaNs produced> myLog <- addWarningReporting(log) > for(i in -2:2) myLog(i)WARNING: <simpleWarning in FUN(args): NaNs produced> args= -2L WARNING: <simpleWarning in FUN(args): NaNs produced> args= -1L Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Ravi Varadhan > Sent: Friday, February 18, 2011 8:29 AM > To: r-help at r-project.org > Subject: [R] How to flag those iterations which yield a warning? > > Hi, > > I am running a simulation study with the survival::coxph. > Some of the simulations result in problematic fits due to > flat partial likelihood. So, you get the warning message: > > Warning message: > In fitter(X, Y, strats, offset, init, control, weights = > weights, ... : > Loglik converged before variable 2 ; beta may be infinite. > > How can I keep track of the simulations which yield any kind > of warning message, so that I can appropriately handle them > in the post-processing of simulation results? > > I know that `try' can be used to capture errors, but I would > like to capture and detect warnings. > > Any pointers would be appreciated. > > Thanks, > Ravi. > ____________________________________________________________________ > > Ravi Varadhan, Ph.D. > Assistant Professor, > Division of Geriatric Medicine and Gerontology > School of Medicine > Johns Hopkins University > > Ph. (410) 502-2619 > email: rvaradhan at jhmi.edu > > ______________________________________________ > 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. >
Apparently Analagous Threads
- How to capture console output in a numeric format
- How to generate a random variate that is correlated with a given right-censored random variate?
- Can't figure out warning message
- Keeping factors with zero occurrences in "table" output
- Column(row)wise minimum and maximum