File: prednew.R

package info (click to toggle)
survival 2.36-14-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,500 kB
  • sloc: asm: 7,352; ansic: 5,369; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 2,590 bytes parent folder | download
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
#
# Make sure that the newdata argument works for various
#   predictions
# We purposely use a subset of the lung data that has only some
#   of the levels of the ph.ecog
library(survival)
options(na.action=na.exclude, contrasts=c('contr.treatment', 'contr.poly'))
aeq <- function(x,y) all.equal(as.vector(x), as.vector(y))

myfit <- coxph(Surv(time, status) ~ age + factor(ph.ecog) + strata(sex), lung)

keep <- which(lung$sex==1 & (lung$ph.ecog==1 | lung$ph.ecog==2))
p1 <- predict(myfit, type='lp')
p2 <- predict(myfit, type="lp", newdata=lung[keep,])
p3 <- predict(myfit, type='lp', se.fit=TRUE)
p4 <- predict(myfit, type="lp", newdata=lung[keep,], se.fit=TRUE)
aeq(p1[keep], p2)
aeq(p1, p3$fit)
aeq(p1[keep], p4$fit)
aeq(p3$se.fit[keep], p4$se.fit)

p1 <- predict(myfit, type='risk')
p2 <- predict(myfit, type="risk", newdata=lung[keep,])
p3 <- predict(myfit, type='risk', se.fit=TRUE)
p4 <- predict(myfit, type="risk", newdata=lung[keep,], se.fit=TRUE)
aeq(p1[keep], p2)
aeq(p1, p3$fit)
aeq(p1[keep], p4$fit)
aeq(p3$se.fit[keep], p4$se.fit)

# The all.equal fails for type=expected, Efron approx, and tied death
#  times due to use of an approximation.  See comments in the source code.
myfit <- coxph(Surv(time, status) ~ age + factor(ph.ecog) + strata(sex), 
               data=lung, method='breslow')
p1 <- predict(myfit, type='expected')
p2 <- predict(myfit, type="expected", newdata=lung[keep,])
p3 <- predict(myfit, type='expected', se.fit=TRUE)
p4 <- predict(myfit, type="expected", newdata=lung[keep,], se.fit=TRUE)
aeq(p1[keep], p2)
aeq(p1, p3$fit)
aeq(p1[keep], p4$fit)
aeq(p3$se.fit[keep], p4$se.fit)

p1 <- predict(myfit, type='terms')
p2 <- predict(myfit, type="terms",newdata=lung[keep,])
p3 <- predict(myfit, type='terms', se.fit=T)
p4 <- predict(myfit, type="terms",newdata=lung[keep,], se.fit=T)
aeq(p1[keep,], p2)
aeq(p1, p3$fit)
aeq(p1[keep,], p4$fit)
aeq(p3$se.fit[keep,], p4$se.fit)

#
# Check out the logic whereby predict does not need to
#  recover the model frame.  The first call should not 
#  need to do so, the second should in each case.
#
myfit <- coxph(Surv(time, status) ~ age + factor(sex), lung, x=T)
p1 <- predict(myfit, type='risk', se=T)
myfit2 <- coxph(Surv(time, status) ~ age + factor(sex), lung)
p2 <- predict(myfit2, type='risk', se=T)
aeq(p1$fit, p2$fit)
aeq(p1$se, p2$se)

p1 <- predict(myfit, type='expected', se=T)
p2 <- predict(myfit2, type='expected', se=T)
aeq(p1$fit, p2$fit)
aeq(p1$se.fit, p2$se.fit)

p1 <- predict(myfit, type='terms', se=T)
p2 <- predict(myfit2, type='terms', se=T)
aeq(p1$fit, p2$fit)
aeq(p1$se.fit, p2$se.fit)