File: model.matrix.Rout.save

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

R version 2.11.1 (2010-05-31)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

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
> 
> #
> # Test out the revised model.matrix code
> #
> 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),
+                     z= factor(c('a', 'a', 'b', 'b', 'c', 'c', 'a')))
> 
> fit1 <- coxph(Surv(time, status) ~ z, test1, iter=1)
> fit2 <- coxph(Surv(time, status) ~z, test1, x=T, iter=1)
> all.equal(model.matrix(fit1), fit2$x)
[1] TRUE
> 
> # This has no level 'b', make sure dummies recode properly
> test2 <- 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),
+                     z= factor(c('a', 'a', 'a', 'a', 'c', 'c', 'a')))
> 
> ftest <- model.frame(fit1, data=test2)
> all.equal(levels(ftest$z), levels(test1$z))
[1] TRUE
> 
> # xtest will have one more row than the others, since it does not delete
> #  the observation with a missing value for status
> xtest <- model.matrix(fit1, data=test2)
> dummy <- fit2$x
> dummy[,1] <- 0
> all.equal(xtest[-2,], dummy, check.attributes=FALSE)
[1] TRUE
>