r语言 - 从 TXT 加载频繁的子序列



是否可以从.txt文件中加载频繁子序列的列表,并使TraMineR将其识别为序列对象?

不幸的是,我没有原始数据,因此我无法重新创建分析。我拥有的唯一文件是一个包含频繁子序列的.txt文件。我假设它是使用 TraMineR 包中的 seqefsub() 函数创建的,带有 maxGap=2 ,因为数据看起来像上述函数的输出。

read.table()将其读取为数据帧,但据我了解,TraMineR 将事件序列处理为具有许多其他属性的列表,例如,这些属性不包含在此文件中。或者我不知道如何提取它们...

这是.txt文件中的几行的样子:

                                             Subsequence    Support  Count
16                                           (WT4)-(WT3) 0.76666667    805
17                                                 (WL2) 0.76380952    802
18                                                  (S1) 0.76000000    798
19                                             (FRF,WL2) 0.74380952    781
20                                           (WT2)-(WT1) 0.70571429    741

若要从(文本)子序列创建事件序列对象,必须将它们转换为垂直时间戳事件 (TSE) 形式。下面的函数为您的数据完成工作

## Function subseq.to.TSE
##  puts the sequences into TSE format using
##  position as timestamp
##  sdf: a data frame with columns Id, Subsequence, Support and Count.
subseq.to.TSE <- function(sdf){
  tse <- data.frame(id=0, event="", time=0)
  k <- 0
  for (i in 1:nrow(sdf)){
    id <- sdf[i,"Id"]
    s <- sdf[i,"Subsequence"]
    ss <- gsub("\(","",s)
    ss <- gsub("\)","",ss)
    # split transitions
    st <- strsplit(ss, split="-")[[1]]
    for (j in 1:length(st)){
      stt <- strsplit(st[j], split=",")[[1]]
      for(jj in 1:length(stt)){
        k <- k+1
        tse[k,1] <- id
        ## parsing for simultaneous events
        if (!(stt[jj] %in% levels(tse[,2])))
          {levels(tse[,2]) <- c(levels(tse[,2]),stt[jj])}
        tse[k,2] <- stt[jj]
        tse[k,3] <- j
      }
    }
  }
  return(tse)
 }

下面介绍了如何在示例数据上使用它。

我们

首先创建我们命名的数据框s.df

s.df <- data.frame(scan(what=list(Id=0, Subsequence="", Support=double(), Count=0)))
16 (WT4)-(WT3) 0.76666667    805
17 (WL2) 0.76380952    802
18 (S1) 0.76000000    798
19 (FRF,WL2) 0.74380952    781
20 (WT2)-(WT1) 0.70571429    741
# leave a blank line to end the scan

然后,我们从s.df中提取 TSE 数据,并使用 seqecreate 从中创建事件序列对象。最后,我们将计数分配为序列权重。

s.tse <- subseq.to.TSE(s.df)
seqe <- seqecreate(s.tse)
seqeweight(seqe) <- s.df[,"Count"] 

现在,您可以例如使用

seqpcplot(seqe)

最新更新