File: factor.Rout.save

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 (56 lines) | stat: -rw-r--r-- 1,808 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
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

R version 2.14.0 (2011-10-31)
Copyright (C) 2011 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.

> #
> # Ensure that factors work in prediction
> #
> library(survival)
Loading required package: splines
> 
> options(na.action="na.exclude") # preserve missings
> options(contrasts=c('contr.treatment', 'contr.poly')) #ensure constrast type
> aeq <- function(x,y, ...) all.equal(as.vector(x), as.vector(y), ...)
> 
> tfit <- coxph(Surv(time, status) ~ age + factor(ph.ecog), lung)
> p1 <- predict(tfit, type='risk')
> 
> # Testing NA handling is important too
> keep <- (is.na(lung$ph.ecog) | lung$ph.ecog !=1)
> lung2 <- lung[keep,]
> p2 <- predict(tfit, type='risk', newdata=lung[keep,])
> aeq(p1[keep], p2)
[1] TRUE
> 
> # Same, for survreg
> tfit <- survreg(Surv(time, status) ~ age + factor(ph.ecog), lung)
> p1 <- predict(tfit, type='response')
> p2 <- predict(tfit, type='response', newdata=lung2)
> aeq(p1[keep], p2)
[1] TRUE
> 
> 
> # Now repeat it tossing the missings
> options(na.action=na.omit) 
> keep2 <- (lung$ph.ecog[!is.na(lung$ph.ecog)] !=1)
> 
> tfit2 <- survreg(Surv(time, status) ~ age + factor(ph.ecog), lung)
> p3 <- predict(tfit2, type='response')
> p4 <- predict(tfit2, type='response', newdata=lung2, na.action=na.omit)
> aeq(p3[keep2] , p4)
[1] TRUE
>