我感兴趣的是根据预先指定的事件列表(例如诊断、手术、治疗1、治疗2、死亡)可视化患者的路径。
测试数据集可能如下所示:
df <- structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L), .Label = c("a", "b", "c"), class = "factor"),
Event = structure(c(2L, 3L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
5L, 1L), .Label = c("death", "diagnosis", "surgery", "treatment1",
"treatment2"), class = "factor"), date = structure(c(14610,
14619, 16667, 14975, 14976, 14977, 15074, 15084, 15006, 15050,
15051, 15053), class = "Date")), .Names = c("ID", "Event",
"date"), row.names = c(NA, 12L), class = "data.frame")
> df
ID Event date
1 a diagnosis 2010-01-01
2 a surgery 2010-01-10
3 a death 2015-08-20
4 b diagnosis 2011-01-01
5 b surgery 2011-01-02
6 b treatment1 2011-01-03
7 b treatment2 2011-04-10
8 b death 2011-04-20
9 c diagnosis 2011-02-01
10 c surgery 2011-03-17
11 c treatment2 2011-03-18
12 c death 2011-03-20
数据已按 ID 和日期排序。
我所追求的是以下内容:
> result
ID parent child datediff
1 a diagnosis surgery 9
2 a surgery death 1950
3 b diagnosis surgery 1
4 b surgery treatment1 1
5 b treatment1 treatment2 90
6 b treatment2 death 10
7 c diagnosis surgery 45
8 c surgery treatment2 1
9 c treatment2 death 2
(请注意,datediff 列中的数字并非实际数字)
即一系列父子节点,它们之间的日期不同。
这将使我能够绘制节点,并对事件之间的时间进行一些进一步的描述性分析。
我找到了一个绘制节点的包(见下文)...有没有一种方法/包可以让箭头宽度反映父子组合的数量?
require(igraph) # possible package to use
parents<-c("A","A","A","A","A","A","C","C","F","F","H","I")
children<-c("I","I","I","I","B","A","D","H","G","H","I","J")
begats<-data.frame(parents=parents,children=children)
graph_begats<-graph.data.frame(begats)
tkplot(graph_begats)