File: doweight.Rout.save

package info (click to toggle)
survival 2.37-7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,684 kB
  • ctags: 364
  • sloc: asm: 6,453; ansic: 4,857; makefile: 2
file content (429 lines) | stat: -rw-r--r-- 15,309 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429

R version 2.15.0 (2012-03-30)
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: i686-pc-linux-gnu (32-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> options(na.action=na.exclude) # preserve missings
> options(contrasts=c('contr.treatment', 'contr.poly')) #ensure constrast type
> library(survival)
Loading required package: splines
> 
> # Tests of the weighted Cox model
> #
> # Similar data set to test1, but add weights,
> #                                    a double-death/censor tied time
> #                                    a censored last subject
> # The latter two are cases covered only feebly elsewhere.
> # 
> # The data set testw2 has the same data, but done via replication
> #
> aeq <- function(x,y) all.equal(as.vector(x), as.vector(y))
> 
> testw1 <- data.frame(time=  c(1,1,2,2,2,2,3,4,5),
+ 		    status= c(1,0,1,1,1,0,0,1,0),
+ 		    x=      c(2,0,1,1,0,1,0,1,0),
+ 		    wt =    c(1,2,3,4,3,2,1,2,1))
> xx <- c(1,2,3,4,3,2,1,2,1)
> testw2 <- data.frame(time=   rep(c(1,1,2,2,2,2,3,4,5), xx),
+ 		     status= rep(c(1,0,1,1,1,0,0,1,0), xx),
+ 		     x=      rep(c(2,0,1,1,0,1,0,1,0), xx),
+ 		     id=     rep(1:9, xx))
> indx <- match(1:9, testw2$id)
> testw2 <- data.frame(time=   rep(c(1,1,2,2,2,2,3,4,5), xx),
+ 		     status= rep(c(1,0,1,1,1,0,0,1,0), xx),
+ 		     x=      rep(c(2,0,1,1,0,1,0,1,0), xx),
+ 		     id=     rep(1:9, xx))
> indx <- match(1:9, testw2$id)
> 
> fit0 <- coxph(Surv(time, status) ~x, testw1, weights=wt,
+ 		    method='breslow', iter=0)
> fit0b <- coxph(Surv(time, status) ~x, testw2, method='breslow', iter=0)
> fit  <- coxph(Surv(time, status) ~x, testw1, weights=wt, method='breslow')
> fitb <- coxph(Surv(time, status) ~x, testw2, method='breslow')
> 
> texp <- function(beta) {  # expected, Breslow estimate
+     r <- exp(beta)
+     temp <- cumsum(c(1/(r^2 + 11*r +7), 10/(11*r +5), 2/(2*r+1)))
+     c(r^2, 1,r,r,1,r,1,r,1)* temp[c(1,1,2,2,2,2,2,3,3)]
+     }
> aeq(texp(0),  c(1/19, 1/19, rep(103/152, 5), rep(613/456,2))) #verify texp()
[1] TRUE
> 
> xbar <- function(beta) { # xbar, Breslow estimate
+     r <- exp(beta)
+     temp <- r* rep(c(2*r + 11, 11/10, 1), c(2, 5, 2))
+     temp * texp(beta)
+     }
> 
> fit0
Call:
coxph(formula = Surv(time, status) ~ x, data = testw1, weights = wt, 
    method = "breslow", iter = 0)


  coef exp(coef) se(coef) z p
x    0         1    0.586 0 1

Likelihood ratio test=0  on 1 df, p=1  n= 9, number of events= 5 
> summary(fit)
Call:
coxph(formula = Surv(time, status) ~ x, data = testw1, weights = wt, 
    method = "breslow")

  n= 9, number of events= 5 

    coef exp(coef) se(coef)     z Pr(>|z|)
x 0.8596    2.3621   0.7131 1.205    0.228

  exp(coef) exp(-coef) lower .95 upper .95
x     2.362     0.4233    0.5839     9.556

Concordance= 0.638  (se = 0.159 )
Rsquare= 0.171   (max possible= 0.999 )
Likelihood ratio test= 1.69  on 1 df,   p=0.1932
Wald test            = 1.45  on 1 df,   p=0.2281
Score (logrank) test = 1.52  on 1 df,   p=0.217

> aeq(resid(fit0), testw1$status - texp(0))
[1] TRUE
> resid(fit0, type='score')
          1           2           3           4           5           6 
 1.24653740  0.03601108  0.10056700  0.10056700 -0.22180142 -0.21193300 
          7           8           9 
 0.46569858 -0.10082189  0.91014302 
> resid(fit0, type='scho')
         1          2          2          2          4 
 1.3157895  0.3125000  0.3125000 -0.6875000  0.3333333 
> 
> aeq(resid(fit0, type='mart'), (resid(fit0b, type='mart'))[indx])
[1] TRUE
> aeq(resid(fit0, type='scor'), (resid(fit0b, type='scor'))[indx])
[1] TRUE
> aeq(unique(resid(fit0, type='scho')), unique(resid(fit0b, type='scho')))
[1] TRUE
> 
> 
> aeq(resid(fit, type='mart'), testw1$status - texp(fit$coef))
[1] TRUE
> resid(fit, type='score')
          1           2           3           4           5           6 
 0.88681615  0.02497653  0.03608964  0.03608964 -0.54297652 -0.12528780 
          7           8           9 
 0.29564605 -0.09476911  0.58400064 
> resid(fit, type='scho')
         1          2          2          2          4 
 1.0368337  0.1613774  0.1613774 -0.8386226  0.1746960 
> aeq(resid(fit, type='mart'), (resid(fitb, type='mart'))[indx])
[1] TRUE
> aeq(resid(fit, type='scor'), (resid(fitb, type='scor'))[indx])
[1] TRUE
> aeq(unique(resid(fit, type='scho')), unique(resid(fitb, type='scho')))
[1] TRUE
> rr1 <- resid(fit, type='mart')
> rr2 <- resid(fit, type='mart', weighted=T)
> aeq(rr2/rr1, testw1$wt)
[1] TRUE
> 
> rr1 <- resid(fit, type='score')
> rr2 <- resid(fit, type='score', weighted=T)
> aeq(rr2/rr1, testw1$wt)
[1] TRUE
> 
> fit  <- coxph(Surv(time, status) ~x, testw1, weights=wt, method='efron')
> fit
Call:
coxph(formula = Surv(time, status) ~ x, data = testw1, weights = wt, 
    method = "efron")


   coef exp(coef) se(coef)    z    p
x 0.873      2.39    0.713 1.22 0.22

Likelihood ratio test=1.75  on 1 df, p=0.186  n= 9, number of events= 5 
> resid(fit, type='mart')
          1           2           3           4           5           6 
 0.85334536 -0.02560716  0.32265266  0.32265266  0.71696234 -1.07772629 
          7           8           9 
-0.45034077 -0.90490339 -0.79598658 
> resid(fit, type='score')
          1           2           3           4           5           6 
 0.88116056  0.02477248  0.06057806  0.06057806 -0.59724033 -0.16737066 
          7           8           9 
 0.38040295 -0.13750290  0.66631324 
> resid(fit, type='scho')
         1          2          2          2          4 
 1.0325955  0.1621759  0.1621759 -0.8378241  0.1728229 
> 
> # Tests of the weighted Cox model, AG form of the data
> #   Same solution as doweight1.s
> #
> testw3 <- data.frame(id  =  c( 1, 1, 2, 3, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9),
+ 		     begin= c( 0, 5, 0, 0,10,15, 0, 0,14, 0, 0, 0,23, 0),
+ 		     time=  c( 5,10,10,10,15,20,20,14,20,20,30,23,40,50),
+ 		    status= c( 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0),
+ 		    x=      c( 2, 2, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0),
+ 		    wt =    c( 1, 1, 2, 3, 3, 3, 4, 3, 3, 2, 1, 2, 2, 1))
> 
> fit0 <- coxph(Surv(begin,time, status) ~x, testw3, weights=wt,
+ 		    method='breslow', iter=0)
> fit  <- coxph(Surv(begin,time, status) ~x, testw3, weights=wt, method='breslow')
> fit0
Call:
coxph(formula = Surv(begin, time, status) ~ x, data = testw3, 
    weights = wt, method = "breslow", iter = 0)


  coef exp(coef) se(coef) z p
x    0         1    0.586 0 1

Likelihood ratio test=0  on 1 df, p=1  n= 14, number of events= 5 
> summary(fit)
Call:
coxph(formula = Surv(begin, time, status) ~ x, data = testw3, 
    weights = wt, method = "breslow")

  n= 14, number of events= 5 

    coef exp(coef) se(coef)     z Pr(>|z|)
x 0.8596    2.3621   0.7131 1.205    0.228

  exp(coef) exp(-coef) lower .95 upper .95
x     2.362     0.4233    0.5839     9.556

Concordance= 0.638  (se = 0.159 )
Rsquare= 0.114   (max possible= 0.991 )
Likelihood ratio test= 1.69  on 1 df,   p=0.1932
Wald test            = 1.45  on 1 df,   p=0.2281
Score (logrank) test = 1.52  on 1 df,   p=0.217

> resid(fit0, type='mart', collapse=testw3$id)
          1           2           3           4           5           6 
 0.94736842 -0.05263158  0.32236842  0.32236842  0.32236842 -0.67763158 
          7           8           9 
-0.67763158 -0.34429825 -1.34429825 
> resid(fit0, type='score', collapse=testw3$id)
          1           2           3           4           5           6 
 1.24653740  0.03601108  0.10056700  0.10056700 -0.22180142 -0.21193300 
          7           8           9 
 0.46569858 -0.10082189  0.91014302 
> resid(fit0, type='scho')
        10         20         20         20         40 
 1.3157895  0.3125000  0.3125000 -0.6875000  0.3333333 
> 
> resid(fit, type='mart', collapse=testw3$id)
          1           2           3           4           5           6 
 0.85531186 -0.02593169  0.17636221  0.17636221  0.65131344 -0.82363779 
          7           8           9 
-0.34868656 -0.64894181 -0.69807852 
> resid(fit, type='score', collapse=testw3$id)
          1           2           3           4           5           6 
 0.88681615  0.02497653  0.03608964  0.03608964 -0.54297652 -0.12528780 
          7           8           9 
 0.29564605 -0.09476911  0.58400064 
> resid(fit, type='scho')
        10         20         20         20         40 
 1.0368337  0.1613774  0.1613774 -0.8386226  0.1746960 
> fit0 <- coxph(Surv(begin, time, status) ~x,testw3, weights=wt, iter=0)
> resid(fit0, 'mart', collapse=testw3$id)
          1           2           3           4           5           6 
 0.94736842 -0.05263158  0.44454887  0.44454887  0.44454887 -0.88126566 
          7           8           9 
-0.88126566 -0.54793233 -1.54793233 
> resid(coxph(Surv(begin, time, status) ~1, testw3, weights=wt)
+ 		      , collapse=testw3$id)  #Null model
          1           2           3           4           5           6 
 0.94736842 -0.05263158  0.44454887  0.44454887  0.44454887 -0.88126566 
          7           8           9 
-0.88126566 -0.54793233 -1.54793233 
> 
> fit  <- coxph(Surv(begin,time, status) ~x, testw3, weights=wt, method='efron')
> fit
Call:
coxph(formula = Surv(begin, time, status) ~ x, data = testw3, 
    weights = wt, method = "efron")


   coef exp(coef) se(coef)    z    p
x 0.873      2.39    0.713 1.22 0.22

Likelihood ratio test=1.75  on 1 df, p=0.186  n= 14, number of events= 5 
> resid(fit, type='mart', collapse=testw3$id)
          1           2           3           4           5           6 
 0.85334536 -0.02560716  0.32265266  0.32265266  0.71696234 -1.07772629 
          7           8           9 
-0.45034077 -0.90490339 -0.79598658 
> resid(fit, type='score', collapse=testw3$id)
          1           2           3           4           5           6 
 0.88116056  0.02477248  0.06057806  0.06057806 -0.59724033 -0.16737066 
          7           8           9 
 0.38040295 -0.13750290  0.66631324 
> resid(fit, type='scho')
        10         20         20         20         40 
 1.0325955  0.1621759  0.1621759 -0.8378241  0.1728229 
> #
> # Check out the impact of weights on the dfbetas
> #    Am I computing them correctly?
> #
> wtemp <- rep(1,26)
> wtemp[c(5,10,15)] <- 2:4
> fit <- coxph(Surv(futime, fustat) ~ age + ecog.ps, ovarian, weights=wtemp)
> rr <- resid(fit, 'dfbeta')
> 
> fit1 <- coxph(Surv(futime, fustat) ~ age + ecog.ps, ovarian, weights=wtemp,
+ 	         subset=(-5))
> fit2 <- coxph(Surv(futime, fustat) ~ age + ecog.ps, ovarian, weights=wtemp,
+ 	         subset=(-10))
> fit3 <- coxph(Surv(futime, fustat) ~ age + ecog.ps, ovarian, weights=wtemp,
+ 	         subset=(-15))
> 
> #
> # Effect of case weights on expected survival curves post Cox model
> #
> fit0  <- coxph(Surv(time, status) ~x, testw1, weights=wt, method='breslow',
+ 	       iter=0)
> fit0b <- coxph(Surv(time, status) ~x, testw2, method='breslow', iter=0)
> 
> surv1 <- survfit(fit0, newdata=list(x=0))
> surv2 <- survfit(fit0b, newdata=list(x=0))
> aeq(surv1$surv, surv2$surv)
[1] TRUE
> #
> # Check out the Efron approx. 
> #
> 
> fit0 <- coxph(Surv(time, status) ~x,testw1, weights=wt, iter=0)
> fit  <- coxph(Surv(time, status) ~x,testw1, weights=wt)
> resid(fit0, 'mart')
          1           2           3           4           5           6 
 0.94736842 -0.05263158  0.44454887  0.44454887  0.44454887 -0.88126566 
          7           8           9 
-0.88126566 -0.54793233 -1.54793233 
> resid(coxph(Surv(time, status) ~1, testw1, weights=wt))  #Null model
          1           2           3           4           5           6 
 0.94736842 -0.05263158  0.44454887  0.44454887  0.44454887 -0.88126566 
          7           8           9 
-0.88126566 -0.54793233 -1.54793233 
> 
> # lfun is the known log-likelihood for this data set, worked out in the
> #   appendix of Therneau and Grambsch
> # ufun is the score vector and ifun the information matrix
> lfun <- function(beta) {
+     r <- exp(beta)
+     a <- 7*r +3
+     b <- 4*r +2
+     11*beta - ( log(r^2 + 11*r +7) + 
+ 	(10/3)*(log(a+b) + log(2*a/3 +b) + log(a/3 +b)) + 2*log(2*r +1))
+     }
> aeq(fit0$log[1], lfun(0))
[1] TRUE
> aeq(fit$log[2], lfun(fit$coef))
[1] TRUE
> 
> ufun <- function(beta, efron=T) {  #score statistic
+     r <- exp(beta)
+     xbar1 <- (2*r^2+11*r)/(r^2+11*r +7)
+     xbar2 <- 11*r/(11*r +5)
+     xbar3 <-  2*r/(2*r +1)
+     xbar2b<- 26*r/(26*r+12)
+     xbar2c<- 19*r/(19*r + 9)
+     temp <- 11 - (xbar1 + 2*xbar3)
+     if (efron) temp - (10/3)*(xbar2 + xbar2b + xbar2c)
+     else       temp - 10*xbar2
+     }
> print(ufun(fit$coef) < 1e-4)  # Should be true
   x 
TRUE 
> 
> ifun <- function(beta, efron=T) {  # information matrix
+     r <- exp(beta)
+     xbar1 <- (2*r^2+11*r)/(r^2+11*r +7)
+     xbar2 <- 11*r/(11*r +5)
+     xbar3 <-  2*r/(2*r +1)
+     xbar2b<- 26*r/(26*r+12)
+     xbar2c<- 19*r/(19*r + 9)
+     temp <- ((4*r^2 + 11*r)/(r^2+11*r +7) - xbar1^2) +
+ 	    2*(xbar3 - xbar3^2)
+     if (efron) temp + (10/3)*((xbar2- xbar2^2) + (xbar2b - xbar2b^2) +
+ 			      (xbar2c -xbar2c^2))
+     else       temp + 10 * (xbar2- xbar2^2)
+     }
> 
> aeq(fit0$var, 1/ifun(0))
[1] TRUE
> aeq(fit$var, 1/ifun(fit$coef))
[1] TRUE
> 
> 
>       
> # Make sure that the weights pass through the residuals correctly
> rr1 <- resid(fit, type='mart')
> rr2 <- resid(fit, type='mart', weighted=T)
> aeq(rr2/rr1, testw1$wt)
[1] TRUE
> rr1 <- resid(fit, type='score')
> rr2 <- resid(fit, type='score', weighted=T)
> aeq(rr2/rr1, testw1$wt)
[1] TRUE
> 
> #
> # Look at the individual components
> #
> dt0 <- coxph.detail(fit0)
> dt <- coxph.detail(fit)
> aeq(sum(dt$score), ufun(fit$coef))  #score statistic
[1] TRUE
> aeq(sum(dt0$score), ufun(0))
[1] TRUE
> aeq(dt0$hazard, c(1/19, (10/3)*(1/16 + 1/(6+20/3) + 1/(6+10/3)), 2/3))
[1] TRUE
> 
> 
> 
> rm(fit, fit0, rr1, rr2, dt, dt0)
> #
> # Effect of weights on the robust variance
> #
> test1 <- data.frame(time=  c(9, 3,1,1,6,6,8),
+                     status=c(1,NA,1,0,1,1,0),
+                     x=     c(0, 2,1,1,1,0,0),
+ 		    wt=    c(3,0,1,1,1,1,1),
+                     id=    1:7)
> testx <- data.frame(time=  c(4,4,4,1,1,2,2,3),
+                     status=c(1,1,1,1,0,1,1,0),
+                     x=     c(0,0,0,1,1,1,0,0),
+ 		    wt=    c(1,1,1,1,1,1,1,1),
+                     id=    1:8)
>  
> fit1 <- coxph(Surv(time, status) ~x + cluster(id), test1, method='breslow',
+               weights=wt)
> fit2 <- coxph(Surv(time, status) ~x + cluster(id), testx, method='breslow')
> 
> db1 <- resid(fit1, 'dfbeta', weighted=F)
> db1 <- db1[-2]         #toss the missing
> db2 <- resid(fit2, 'dfbeta')
> aeq(db1, db2[3:8])
[1] TRUE
> 
> W <- c(3,1,1,1,1,1)   #Weights, after removal of the missing value
> aeq(fit2$var, sum(db1*db1*W))
[1] TRUE
> aeq(fit1$var, sum(db1*db1*W*W))
[1] TRUE
> 
> 
> proc.time()
   user  system elapsed 
  0.292   0.036   0.325