0

阿罗哈,

我计划对在全国范围内均匀分布的研究地点进行病例对照研究。我需要选择数据集中的每个案例,然后将其与 x 个控件进行匹配(我们将使用敏感性分析来选择最佳匹配,因此我需要能够运行 1、2、3、4、5 ,6,7,8 等控制数量)。由于数据存在空间元素,因此我想通过选择案例 25000 米内的控件在距离矩阵内运行此计算。

我找不到在 R 中运行此计算的最佳算法。有人知道可以帮助我实现这一目标的最佳 R 包吗?

谢谢

4

1 回答 1

0

为了解决这个问题,我做了以下

获得站点质心的坐标 (x,y)

将数据库拆分为我的病例对照组

运行案例的空间缓冲区

跑了一个控件的交叉点

为所有交叉点分配标签 (match_no)

从 match_no 列中随机抽样

代码如下。

db1 <- read.csv("db1_clf.csv")

library(sf)
dat <- st_as_sf(x=db1,
                   coords = c("x_coor_farm", "y_coor_farm"),
                   crs= "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")


##Filter the positive cases
library(dplyr)
case = dat %>% filter(TB2017 == "1")
control = dat %>% filter(TB2017 == "0")

case_buff = st_buffer(case, dist = 25000)

case_int = st_intersection(case_buff, control)

library(dplyr)

case_int$match_no <- as.integer(factor(case_int$idunique))

library(dplyr)

pos_db <- case_int %>%
  select("idunique", "match_no")

pos_db$geometry= NULL
pos_db <- unique(pos_db)

neg_db <- case_int %>%
  select("idunique.1", "match_no")

neg_db$geometry= NULL
neg_db <- unique(neg_db)


head(neg_db)


####Now the samples####
library(tidyverse)
control1 <- neg_db %>% group_by(match_no) %>% sample_n(1)
control2 <- neg_db %>% group_by(match_no) %>% sample_n(2)
control3 <- neg_db %>% group_by(match_no) %>% sample_n(3)
control4 <- neg_db %>% group_by(match_no) %>% sample_n(4)
control5 <- neg_db %>% group_by(match_no) %>% sample_n(5)
control6 <- neg_db %>% group_by(match_no) %>% sample_n(6)
control7 <- neg_db %>% group_by(match_no) %>% sample_n(7)
control8 <- neg_db %>% group_by(match_no) %>% sample_n(8)
control9 <- neg_db %>% group_by(match_no) %>% sample_n(9)
control10<- neg_db %>% group_by(match_no) %>% sample_n(10)
于 2021-04-01T16:35:33.767 回答