路径:操作父-子'中的事件列表;节点';在R
我感兴趣的是根据预先指定的事件列表(例如诊断、手术、治疗1、治疗2、死亡)可视化患者的路径 测试数据集可能如下所示:路径:操作父-子'中的事件列表;节点';在R,r,igraph,data-manipulation,R,Igraph,Data Manipulation,我感兴趣的是根据预先指定的事件列表(例如诊断、手术、治疗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, 1
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
(请注意,datediff列中的数字不是实际数字)
i、 e.一系列具有日期差异的父子节点
这将允许我绘制节点,并对事件之间的时间进行进一步的描述性分析
我发现了一个用于绘制节点的包(见下文),但是,如果有人知道一种允许箭头宽度反映父子组合数量的方法/包,那就太棒了
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)
require(igraph)#可使用的软件包
家长将数据折叠起来,给出每个家长-孩子组合及其发生次数的计数,例如:
# put the previous event against the current event, and drop the rows before the first event:
df$Event <- as.character(df$Event)
df$PreEvent <- with(df, ave(Event,ID,FUN=function(x) c(NA,head(x,-1)) ) )
result <- df[!is.na(df$PreEvent),c("ID","PreEvent","Event")]
# aggregate the combos by how often they occur:
result <- aggregate(list(count=rownames(result)),result[c("PreEvent","Event")],FUN=length)
# PreEvent Event count
#1 surgery death 1
#2 treatment2 death 2
#3 diagnosis surgery 3
#4 surgery treatment1 1
#5 surgery treatment2 1
#6 treatment1 treatment2 1
# plot in igraph, adjusting the edge.width to account for how many cases of each
# parent-child combo exist:
library(igraph)
g <- graph.data.frame(result)
plot(g,edge.width=result$count)
#将上一个事件与当前事件相对,并删除第一个事件前的行:
df$事件
# put the previous event against the current event, and drop the rows before the first event:
df$Event <- as.character(df$Event)
df$PreEvent <- with(df, ave(Event,ID,FUN=function(x) c(NA,head(x,-1)) ) )
result <- df[!is.na(df$PreEvent),c("ID","PreEvent","Event")]
# aggregate the combos by how often they occur:
result <- aggregate(list(count=rownames(result)),result[c("PreEvent","Event")],FUN=length)
# PreEvent Event count
#1 surgery death 1
#2 treatment2 death 2
#3 diagnosis surgery 3
#4 surgery treatment1 1
#5 surgery treatment2 1
#6 treatment1 treatment2 1
# plot in igraph, adjusting the edge.width to account for how many cases of each
# parent-child combo exist:
library(igraph)
g <- graph.data.frame(result)
plot(g,edge.width=result$count)