0

我们有时间序列数据,其中对几个受试者进行了重复观察。我想计算positive == 1每个主题(变量id)出现变量的次数。

第二个目的是确定这些连续观察运行的最大长度,其中positive == 1. 对于每个主题,在研究期间可能会有多次运行。我不想计算每个受试者连续阳性观察的最大数量,而是计算单个运行中的最大运行长度。

这是一个说明问题的玩具数据集:

set.seed(1234)
test <- data.frame(id = rep(1:3, each = 10), positive = round(runif(30,0,1)))
test$run <- sequence(rle(test$positive)$lengths)
test$run_positive <- ifelse(test$positive == '0', '0', test$run)
test$episode <- ifelse(test$run_positive == '1', '1', '0')

count(test$episode)
  x freq
1 0   25
2 1    5

上面的代码接近回答我的第一个问题,我试图计算阳性情节的数量,但它不受主题的限制。这具有计算同一运行中对受试者#1 的最后一次观察和对受试者#2 的第一次观察的不利影响。谁能帮我开发代码来按主题调节这种运行长度编码?

其次,如何仅提取每次运行的最大运行长度positive == 1?我想添加一个额外的列,其中仅记录最大运行长度的观察结果。对于主题 #1,这看起来像:

   id positive run run_positive episode max_run
1   1        0   1            0       0       0
2   1        1   1            1       1       0
3   1        1   2            2       0       0
4   1        1   3            3       0       0
5   1        1   4            4       0       0
6   1        1   5            5       0       5
7   1        0   1            0       0       0
8   1        0   2            0       0       0
9   1        1   1            1       1       0
10  1        1   2            2       0       2

如果有人能想出一个方法来做到这一点,我将非常感激。

4

1 回答 1

1

我认为这回答了你的第一个问题:

aggregate(positive ~ id, data = test, FUN = sum)

  id positive
1  1        7
2  2        4
3  3        4

这可能会回答您的第二个问题,但我需要查看每个id要检查的所需结果:

set.seed(1234)
test <- data.frame(id = rep(1:3, each = 10), positive = round(runif(30,0,1)))
test$run <- sequence(rle(test$positive)$lengths)
test$run_positive <- ifelse(test$positive == '0', '0', test$run)
test$episode <- ifelse(test$run_positive == '1', '1', '0')

test$group <- paste(test$id*10, test$positive, sep='')

my.seq <- data.frame(rle(test$group)$lengths)
test$first <- unlist(apply(my.seq, 1, function(x) seq(1,x)))
test$last  <- unlist(apply(my.seq, 1, function(x) seq(x,1,-1)))

test$max <- ifelse(test$last == 1 & test$positive==1, test$run, 0)
test

   id positive run run_positive episode group first last max
1   1        0   1            0       0   100     1    1   0
2   1        1   1            1       1   101     1    5   0
3   1        1   2            2       0   101     2    4   0
4   1        1   3            3       0   101     3    3   0
5   1        1   4            4       0   101     4    2   0
6   1        1   5            5       0   101     5    1   5
7   1        0   1            0       0   100     1    2   0
8   1        0   2            0       0   100     2    1   0
9   1        1   1            1       1   101     1    2   0
10  1        1   2            2       0   101     2    1   2
11  2        1   3            3       0   201     1    2   0
12  2        1   4            4       0   201     2    1   4
13  2        0   1            0       0   200     1    1   0
14  2        1   1            1       1   201     1    1   1
15  2        0   1            0       0   200     1    1   0
16  2        1   1            1       1   201     1    1   1
17  2        0   1            0       0   200     1    4   0
18  2        0   2            0       0   200     2    3   0
19  2        0   3            0       0   200     3    2   0
20  2        0   4            0       0   200     4    1   0
21  3        0   5            0       0   300     1    5   0
22  3        0   6            0       0   300     2    4   0
23  3        0   7            0       0   300     3    3   0
24  3        0   8            0       0   300     4    2   0
25  3        0   9            0       0   300     5    1   0
26  3        1   1            1       1   301     1    4   0
27  3        1   2            2       0   301     2    3   0
28  3        1   3            3       0   301     3    2   0
29  3        1   4            4       0   301     4    1   4
30  3        0   1            0       0   300     1    1   0
于 2013-09-07T02:49:57.897 回答