r语言 - 转换生存数据从宽到长



我正在阅读http://www.uk.sagepub.com/books/Book233417, Rcmdr用于将Rossi数据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

致罗西。长数据

> 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内运行

'展开'函数位于此处(如您链接到的pdf文档所示):

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

脚本不需要rcdr。它将需要car(反过来加载MASSnnet,但如果你有rcdr,那么你必须有car),它会加载survival,这是一个推荐的包,应该在所有安装中可用。它在R 3.0.0测试版中没有错误地运行完成,我强烈怀疑它将在R 2.15.x中运行完成。

相关内容

  • 没有找到相关文章

最新更新