[R] cox regression extract strata as numeric

Terry Therneau therneau at mayo.edu
Wed Oct 28 16:47:12 CET 2009


-- begin included message ---

> (basehazzft.ln$stra[285])
[1] stra=2
134 Levels: stra=1 stra=10 stra=100 stra=101 stra=102 ... stra=99
> c(basehazzft.ln$stra[285])
[1] 47

while the desired value is 2, I get a 47. What am I doing wrong? I tried
the as.numeric function but I have the same problem..

--- end included ---

The variable in question is a factor.  The internal codes used for the
levels of a factor variable 'x', as reported by as.numeric(x) or c(x),
are not the same as the names of the levels, which appear from printing
x itself.

The confusing nature of factor variables makes a regular appearance on R
help.  I would suggest reading the section on factor variables in one of
the many introductory R texts. Your question is not per se an issue with
the Cox model routines.  

But to answer your underlying query, try substring(x, 6,10).  

PS If you use survfit() instead of basehaz() you will get back a
survival curve object, which is often much more useful.  I never use
basehaz myself, but there was a cry for a function with the same name
and same (limite) output as the SAS phreg option.  (Basehaz calls
survfit and then throws away 1/2 the information.)
 
Terry T.




More information about the R-help mailing list