@@ -54,17 +54,22 @@ print.survfit <- function(x, scale=1,
5454 mtemp <- if (is.matrix(temp $ matrix )) temp $ matrix
5555 else matrix (temp $ matrix , nrow = 1 ,
5656 dimnames = list (NULL , names(temp $ matrix )))
57- if (all(mtemp [,2 ] == mtemp [,3 ])){
58- cname <- dimnames(mtemp )[[2 ]]
59- mtemp <- mtemp [,- 2 , drop = FALSE ]
60- cname <- cname [- 2 ]
61- cname [2 ] <- " n"
62- dimnames(mtemp )[[2 ]] <- cname
57+ if (dimnames(mtemp )[[2 ]][2 ] == " n.id" ) {
58+ mtemp <- mtemp [,- 3 , drop = FALSE ] # no need for n.start
59+ dimnames(mtemp )[[2 ]][2 ] <- " n"
6360 }
61+ else { # older path
62+ if (all(mtemp [,2 ] == mtemp [,3 ])){
63+ cname <- dimnames(mtemp )[[2 ]]
64+ mtemp <- mtemp [,- 3 , drop = FALSE ]
65+ cname <- cname [- 3 ]
66+ cname [2 ] <- " n"
67+ dimnames(mtemp )[[2 ]] <- cname
68+ }
6469
65- if (all(mtemp [,1 ] == mtemp [,2 ]))
66- mtemp <- mtemp [,- 1 , drop = FALSE ]
67-
70+ if (all(mtemp [,1 ] == mtemp [,2 ]))
71+ mtemp <- mtemp [,- 1 , drop = FALSE ]
72+ }
6873 # for printing, put a footnote on the rmean label
6974 if (rmean != ' none' ) {
7075 dd <- dimnames(mtemp )
@@ -99,7 +104,7 @@ survmean <- function(x, scale=1, rmean) {
99104 # i.e., once per curve. It creates the line of output
100105 #
101106 pfun <- function (nused , time , surv , n.risk , n.event , lower , upper ,
102- start.time , end.time ) {
107+ start.time , end.time , nid ) {
103108 #
104109 # Start by defining a small utility function
105110 # Multiple times, we need to find the x corresponding to the first
@@ -161,16 +166,17 @@ survmean <- function(x, scale=1, rmean) {
161166 varmean <- 0 # placeholders
162167 }
163168
169+ if (is.null(nid )) maxn = max(n.risk ) else maxn = nid
164170 # compute the median and ci(median)
165171 med <- minmin(surv , time )
166172 if (! is.null(upper )) {
167173 upper <- minmin(upper , time )
168174 lower <- minmin(lower , time )
169- c(nused , max( n.risk ) , n.risk [1 ],
175+ c(nused , maxn , n.risk [1 ],
170176 sum(n.event ), sum(mean ), sqrt(varmean ), med , lower , upper )
171177 }
172178 else
173- c(nused , max( n.risk ) , n.risk [1 ], sum(n.event ),
179+ c(nused , maxn , n.risk [1 ], sum(n.event ),
174180 sum(mean ), sqrt(varmean ), med , 0 , 0 )
175181 }
176182
@@ -188,7 +194,7 @@ survmean <- function(x, scale=1, rmean) {
188194 " rmean" , " se(rmean)" , " median" ,
189195 paste(x $ conf.int , c(" LCL" , " UCL" ), sep = ' ' )) # col labels
190196 ncols <- 9 # number of columns in the output
191-
197+ if ( ! is.null( x $ n.id )) plab [ 2 ] <- " n.id "
192198
193199 # Four cases: strata Y/N by ncol(surv)>1 Y/N
194200 # Repeat the code, with minor variations, for each one
@@ -206,17 +212,17 @@ survmean <- function(x, scale=1, rmean) {
206212 if (is.null(x $ conf.int ))
207213 out [i ,] <- pfun(x $ n , stime , surv [,i ], x $ n.risk ,
208214 x $ n.event [,i ],
209- NULL , NULL , start.time , end.time )
215+ NULL , NULL , start.time , end.time , x $ n.id )
210216 else out [i ,] <- pfun(x $ n , stime , surv [,i ], x $ n.risk ,
211217 x $ n.event [,i ],
212218 x $ lower [,i ], x $ upper [,i ], start.time ,
213- end.time )
219+ end.time , x $ n.id )
214220 }
215221 dimnames(out ) <- list (dimnames(surv )[[2 ]], plab )
216222 }
217223 else {
218224 out <- matrix (pfun(x $ n , stime , surv , x $ n.risk , x $ n.event , x $ lower ,
219- x $ upper , start.time , end.time ), nrow = 1 )
225+ x $ upper , start.time , end.time , x $ n.id ), nrow = 1 )
220226 dimnames(out ) <- list (NULL , plab )
221227 }
222228 }
@@ -248,11 +254,12 @@ survmean <- function(x, scale=1, rmean) {
248254 if (is.null(x $ lower ))
249255 out [k ,] <- pfun(x $ n [i ], stime [who ], surv [who ,j ],
250256 x $ n.risk [who ], x $ n.event [who ,j ],
251- NULL , NULL , start.time , end.time [i ])
257+ NULL , NULL , start.time , end.time [i ],
258+ x $ n.id [j ])
252259 else out [k ,] <- pfun(x $ n [i ], stime [who ], surv [who ,j ],
253260 x $ n.risk [who ], x $ n.event [who ,j ],
254261 x $ lower [who ,j ], x $ upper [who ,j ],
255- start.time , end.time [i ])
262+ start.time , end.time [i ], x $ n.id [ j ] )
256263 }
257264 }
258265 }
@@ -264,11 +271,12 @@ survmean <- function(x, scale=1, rmean) {
264271 if (is.null(x $ lower ))
265272 out [i ,] <- pfun(x $ n [i ], stime [who ], surv [who ],
266273 x $ n.risk [who ], x $ n.event [who ],
267- NULL , NULL , start.time , end.time [i ])
274+ NULL , NULL , start.time , end.time [i ],
275+ x $ n.id [i ])
268276 else out [i ,] <- pfun(x $ n [i ], stime [who ], surv [who ],
269277 x $ n.risk [who ], x $ n.event [who ],
270278 x $ lower [who ], x $ upper [who ], start.time ,
271- end.time [i ])
279+ end.time [i ], x $ n.id [ i ] )
272280 }
273281 }
274282 }
0 commit comments