我已经看到多个问题,询问有关按ID从基线开始的计算变化的问题,但是每个ID的基线总是通过对找到基线的列进行排序来固定的。在我的情况下,基线定义为relative_change小于或等于20的值。我已将“基线”列中的值标记为我自己的参考。
单个ID可以存在多个基线,并且基线之后只能存在一个值。目的是确定在一定时间段(例如1个月)后,是否能够持续维持相对于基线的变化。
相对变化的公式为(x-lag(x))/((((x + lag(x))/ 2))* 100
lag(x)值固定为ID的基线值,直到下一个基线值。
我期望的结果(对于ID 1,手动计算):
PatientId date value relative_change baseline baseline_change
1 1/16/2015 47.6102 NA
1 1/21/2015 44.1548 -7.53
1 1/29/2015 51.0441 14.47
1 2/5/2015 57.5873 12.05
1 2/20/2015 50.6741 -12.77
1 2/27/2015 48.5987 -4.18
1 3/6/2015 53.0718 8.8
1 3/13/2015 58.2327 9.27
1 4/10/2015 65.7751 12.16 baseline
1 5/1/2015 53.0103 -21.49 -21.49
1 5/14/2015 56.1289 5.71 -15.83
1 5/29/2015 61.6908 9.44 -6.41
1 6/16/2015 55.7482 -10.12 -16.50
1 9/25/2015 60.7776 8.63 baseline
1 11/19/2015 48.3786 -22.72 -22.72
1 11/26/2015 54.8932 12.62 -10.17
1 12/3/2015 55.9178 1.85 -8.33
2 1/6/2015 82.8417 NA
2 1/6/2015 82.8417 0
2 2/17/2015 99.9565 18.73 baseline
2 5/25/2015 75.914 -27.34
2 6/23/2015 97.1738 24.57
2 8/18/2015 84.2979 -14.19
2 11/17/2015 83.8618 -0.52
2 12/8/2015 79.0915 -5.85 baseline
2 12/17/2015 61.9022 -24.38
2 12/22/2015 88.5522 35.43
3 2/3/2015 57.6384 NA baseline
3 5/26/2015 46.865 -20.62
3 6/30/2015 46.4967 -0.79
3 7/10/2015 55.1934 17.1
3 7/16/2015 56.7106 2.71 baseline
3 11/24/2015 39.4603 -35.87
3 12/1/2015 58.0226 38.08
3 12/8/2015 58.5091 0.83
4 3/11/2015 55.0243 NA baseline
4 8/12/2015 34.3651 -46.22
4 8/21/2015 53.7478 44
4 11/30/2015 44.1216 -19.67
4 12/11/2015 53.9511 20.05
5 4/16/2015 62.5017 NA baseline
5 7/23/2015 43.6771 -35.46
6 3/17/2015 68.6899 NA
7 6/2/2015 65.4696 -4.8 baseline
7 9/15/2015 51.4805 -23.92
7 12/8/2015 43.1847 -17.53
8 3/3/2015 72.9291 NA baseline
8 6/2/2015 53.1171 -31.44
8 11/2/2015 63.9017 18.43
到目前为止我所拥有的:
data <- data %>% group_by(id) %>% mutate(baseline_change = (((value-value[baseline == "baseline"])/((value+value[baseline == "baseline"])/2))*100))
我到目前为止的结果: -基线计算正确,因为从基线开始的baseline_change为0。 -基线值之后的第一个baseline_change计算应与第一个relative_change值相同。不太确定我在做什么错。
PatientId date value relative_change baseline baseline_change
1 1/16/2015 47.6102 NA -27.6166817
1 1/21/2015 44.1548 -7.53 -27.350208
1 1/29/2015 51.0441 14.47 -22.3960131
1 2/5/2015 57.5873 12.05 -5.2491378
1 2/20/2015 50.6741 -12.77 -22.958536
1 2/27/2015 48.5987 -4.18 -20.0384681
1 3/6/2015 53.0718 8.8 -19.3132356
1 3/13/2015 58.2327 9.27 -4.1872335
1 4/10/2015 65.7751 12.16 baseline 0
1 5/1/2015 53.0103 -21.49 -12.7798728
1 5/14/2015 56.1289 5.71 -14.6654281
1 5/29/2015 61.6908 9.44 1.5025272
1 6/16/2015 55.7482 -10.12 -15.2442186
1 9/25/2015 60.7776 8.63 baseline 0
1 11/19/2015 48.3786 -22.72 -26.4484585
1 11/26/2015 54.8932 12.62 -9.6818565
1 12/3/2015 55.9178 1.85 -14.9863702
2 1/6/2015 82.8417 NA -17.1222482
2 1/6/2015 82.8417 0 4.7415968
2 2/17/2015 99.9565 18.73 baseline 0
2 5/25/2015 75.914 -27.34 -4.0174987
2 6/23/2015 97.1738 24.57 -2.783911
2 8/18/2015 84.2979 -14.19 6.5827554
2 11/17/2015 83.8618 -0.52 -16.1017042
2 12/8/2015 79.0915 -5.85 baseline 0
2 12/17/2015 61.9022 -24.38 -38.0708608
2 12/22/2015 88.5522 35.43 11.9617152
3 2/3/2015 57.6384 NA baseline 0
3 5/26/2015 46.865 -20.62 -17.3611283
3 6/30/2015 46.4967 -0.79 -19.3303423
3 7/10/2015 55.1934 17.1 -2.6753376
3 7/16/2015 56.7106 2.71 baseline 0
3 11/24/2015 39.4603 -35.87 -30.4181229
3 12/1/2015 58.0226 38.08 0.6665695
3 12/8/2015 58.5091 0.83 3.1713648
4 3/11/2015 55.0243 NA baseline
4 8/12/2015 34.3651 -46.22 -37.5455935
4 8/21/2015 53.7478 44 -2.3198841
4 11/30/2015 44.1216 -19.67 -19.8143366
4 12/11/2015 53.9511 20.05 -1.950411
5 4/16/2015 62.5017 NA baseline 0
5 7/23/2015 43.6771 -35.46 -30.1185408
6 3/17/2015 68.6899 NA 4.9187715
7 6/2/2015 65.4696 -4.8 baseline 0
7 9/15/2015 51.4805 -23.92 -21.3673216
7 12/8/2015 43.1847 -17.53 -34.0385461
8 3/3/2015 72.9291 NA baseline 0
8 6/2/2015 53.1171 -31.44 -27.1661106
8 11/2/2015 63.9017 18.43 -12.3783236