You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于后续阴性检测结果修正阳性类检测值的技术实现问询

R语言数据集假阳性检测结果修正逻辑实现

需求说明

针对含受试者ID、检测日期、检测结果的数据集,执行以下修正规则:

  • 同一ID受试者的某次检测结果若属于阳性类(取值为equivocal、indeterminate或positive)
  • 只要该记录的同一天或之后存在negative检测结果
  • 则将该阳性类检测值修正为negative(判定为假阳性)

示例数据集构建

用tidyverse构建

library(tidyverse)
library(lubridate)

sample_data <- tibble(
  ID = c(1,1,1,2,2,3,3,3),
  test_date = ymd(c("2023-01-05", "2023-01-05", "2023-01-10", 
                    "2023-02-01", "2023-02-03", "2023-03-15", "2023-03-20", "2023-03-20")),
  test_result = c("positive", "negative", "equivocal",
                  "indeterminate", "negative", "positive", "positive", "negative")
)

用data.table构建

library(data.table)
library(lubridate)

sample_data_dt <- data.table(
  ID = c(1,1,1,2,2,3,3,3),
  test_date = ymd(c("2023-01-05", "2023-01-05", "2023-01-10", 
                    "2023-02-01", "2023-02-03", "2023-03-15", "2023-03-20", "2023-03-20")),
  test_result = c("positive", "negative", "equivocal",
                  "indeterminate", "negative", "positive", "positive", "negative")
)

需修正的检测项说明

结合上述示例数据集:

  • ID=1:2023-01-05的positive记录同日存在negative,需修正为negative;2023-01-10的equivocal无后续/同日阴性,保留原值
  • ID=2:2023-02-01的indeterminate之后有negative,需修正为negative
  • ID=3:2023-03-15的positive之后有negative,需修正为negative;2023-03-20的positive同日存在negative,需修正为negative

解决方案实现

用tidyverse实现

corrected_data <- sample_data %>%
  group_by(ID) %>%
  # 标记每个ID下,当前记录的同日及之后是否存在negative结果
  mutate(has_later_negative = any(test_result == "negative" & test_date >= test_date)) %>%
  # 应用修正规则
  mutate(corrected_result = case_when(
    test_result %in% c("equivocal", "indeterminate", "positive") & has_later_negative ~ "negative",
    TRUE ~ test_result
  )) %>%
  ungroup()

用data.table实现

# 按ID分组,标记每条记录的同日及之后是否存在negative结果
sample_data_dt[, has_later_negative := any(test_result == "negative" & test_date >= test_date), by = ID]

# 应用修正规则生成修正后结果
sample_data_dt[, corrected_result := fifelse(
  test_result %in% c("equivocal", "indeterminate", "positive") & has_later_negative,
  "negative",
  test_result
)]

内容的提问来源于stack exchange,提问作者Vicki Latham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 15:27:12