将生存数据从宽到长转换

时间:2013-03-29 19:29:06

标签: r reshape survival-analysis

我正在阅读http://www.uk.sagepub.com/books/Book233417Rcmdr用于将罗西数据http://cran.r-project.org/doc/contrib/Fox-Companion/Rossi.txt从宽到长转换为时变生存分析。

进行转换的Rcmdr脚本是:

.CovSets <-structure(list(covariate.1 = c("emp1", "emp2", "emp3", "emp4", "emp5", "emp6", "emp7", "emp8", "emp9", "emp10", "emp11", "emp12", "emp13", "emp14", "emp15", "emp16", "emp17", "emp18", "emp19", "emp20", "emp21", "emp22", "emp23", "emp24", "emp25", "emp26", "emp27", "emp28", "emp29","emp30", "emp31", "emp32", "emp33", "emp34", "emp35", "emp36", "emp37", "emp38", "emp39", "emp40", "emp41", "emp42", "emp43", "emp44", "emp45", "emp46", "emp47", "emp48", "emp49", "emp50", "emp51", "emp52")), .Names =  "covariate.1")
Rossi.long <- unfold(Rossi, time="week", event="arrest", cov=.CovSets, 
                                         cov.names=c("covariate.1"))
remove(.CovSets)

但是,如果未加载Rcmdr,则此脚本无法运行。

Rcmdr脚本的结果转换了

中的Rossi数据帧
> head(Rossi,20)
   week arrest fin age race wexp mar paro prio educ emp1 emp2 emp3 emp4 emp5 emp6 emp7 emp8 emp9 emp10 emp11 emp12 emp13 emp14 emp15 emp16 emp17 emp18 emp19 emp20 emp21 emp22 emp23 emp24 emp25
1    20      1   0  27    1    0   0    1    3    3    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0    NA    NA    NA    NA    NA
2    17      1   0  18    1    0   0    1    8    4    0    0    0    0    0    0    0    0    0     1     1     1     1     1     0     0     0    NA    NA    NA    NA    NA    NA    NA    NA
3    25      1   0  19    0    1   0    1   13    3    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0
4    52      0   1  23    1    1   1    1    1    5    0    0    0    0    1    1    1    1    1     1     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0
5    52      0   0  19    0    1   0    1    3    3    0    0    0    0    0    0    0    0    0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
6    52      0   0  24    1    1   0    0    2    4    0    0    0    0    1    1    1    1    1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1
7    23      1   0  25    1    1   1    1    0    4    1    1    1    1    1    1    1    1    1     1     1     1     1     1     1     1     1     1     1     1     1     1     0    NA    NA
8    52      0   1  21    1    1   0    1    4    3    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     1     1     0     0     1     1
9    52      0   0  22    1    0   0    0    6    3    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     1     1     1     1     1     1     1     0     0     0
10   52      0   0  20    1    1   0    0    0    5    0    1    1    1    1    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
11   52      0   1  26    1    0   0    1    3    3    0    0    0    0    0    0    0    0    1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0
12   52      0   0  40    1    1   0    0    2    5    0    0    0    0    1    1    1    1    1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
13   37      1   0  17    1    1   0    1    5    3    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
14   52      0   0  37    1    1   0    0    2    3    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1
15   25      1   0  20    1    0   0    1    3    4    0    0    0    0    0    0    0    0    0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     0     0
16   46      1   1  22    1    1   0    1    2    3    0    1    1    1    1    1    1    1    1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
17   28      1   0  19    1    0   0    0    7    3    0    0    0    0    0    0    0    0    0     0     0     0     0     1     1     1     1     1     0     0     0     0     0     0     0
18   52      0   0  20    1    0   0    0    2    3    0    0    0    0    0    0    0    0    0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
19   52      0   0  25    1    0   0    1   12    3    0    0    0    0    0    0    0    0    0     0     0     1     1     1     1     1     1     1     1     1     1     1     0     0     0
20   52      0   0  24    0    1   0    1    1    3    0    1    1    0    0    0    0    0    1     1     1     1     1     1     0     0     0     1     1     1     1     1     1     1     1
   emp26 emp27 emp28 emp29 emp30 emp31 emp32 emp33 emp34 emp35 emp36 emp37 emp38 emp39 emp40 emp41 emp42 emp43 emp44 emp45 emp46 emp47 emp48 emp49 emp50 emp51 emp52 id
1     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA  1
2     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA  2
3     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA  3
4      0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1  4
5      1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0  5
6      1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0  6
7     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA  7
8      0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0  8
9      0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1  9
10     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0 10
11     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1 11
12     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1 12
13     0     0     1     1     0     0     0     0     0     0     0     0    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA 13
14     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1 14
15    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA 15
16     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     0    NA    NA    NA    NA    NA    NA 16
17     0     0     0    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA 17
18     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0 18
19     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1 19
20     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0 20

到Rossi.long数据

> head(Rossi.long,30)
     start stop arrest.time week arrest fin age race wexp mar paro prio educ id covariate.1
1.1      0    1           0   20      1   0  27    1    0   0    1    3    3  1           0
1.2      1    2           0   20      1   0  27    1    0   0    1    3    3  1           0
1.3      2    3           0   20      1   0  27    1    0   0    1    3    3  1           0
1.4      3    4           0   20      1   0  27    1    0   0    1    3    3  1           0
1.5      4    5           0   20      1   0  27    1    0   0    1    3    3  1           0
1.6      5    6           0   20      1   0  27    1    0   0    1    3    3  1           0
1.7      6    7           0   20      1   0  27    1    0   0    1    3    3  1           0
1.8      7    8           0   20      1   0  27    1    0   0    1    3    3  1           0
1.9      8    9           0   20      1   0  27    1    0   0    1    3    3  1           0
1.10     9   10           0   20      1   0  27    1    0   0    1    3    3  1           0
1.11    10   11           0   20      1   0  27    1    0   0    1    3    3  1           0
1.12    11   12           0   20      1   0  27    1    0   0    1    3    3  1           0
1.13    12   13           0   20      1   0  27    1    0   0    1    3    3  1           0
1.14    13   14           0   20      1   0  27    1    0   0    1    3    3  1           0
1.15    14   15           0   20      1   0  27    1    0   0    1    3    3  1           0
1.16    15   16           0   20      1   0  27    1    0   0    1    3    3  1           0
1.17    16   17           0   20      1   0  27    1    0   0    1    3    3  1           0
1.18    17   18           0   20      1   0  27    1    0   0    1    3    3  1           0
1.19    18   19           0   20      1   0  27    1    0   0    1    3    3  1           0
1.20    19   20           1   20      1   0  27    1    0   0    1    3    3  1           0
2.1      0    1           0   17      1   0  18    1    0   0    1    8    4  2           0
2.2      1    2           0   17      1   0  18    1    0   0    1    8    4  2           0
2.3      2    3           0   17      1   0  18    1    0   0    1    8    4  2           0
2.4      3    4           0   17      1   0  18    1    0   0    1    8    4  2           0
2.5      4    5           0   17      1   0  18    1    0   0    1    8    4  2           0
2.6      5    6           0   17      1   0  18    1    0   0    1    8    4  2           0
2.7      6    7           0   17      1   0  18    1    0   0    1    8    4  2           0
2.8      7    8           0   17      1   0  18    1    0   0    1    8    4  2           0
2.9      8    9           0   17      1   0  18    1    0   0    1    8    4  2           0
2.10     9   10           0   17      1   0  18    1    0   0    1    8    4  2           1

是否可以使用reshape或任何其他数据转换包执行此精确转换?

更新:Rcmdr脚本只能在Rcmdr

内运行

1 个答案:

答案 0 :(得分:2)

'展开'功能位于此处(如您链接到的pdf中所述:

http://socserv.mcmaster.ca/jfox/Books/Companion/scripts/appendix-cox.R

脚本需要Rcmdr。它需要car(反过来会加载MASSnnet但是如果你有Rcmdr那么你必须有car)并且加载survival这是{{1}}推荐的包装,应该可以在所有安装中使用。它在R 3.0.0 beta中运行完成且没有错误,我强烈怀疑它将在R 2.15.x中完成。