2

我正在阅读http://www.uk.sagepub.com/books/Book233417并且Rcmdr用于转换罗西数据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

4

1 回答 1

2

The 'unfold' function is located here (as documented in the pdf you linked to:

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

The script does not require Rcmdr. It would require car (which in turn loads MASS and nnet but if you have Rcmdr then you must have car) and it does load survival which is a recommended package and should be available in all installations. It runs to completion without error in R 3.0.0 beta and I strongly suspect it would have run to completion in R 2.15.x.

于 2013-03-29T21:12:39.130 回答