[R] cox regression extract strata as numeric

Steven McKinney smckinney at bccrc.ca
Wed Oct 28 03:54:40 CET 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of ?e???da? ?pa?t??
> Sent: Tuesday, October 27, 2009 3:51 PM
> To: r-help at r-project.org
> Subject: [R] cox regression extract strata as numeric
> 
> 
> Hi there,
> 
> I perform a stratified cox and then I need the strata as a  numeric
> array "straft.ln"
> ft.ln <-
> coxph(Surv(times,deaths)~ages+chemos+chemos:f1+chemos:f2+horms+horms:f1
> +horms:f2+grades+grades:f1+grades:f2+positives+positives:f1+positives:f
> 2+sizes+sizes:f1+sizes:f2+strata(stra),data=ddd)
> 
> basehazzft.ln=basehaz(ft.ln,centered=FALSE)
> H0ft.ln=c(basehazzft.ln[,1])
> timeft.ln=c(basehazzft.ln[,2])
> straft.ln=c(basehazzft.ln[,3])
> 
> I notice that basehazzft.ln[,3] is not the same with straft.ln with
> respect to the values i.e.:
> 
> (basehazzft.ln$stra[1])
> [1] stra=1
> 134 Levels: stra=1 stra=10 stra=100 stra=101 stra=102 ... stra=99
> > c(basehazzft.ln$stra[1])
> [1] 1
> 
> 
> so far so good but:
> 
> (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..
> 
> I just want instead of the [ "stra=1" "stra=1" "stra=1" etc]  the
> [1,1,1 etc]
> 
> Thanx in advance for any answers!

Here's one way to do it (using the example on coxph help page):

> require("survival")
Loading required package: survival
Loading required package: splines
> bladder1 <- bladder[bladder$enum < 5, ] 
> m1 <- coxph(Surv(stop, event) ~ (rx + size + number) * strata(enum) + cluster(id), bladder1)
> basehazzft.ln=basehaz(m1,centered=FALSE)
> (basehazzft.ln$stra[1])
[1] enum=1
Levels: enum=1 enum=2 enum=3 enum=4
> basehazzft.ln$stra
 [1] enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=1
[14] enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=1 enum=2 enum=2 enum=2 enum=2 enum=2
[27] enum=2 enum=2 enum=2 enum=2 enum=2 enum=2 enum=2 enum=2 enum=2 enum=2 enum=2 enum=2 enum=2
[40] enum=2 enum=3 enum=3 enum=3 enum=3 enum=3 enum=3 enum=3 enum=3 enum=3 enum=3 enum=3 enum=3
[53] enum=3 enum=3 enum=3 enum=3 enum=3 enum=4 enum=4 enum=4 enum=4 enum=4 enum=4 enum=4 enum=4
[66] enum=4 enum=4 enum=4
Levels: enum=1 enum=2 enum=3 enum=4
> as.character(basehazzft.ln$stra)
 [1] "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1"
[11] "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1" "enum=1"
[21] "enum=1" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2"
[31] "enum=2" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2" "enum=2"
[41] "enum=3" "enum=3" "enum=3" "enum=3" "enum=3" "enum=3" "enum=3" "enum=3" "enum=3" "enum=3"
[51] "enum=3" "enum=3" "enum=3" "enum=3" "enum=3" "enum=3" "enum=3" "enum=4" "enum=4" "enum=4"
[61] "enum=4" "enum=4" "enum=4" "enum=4" "enum=4" "enum=4" "enum=4" "enum=4"
> substr(as.character(basehazzft.ln$stra), 6, 999)
 [1] "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "2" "2" "2"
[25] "2" "2" "2" "2" "2" "2" "2" "2" "2" "2" "2" "2" "2" "2" "2" "2" "3" "3" "3" "3" "3" "3" "3" "3"
[49] "3" "3" "3" "3" "3" "3" "3" "3" "3" "4" "4" "4" "4" "4" "4" "4" "4" "4" "4" "4"
> as.numeric(substr(as.character(basehazzft.ln$stra), 6, 999))
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
[49] 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4
>

HTH

Steven McKinney
Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre



> 
> 
> 
> 
> 
> ___________________________________________________________
> ×ñçóéìïðïéåßôå Yahoo!;
> ÂáñåèÞêáôå ôá åíï÷ëçôéêÜ ìçíýìáôá (spam); Ôï Yahoo! Mail
> äéáèÝôåé ôçí êáëýôåñç äõíáôÞ ðñïóôáóßá êáôÜ ôùí åíï÷ëçôéêþí
> ìçíõìÜôùí http://login.yahoo.com/config/mail?.intl=gr
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list