根据 2 个匹配队列的子类进行重塑,然后进行连续的 McNemar 测试
Reshape based on subclass with respect to the 2 matched cohorts then do consecutive McNemar tests
我要
- 将我的匹配数据(MR 与 MS)重塑为如所附屏幕截图所示;基本上具有相同的列标题,但添加了指代第二组 (MS) 的“.1”。这是在我对它们进行排序以获得匹配对(在名为
subclass
的列中)之后匹配的 2 个队列的输出,因此我可以在那之后进行 McNemar 测试。
- 编写一个函数对连续的相似组进行 McNemar 检验,例如
Gender
vs Gender.1
、Smoking_2gps
vs Smoking_2gps.1
、Diabetes1.0
vs Diabetes1.0.1
.等 MR 组与 MS 组
数据集是 here(显示 sheet 1 用于垂直格式,sheet 2 用于水平所需格式)。
我正在考虑 根据 Status.of.Mitral.Valve
变量
中 2 个匹配队列 MS 与 MR 的子类重塑
修正
当我尝试使用 mcnemar
时,我得到了 list()
。这里是 str(df
**注意:* wide
数据集是我们需要基于它做 mcnemar
的水平格式的数据集。
> str(df)
'data.frame': 124 obs. of 17 variables:
$ Serial.ID : int 39 862 458 581 869 774 888 83 433 655 ...
$ Status.of.Mitral.Valve : chr "MR" "MR" "MR" "MR" ...
$ Age : int 65 60 56 33 50 75 56 79 42 46 ...
$ Gender : Factor w/ 2 levels "female","male": 2 2 2 2 2 2 2 2 2 2 ...
$ Smoking_2gps : int 1 0 0 0 0 1 0 1 0 0 ...
$ COPD : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Diabetes1.0 : int 0 0 0 0 0 0 0 1 0 0 ...
$ Urgent.emergent_procedure : Factor w/ 2 levels "elective","Urgent/emergent procedure": 1 1 1 1 2 1 1 1 1 1 ...
$ MAE : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Postop.Deep.Sternal.Wound.Infection: Factor w/ 1 level "no": 1 1 1 1 1 1 1 1 1 1 ...
$ Myocardial.Infarction : Factor w/ 1 level "no": 1 1 1 1 1 1 1 1 1 1 ...
$ postop.newDialysis : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Takeback.for.Bleeding : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Discharge.Status : Factor w/ 1 level "alive": 1 1 1 1 1 1 1 1 1 1 ...
$ distance : num 0.021 0.339 0.193 0.206 0.105 ...
$ weights : int 1 1 1 1 1 1 1 1 1 1 ...
$ subclass : int 1 2 3 6 7 9 10 11 12 13 ...
我使用的方法是拆分 data.frame,然后再次 cbind
,然后更改 colnames
df <- df[nzchar(df$Status.of.Mitral.Valve), ]
wide <- do.call(cbind, split(df, df$Status.of.Mitral.Valve))
# run install.packages("stringr") if you don't have stringr installed
colnames(wide) <- stringr::str_replace_all(colnames(wide), c("^MS\.(.+)$"="\1.1", "^MR\."=""))
然后我们可以lapply
所有字符类型的变量进入McNemar测试:
sapply(colnames(df)[sapply(df, is.factor)], function(x){
tib <- table(wide[paste0(x, c("",".1"))])
if(any(dim(tib)<2)) NULL else mcnemar.test(tib)$p.value
}) -> lst
lst <- lst[!sapply(lst, is.null)]
data.frame(vars=names(lst) , p.value=unlist(lst, use.names=F))
它给出了想要的输出:
vars p.value
1 Gender 1
2 COPD 1
3 Urgent.emergent_procedure 1
4 MAE 1
我要
- 将我的匹配数据(MR 与 MS)重塑为如所附屏幕截图所示;基本上具有相同的列标题,但添加了指代第二组 (MS) 的“.1”。这是在我对它们进行排序以获得匹配对(在名为
subclass
的列中)之后匹配的 2 个队列的输出,因此我可以在那之后进行 McNemar 测试。 - 编写一个函数对连续的相似组进行 McNemar 检验,例如
Gender
vsGender.1
、Smoking_2gps
vsSmoking_2gps.1
、Diabetes1.0
vsDiabetes1.0.1
.等 MR 组与 MS 组
数据集是 here(显示 sheet 1 用于垂直格式,sheet 2 用于水平所需格式)。
我正在考虑 根据 Status.of.Mitral.Valve
变量
修正
当我尝试使用 mcnemar
时,我得到了 list()
。这里是 str(df
**注意:* wide
数据集是我们需要基于它做 mcnemar
的水平格式的数据集。
> str(df)
'data.frame': 124 obs. of 17 variables:
$ Serial.ID : int 39 862 458 581 869 774 888 83 433 655 ...
$ Status.of.Mitral.Valve : chr "MR" "MR" "MR" "MR" ...
$ Age : int 65 60 56 33 50 75 56 79 42 46 ...
$ Gender : Factor w/ 2 levels "female","male": 2 2 2 2 2 2 2 2 2 2 ...
$ Smoking_2gps : int 1 0 0 0 0 1 0 1 0 0 ...
$ COPD : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Diabetes1.0 : int 0 0 0 0 0 0 0 1 0 0 ...
$ Urgent.emergent_procedure : Factor w/ 2 levels "elective","Urgent/emergent procedure": 1 1 1 1 2 1 1 1 1 1 ...
$ MAE : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Postop.Deep.Sternal.Wound.Infection: Factor w/ 1 level "no": 1 1 1 1 1 1 1 1 1 1 ...
$ Myocardial.Infarction : Factor w/ 1 level "no": 1 1 1 1 1 1 1 1 1 1 ...
$ postop.newDialysis : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Takeback.for.Bleeding : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Discharge.Status : Factor w/ 1 level "alive": 1 1 1 1 1 1 1 1 1 1 ...
$ distance : num 0.021 0.339 0.193 0.206 0.105 ...
$ weights : int 1 1 1 1 1 1 1 1 1 1 ...
$ subclass : int 1 2 3 6 7 9 10 11 12 13 ...
我使用的方法是拆分 data.frame,然后再次 cbind
,然后更改 colnames
df <- df[nzchar(df$Status.of.Mitral.Valve), ]
wide <- do.call(cbind, split(df, df$Status.of.Mitral.Valve))
# run install.packages("stringr") if you don't have stringr installed
colnames(wide) <- stringr::str_replace_all(colnames(wide), c("^MS\.(.+)$"="\1.1", "^MR\."=""))
然后我们可以lapply
所有字符类型的变量进入McNemar测试:
sapply(colnames(df)[sapply(df, is.factor)], function(x){
tib <- table(wide[paste0(x, c("",".1"))])
if(any(dim(tib)<2)) NULL else mcnemar.test(tib)$p.value
}) -> lst
lst <- lst[!sapply(lst, is.null)]
data.frame(vars=names(lst) , p.value=unlist(lst, use.names=F))
它给出了想要的输出:
vars p.value
1 Gender 1
2 COPD 1
3 Urgent.emergent_procedure 1
4 MAE 1