【问题标题】:Is there an R function to compare timestamps in two different datasets?是否有一个 R 函数来比较两个不同数据集中的时间戳?
【发布时间】:2021-12-30 10:23:59
【问题描述】:

我是 R 新手,刚刚开始我的研究工作,所以如果答案很明显,请原谅。 我试图在其他问题中找到答案,但我不确定我是否使用了正确的术语。包括这个相似但不相同的问题 (R Stats: Comparing timestamps in two dataframes)。

对于我的研究问题,我们想测量患者心律失常(心房颤动=afib)的发作情况。 我们使用两种不同的方法来做到这一点:ECG 和 PPG

因此,每个患者有两个不同的数据框。

心电图:

start               | end                   | type
19.10.2020 11:34:53 | 19.10.2020 11:35:24   | noise   
19.10.2020 22:49:53 | 19.10.2020 22:59:53   | Afib
19.10.2020 23:00:21 | 19.10.2020 23:10:53   | Afib
19.10.2020 23:47:14 | 19.10.2020 23:56:22   | Afib

PPG:

start               | end                   | type
19.10.2020 11:25:53 | 19.10.2020 11:40:24   | noise   
19.10.2020 22:49:53 | 19.10.2020 22:59:53   | Afib
19.10.2020 23:00:21 | 19.10.2020 23:15:53   | Afib
19.10.2020 23:42:04 | 19.10.2020 23:54:38   | Afib
20.10.2020 00:02:14 | 20.10.2020 00:19:26   | Afib

每一行代表一集 Afib 或一集噪声(信号不足以检测)。 测量是连续的,但只记录了心律失常事件。

我们想将第二种方法与第一种方法进行比较,看看它是否是检测患者心律失常的可行替代方案。 因此我们要找到:

  • 真阳性:在金标准 (ECG) 和 PPG 中检测到的发作(上例中的第 2 行)

  • 误报:仅使用 PPG 方法检测到的情节。 (上例中的第 5 行)

等等……

到目前为止,我已经更改了时间戳的格式,以便 R 知道它是时间而不仅仅是文本,使用以下行:

ppg$Start<-dmy_hms(ppg$Start, tz=Sys.timezone())
ppg$End<-dmy_hms(ppg$End, tz=Sys.timezone())

导致:

2020-10-19** 22:49:53 | 2020-10-19** 22:59:53 | Afib

真阳性的条件是 ECG 事件与 PPG 事件重叠 30 秒。

我将如何去实现它来计算 R 中的真假阳性?

感谢您的帮助。

【问题讨论】:

  • 您可以按照例如描述进行重叠连接。这里:Overlap join with start and end positionsLinked 其中。对于真正的肯定,创建一组新的开始和结束列,其中包括 30 秒缓冲区。
  • 非常感谢。我已经尝试了所描述的方法,到目前为止,这个概念似乎很有效。
  • 感谢您的反馈。很高兴听到这篇文章很有帮助。 data.table 非 equi 连接确实是一个非常有用的功能。祝你好运!

标签: r


【解决方案1】:

以下函数可能太复杂了,但我认为它可以满足问题的要求。
它的输入参数是

  • X 心电图数据帧
  • Y PPG 数据帧
  • duration 最短持续时间
  • startcol 开始日期时间列的名称
  • endcol 结束日期时间列的名称
  • noisecol 哪一列有type,如果是"noise",就把这一行算出来
  • noiseval 不考虑的值向量。

输出是一个包含成员TPFP 的列表。

overlapDuration <- function(X, Y, duration = 30, startcol, endcol, noisecol, noiseval){
  overlap_length <- function(x, y){
    if(int_overlaps(x, y)){
      xstart <- int_start(x)
      xend <- int_end(x)
      ystart <- int_start(y)
      yend <- int_end(y)
      start <- max(xstart, ystart)
      end <- min(xend, yend)
      int <- interval(start, end)
      int_length(int)
    } else NA
  }
  xname <- deparse(substitute(X))
  yname <- deparse(substitute(Y))
  Xi <- interval(X[[startcol]], X[[endcol]])
  Yi <- interval(Y[[startcol]], Y[[endcol]])
  overl <- sapply(Yi, \(x){
    sapply(Xi, overlap_length, x)
  })
  i <- which(X[[noisecol]] %in% noiseval)
  j <- which(Y[[noisecol]] %in% noiseval)
  overl[i, j] <- NA
  w <- which(!is.na(overl) & overl >= duration, arr.ind = TRUE)
  colnames(w) <- c(xname, yname)
  TP <- cbind(w, secs = overl[w])
  FP <- which(!(rownames(Y) %in% w[, yname] | Y[[noisecol]] %in% noiseval))
  list(TP = TP, FP = FP)
}

minduration <- 30
start <- "start"
end <- "end"
typecol <- "type"
noise <- "noise"
overlapDuration(ECG, PPG, minduration, start, end, typecol, noise)
#$TP
#     ECG PPG secs
#[1,]   2   2  600
#[2,]   3   3  632
#[3,]   4   4  444
#
#$FP
#[1] 5

【讨论】:

  • 非常感谢您所做的详细工作以及您为此付出的努力。我真的很感谢你的帮助。我将在接下来的几天内清理我的数据集时尝试一下。我现在不完全理解代码。但我会进入它,因为它看起来完全符合我的需要。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
相关资源
最近更新 更多