【发布时间】:2021-09-07 13:36:52
【问题描述】:
我有一个包含 161 个免疫标记的数据集,每个标记都是数据框中的一个向量。使用 R,我想使用 Wilcoxon 符号秩(配对)检验比较 78 对这些向量。免疫标记的名称以“_MOM”或“_CB”来区分。
这是一个带有示例变量名称的“玩具”数据集:
# Create toy data frame
toydata = data.frame(CCBB_dyad_number=c(1,2,3,4,5,6,7,8,9,10),
cCMV_status = c("cCMV+", "cCMV-", "cCMV-",
"cCMV+", "cCMV+", "cCMV-",
"cCMV-", "cCMV+", "cCMV+",
"cCMV+"),
maternal_CMV_IgM_status = c("negative", "negative", "positive",
"negative", "positive", "negative",
"positive", "positive", "positive",
"negative"),
TB40E_conc_CB = c(1.954727, NA, 1.992956,
1.831331, 1.905936, 2.053446,
2.055809, 1.739377, 2.052576,
1.961838),
AD169r_conc_CB = c(5.86714, 6.469020, 9.387268,
5.733174, 6.480673, 5.176167,
7.548077, 7.209173, 4.944089,
9.667219),
TB40E_conc_MOM = c(7.389400, 5.917861, 7.022016,
8.017846, 10.046830, 7.503896,
6.427719, 9.498801, 7.351678,
6.050478),
AD169r_conc_MOM = c(7.011906, 6.506734, 9.986478,
5.673412, 3.825439, 5.795331,
7.082124, 6.810222, 5.54213,
8.271366)
)
在一些帮助下,我编写了代码来遍历所有 161 个向量,并使用 lapply 生成具有 p 值和测试类型的新数据框:
# Pull actual names of variables, not just numbers
excluded_vars <- toydata %>%
select(., c(CCBB_dyad_number,
cCMV_status,
maternal_CMV_IgM_status)) %>%
names(.)
var_list <- toydata %>%
select(., -any_of(excluded_vars)) %>%
names(.)
out = lapply(var_list, function(v){
#cat(paste0("Wilcox: ", v, "\n")) #Loop message for checking
fmla <- formula(paste(v, " ~ cCMV_status"))
wilcox.test(fmla, data = toydata, paired = FALSE) %>%
purrr::flatten() %>% #Unnest/convert to plain list
as.data.frame(stringsAsFactors=FALSE) %>% #Set as data frame
mutate(Variable = v) %>% #add new variable column (could also get it from data.name)
select(Variable, W.statistic=W, P.value=p.value, Method=method) %>%
mutate(P.value=scientific(P.value, digits=2, format="e"))
}) %>% #%T>% { names(out) <- var_list } %>% #Didn't actually need this, but could if wanted a named list
purrr::compact() %>% #Remove any empty data frames/list elements (NULL)
dplyr::bind_rows() #Bind list of data frames into single data frame
out$FDR_P.value <- p.adjust(out$P.value, method="fdr", n=length(out$P.value)) %>%
scientific(., digits = 2, format = "e")
col_order <- c("Variable", "W.statistic", "P.value", # Reorder columns for tabling
"FDR_P.value", "Method")
out <- out[, col_order]
kable(out, "html", booktabs = T) %>%
kable_styling(latex_options = c("striped", "scale_down")) # Print output as a nice table
但是,我在思考如何编写代码以通过多个不同的向量对循环签名等级测试时遇到了麻烦。我想我会提取向量(或只是向量名称?),如下所示:
toy_cCMV_pos <- toydata %>%
filter(cCMV_status == 'cCMV+') %>%
select(., -any_of(excluded_vars))
variable.set1 <- toy_cCMV_pos %>%
select(., ends_with("_MOM"))
variable.set2 <- toy_cCMV_pos %>%
select(., ends_with("_CB"))
有人建议像这样循环遍历向量。但是,我不断收到“未定义的列已选择”错误,因为我不太了解下面的代码在做什么,所以我无法排除故障。
for (a in variable.set1) {
groups = unique(toy_cCMV_pos[,a])
for (b in variable.set2) {
wilcox.test(x=toy_cCMV_pos[which(toy_cCMV_pos[a]==groups[1]),b],
y=toy_cCMV_pos[which(toy_cCMV_pos[a]==groups[2]),b],
paired=TRUE)
}
}
# Keep getting error "undefined columns selected"
我希望能够将结果(包括 p 值)提取到一个新的数据框中,就像秩和测试一样。
谁能帮我想想如何运行这些配对测试?
【问题讨论】:
标签: r statistical-test