[R] Capture warning messages from coxph()

From: xinyi lin <x1lin_at_ucsd.edu>
Date: Mon, 17 Dec 2007 23:16:53 +0800


Hi,

I want to fit multiple cox models using the coxph() function. To do this, I use a for-loop and save the relevant results in a separate matrix. In the example below, only two models are fitted (my actual matrix has many more columns), one gives a warning message, while the other does not. Right now, I see all the warning message(s) after the for-loop is completed but have no idea which model gave the warning message. Is there a way in which the warning message can be captured and saved (i.e. as a binary variable, having value 1 if there was a warning message and 0 otherwise)? I can't possibly fit the models one by one (and see if they give a warning message) as I have many of them to fit.

> library(survival)

Loading required package: splines
> time= c(4,3,1,1,2,2,3,3,2)
> status=c(1,0,0,0,1,1,1,1,1)
> TIME=Surv(time,status)
> x= cbind(c(0,2,1,1,0,0,0,2,0),c(0,2,1,1,0,0,0,0,0))
>
> results=matrix(NA,ncol=3,nrow=ncol(x))
> colnames(results)=c("coef","se","p")
>
> for(i in 1:ncol(x)){

+ fit=summary(coxph(TIME~x[,i]))
+ results[i,1]=fit$coef[1]
+ results[i,2]=fit$coef[3]
+ results[i,3]=fit$coef[5]
+ rm(fit)
+ }

Warning message:
Loglik converged before variable 1 ; beta may be infinite. in: fitter(X, Y, strats, offset, init, control, weights = weights,
>
> results

            coef se p
[1,] -0.5117033 5.647385e-01 0.36
[2,] -10.2256937 1.146168e+04 1.00
>
> #To see which model gave the warning message
> coxph(TIME~x[,1])

Call:
coxph(formula = TIME ~ x[, 1])

         coef exp(coef) se(coef)      z    p
x[, 1] -0.512       0.6    0.565 -0.906 0.36

Likelihood ratio test=0.97 on 1 df, p=0.324 n= 9
> coxph(TIME~x[,2])

Call:
coxph(formula = TIME ~ x[, 2])

        coef exp(coef) se(coef) z p x[, 2] -10.2 3.62e-05 11462 -0.000892 1

Likelihood ratio test=2.51 on 1 df, p=0.113 n= 9 Warning message:
Loglik converged before variable 1 ; beta may be infinite. in: fitter(X, Y, strats, offset, init, control, weights = weights,

Thank you,
Cindy Lin



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. Received on Mon 17 Dec 2007 - 15:26:37 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 Mon 17 Dec 2007 - 16:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.