Re: [R] Capture warning messages from coxph()

From: Terry Therneau <>
Date: Tue, 18 Dec 2007 07:37:05 -0600 (CST)

Xinyi Li asked how to keep track of which coxph models, called from within a loop, were responsible for warning messges. One solution is to modify the coxph code so that those models are marked in the return coxph object. Below is a set of changes to the final 40 lines of, that will cause the component "infinite.warn" to be added to the result whenever a warning was generated; it will be a vector of T/F showing which component(s) of the coefficient vector generated the warning.   Change the code for, then do

> source('') #or whatever you called it
> coxph <- source('coxph.s')
> coxph.wtest <- survival:::coxph.wtest

Line 2 causes you to have a local version of coxph, otherwise, due to name spaces, the original version of will still be used. Line 3 is another consequence of name spaces. Then code such as

	for(i in 1:ncol(x)){
 	    if (!is.null(fit$infinite.warn)) cat("Waring in fit", i, "\n")
	    sfit <- summary(fit)

Will report out the models with a warning.


  1. The warning message is not completely reliable
  2. Name spaces protect a package from accidental overrides, when a user or some other package reuses a function name. With its hundreds of packages, they are a necessity for R. But sometimes you really do want to override a function; then they are a bit of a pain. Others with a better grasp of R internals may be able to suggest a better override than I have done here.

        Terry Therneau

	infs <- abs(coxfit$u %*% var)
        keep.infs <- F                    # new line
	if (maxiter >1) {
	    if (coxfit$flag == 1000)
		   warning("Ran out of iterations and did not converge")
	    else {
		infs <- ((infs > control$eps) & 
			 infs > control$toler.inf*abs(coef))
		if (any(infs))
		warning(paste("Loglik converged before variable ",
			  "; beta may be infinite. "))
                keep.infs <- T          #new line

	names(coef) <- dimnames(x)[[2]]
	lp <- c(x %*% coef) + offset - sum(coef*coxfit$means)
	score <- exp(lp[sorted])
	coxres <- .C("coxmart", as.integer(n),
	resid <- double(n)
	resid[sorted] <- coxres$resid
	names(resid) <- rownames
	coef[which.sing] <- NA

	temp <- list(coefficients  = coef,  #modified line
		    var    = var,
		    loglik = coxfit$loglik,
		    score  = coxfit$sctest,
		    iter   = coxfit$iter,
		    linear.predictors = as.vector(lp),
		    residuals = resid,
		    means = coxfit$means,
        if (keep.infs) temp$infinite.warn <- infs   #new line
        temp				            #new line

    } mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 18 Dec 2007 - 13:43:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 18 Dec 2007 - 14:30:20 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.