我正在使用 R 来做一些统计,这个问题是从统计交换中重复的,它被关闭,因为它不是真正的统计问题,所以我认为它可能与堆栈溢出( https://stats.stackexchange.com/questions/441638/how-do-i-run-only-a-subset-of-comparisons-in-a-t-test-using-r/441674#441674 )更相关。尽管这里给出的答案(对数据进行子集化,然后运行测试)在逻辑上似乎是正确的,但我看不出有什么方法可以在不对每个聚糖重复 100 段不同的代码的情况下做到这一点(见下文):
我已经从原始数据生成了一个data.frame。数据包括一个数值变量 (fold_change) 和两个因子变量(dis_status 包括 RF 和 con,以及聚糖,其中包括 100 种不同的聚糖)
这是一个可重现的示例,其中只有 3 个聚糖,每个聚糖有 3 个“RF”和 3 个“con”。
> example
dis_status glycan fold_change
1 RF a 4.83433185
2 RF a 3.88519084
3 RF a 2.80368849
4 con a 0.94730194
5 con a 1.91278688
6 con a 1.23225002
7 RF b 4.07173876
8 RF b 5.70383491
9 RF b 0.05282291
10 con b 1.34631723
11 con b 4.26723583
12 con b 4.26723583
13 RF c 2.20887813
14 RF c 4.62220094
15 RF c 0.94730194
16 con c 0.53597973
17 con c 2.92572685
18 con c 1.58871049
> dput(example)
structure(list(dis_status = structure(c(2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("con",
"RF"), class = "factor"), glycan = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("a",
"b", "c"), class = "factor"), fold_change = c(4.834331853, 3.885190842,
2.803688487, 0.947301944, 1.912786879, 1.232250023, 4.071738761,
5.703834911, 0.052822912, 1.346317234, 4.267235834, 4.267235834,
2.208878135, 4.622200944, 0.947301944, 0.535979733, 2.925726849,
1.588710491)), class = "data.frame", row.names = c(NA, -18L))
我可以对数据运行 t.test:
ad_nonpaired <- pairwise.t.test(stats_df$fold_change, stats_df$dis_status:stats_df$glycan,
paired = F,
pool.sd = F,
p.adj = "none")
接下来我将纠正多重比较,但我遇到的问题是在 dis_status 和聚糖的每种可能组合之间进行 t.tests。
我只对每个聚糖的“RF”与“con”感兴趣。因此,对于上面的三个聚糖,我只想将“RF”中的“x”与“con”中的“x”进行比较,而不是“x”与“y”之间的任何比较,但无法弄清楚如何在测试中指定这一点?
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] en_NZ.UTF-8/en_NZ.UTF-8/en_NZ.UTF-8/C/en_NZ.UTF-8/en_NZ.UTF-8
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.25 broom_0.5.2 ggrepel_0.8.1 readxl_1.3.1 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3 purrr_0.3.3
[9] readr_1.3.1 tidyr_1.0.0 tibble_2.1.3 ggplot2_3.2.1 tidyverse_1.2.1 limma_3.38.3 hexbin_1.27.3 vsn_3.50.0
[17] Biobase_2.42.0 BiocGenerics_0.28.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.2 lubridate_1.7.4 lattice_0.20-38 gtools_3.8.1 rprojroot_1.3-2 assertthat_0.2.1 zeallot_0.1.0 digest_0.6.22
[9] utf8_1.1.4 plyr_1.8.4 R6_2.4.0 cellranger_1.1.0 backports_1.1.5 evaluate_0.14 highr_0.8 httr_1.4.1
[17] pillar_1.4.2 gplots_3.0.1.1 zlibbioc_1.28.0 rlang_0.4.1 lazyeval_0.2.2 curl_4.2 rstudioapi_0.10 gdata_2.18.0
[25] preprocessCore_1.44.0 desc_1.2.0 labeling_0.3 splines_3.5.2 munsell_0.5.0 xfun_0.10 compiler_3.5.2 modelr_0.1.5
[33] pkgconfig_2.0.3 tidyselect_0.2.5 fansi_0.4.0 crayon_1.3.4 withr_2.1.2 bitops_1.0-6 grid_3.5.2 nlme_3.1-141
[41] jsonlite_1.6 gtable_0.3.0 lifecycle_0.1.0 affy_1.60.0 magrittr_1.5 scales_1.0.0 KernSmooth_2.23-16 cli_1.1.0
[49] stringi_1.4.3 affyio_1.52.0 testthat_2.2.1 xml2_1.2.2 ellipsis_0.3.0 generics_0.0.2 vctrs_0.2.0 tools_3.5.2
[57] glue_1.3.1 hms_0.5.2 pkgload_1.0.2 yaml_2.2.0 colorspace_1.4-1 BiocManager_1.30.9 caTools_1.17.1.2 rvest_0.3.4
[65] haven_2.1.1
最佳答案
您可以按聚糖拆分数据框,然后按 dis_status 组进行 t 测试,无需任何外部库:
results <- do.call("rbind", lapply(split.data.frame(df, df$glycan),
function(x) {
pairwise.t.test(x$fold_change, x$dis_status,
paired = FALSE, pool.sd = FALSE,
p.adj = "none") -> test;
as.numeric(tapply(x$fold_change, x$dis_status, mean)) -> ta;
data.frame(glycan = as.character(x$glycan[1]),
mean.con = ta[1],
mean.RF = ta[2],
pvalue = as.numeric(test$p.value));
}))
根据评论给出您想要的数据框
results
glycan mean.con mean.RF pvalue
a a 1.364113 3.841070 0.03403083
b b 3.293596 3.276132 0.99335164
c c 1.683472 2.592794 0.52325471
关于r - 如何使用 R 在 t.test 中仅运行比较的子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59619280/