File: factor2.R

package info (click to toggle)
survival 3.8-6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,496 kB
  • sloc: ansic: 8,088; makefile: 77
file content (26 lines) | stat: -rw-r--r-- 927 bytes parent folder | download | duplicates (8)
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
library(survival)
aeq <- function(x,y) all.equal(as.vector(x), as.vector(y))
options(na.action=na.exclude)
#
# More tests of factors in prediction, using a new data set
#
fit <- coxph(Surv(time, status) ~  factor(ph.ecog), lung)

tdata <- data.frame(ph.ecog = factor(0:3))
p1 <- predict(fit, newdata=tdata, type='lp')
p2 <- predict(fit, type='lp')
aeq(p1, p2[match(0:3, lung$ph.ecog)])

fit2 <- coxph(Surv(time, status) ~ factor(ph.ecog) + factor(sex), lung)
tdata <- expand.grid(ph.ecog = factor(0:3), sex=factor(1:2))
p1 <- predict(fit2, newdata=tdata, type='risk')

xdata <- expand.grid(ph.ecog=factor(1:3), sex=factor(1:2))
p2 <- predict(fit2, newdata=xdata, type='risk')
all.equal(p2, p1[c(2:4, 6:8)], check.attributes=FALSE)


fit3 <- survreg(Surv(time, status) ~ factor(ph.ecog) + age, lung)
tdata <- data.frame(ph.ecog=factor(0:3), age=50)
predict(fit, type='lp', newdata=tdata)
predict(fit3, type='lp', newdata=tdata)