我有一个由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值,与秩和检验一样,拉到一个新的数据框架中。
有人能帮我想一想怎么做这些配对测试吗?
发布于 2021-09-07 14:17:18
不确定这是否是您想要的,但在这里,我对每个前缀组执行CB和MOM之间的Wilcoxon测试。
library(tidyverse)
library(broom)
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))
toydata %>%
as_tibble() %>%
gather("var", "val", -1:-3) %>%
separate(var, c("marker", "conc", "type")) %>%
spread(type, val) %>%
group_by(marker) %>%
summarize(wilcox = tidy(wilcox.test(MOM, CB)))
#> # A tibble: 2 × 2
#> marker wilcox$statistic $p.value $method $alternative
#> <chr> <dbl> <dbl> <chr> <chr>
#> 1 AD169r 49 0.971 Wilcoxon rank sum exact test two.sided
#> 2 TB40E 90 0.0000217 Wilcoxon rank sum exact test two.sidedhttps://stackoverflow.com/questions/69089240
复制相似问题