[R] Capture warning messages from coxph()
jim holtman
jholtman at gmail.com
Mon Dec 17 16:55:08 CET 2007
One way is to turn the 'warnings' into 'errors' and then trap the error:
> library(survival)
>
> 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")
>
> old.warn <- options(warn=2)
> for(i in 1:ncol(x)){
+
+ aa <- try(fit <- summary(coxph(TIME~x[,i])))
+ if (class(aa) == "try-error"){
+ print(paste("i =", i, "had error"))
+ next # skip iteration
+ }
+
+ results[i,1]=fit$coef[1]
+ results[i,2]=fit$coef[3]
+ results[i,3]=fit$coef[5]
+ rm(fit)
+ }
Error in fitter(X, Y, strats, offset, init, control, weights = weights, :
(converted from warning) Loglik converged before variable 1 ; beta
may be infinite.
[1] "i = 2 had error"
> options(old.warn)
>
>
On Dec 17, 2007 10:16 AM, xinyi lin <x1lin at ucsd.edu> wrote:
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list