我正在处理细菌焦磷酸测序数据,并使用 R 进行统计分析。我有 21 个样本和 7 种不同的处理方法。我将数据加载到 R phyloseq 中,获得:
> psR
phyloseq-class experiment-level object
otu_table() OTU Table: [ 7498 taxa and 21 samples ]
sample_data() Sample Data: [ 21 samples by 8 sample variables ]
tax_table() Taxonomy Table: [ 7498 taxa by 6 taxonomic ranks ]
phy_tree() Phylogenetic Tree: [ 7498 tips and 7497 internal nodes ]
Run Code Online (Sandbox Code Playgroud)
由于我发现处理之间(使用 adonis 函数)存在统计显着差异,因此我想知道哪些 OTU 在不同处理中具有不同的丰度。为此,我使用了 dunn.test 函数(包含 Kruskal-Wallis 测试),首先交换 OTU 表中的行和列以应用测试:
swap_otu_table <- t(otu_table(psR))
treatment <- c('A', 'A', 'A', 'B', 'B', 'B', …Run Code Online (Sandbox Code Playgroud) 假设我有一个数据框
> col1<-c(1,5,2,6,8,1,3,8,9,1,8)
> col2<-c(1,2,1,1,2,2,1,2,2,1,1)
> df<-data.frame(col1,col2)
> df
col1 col2
1 1 1
2 5 2
3 2 1
4 6 1
5 8 2
6 1 2
7 3 1
8 8 2
9 9 2
10 1 1
11 8 1
Run Code Online (Sandbox Code Playgroud)
我已经用我所拥有的数据进行了 Kruskal-Wallis 测试df
> dfKW<-kruskal.test(col1~col2, data=df)
> dfKW
Kruskal-Wallis rank sum test
data: col1 by col2
Kruskal-Wallis chi-squared = 1.695, df = 1, p-value = 0.1929
Run Code Online (Sandbox Code Playgroud)
我想做的是将 p 值提取到向量中(仅提取没有标签“p 值”的值)。我已经尝试过这个:
> dfKWx<-sapply(dfKW, '[', 'p.value')
> …Run Code Online (Sandbox Code Playgroud) 我正在比较癌症患者和健康人 DNA 中结构断裂的区域。我正在尝试对每个区域的中断次数运行 Kruskal-Wallis 测试 (SciPy Stats),以查看两个分布之间是否存在差异。我不确定 Kruskal - Wallis 的输入应该是数组(文档)还是数组列表(互联网上的其他地方)。
首先,我尝试了一个这样的样本+控制数组:
controls = ['1', '2', '3', '4', '5']
samples = ['10', '20', '30', '40', '50']
n=0
for item in controls:
array_item = np.array([item, samples[n]])
kw_test = stats.mstats.kruskalwallis(array_item)
print(kw_test)
n+=1
Run Code Online (Sandbox Code Playgroud)
这给了我所有项目的以下输出:
(0.0, nan)
Run Code Online (Sandbox Code Playgroud)
我还尝试转换数组中的各个数据点,然后运行 KW 测试。
controls = ['1', '2', '3', '4', '5']
samples = ['10', '20', '30', '40', '50']
n=0
kw_results = []
for item in controls:
array_controls = np.array([item])
array_samples = np.array([samples[n]])
kw_test = stats.mstats.kruskalwallis(array_samples, array_controls)
kw_results.append(kw_test)
n+=1 …Run Code Online (Sandbox Code Playgroud) 考虑一个Data具有多个因子和多个数值连续变量的数据集。这些变量中的一些,比方说slice_by_1(类别为“男性”、“女性”)和slice_by_2(类别为“悲伤”、“中性”、“快乐”)用于将数据“切片”为子集。对于每个子集,Kruskal-Wallis 测试都应该在变量length,上运行preasure,pulse每个变量都由另一个称为 的因子变量分组compare_by。R 中是否有一种快速方法来完成此任务并将计算出的 p 值放入矩阵?
我使用dplyr包来准备数据。
示例数据集:
library(dplyr)
set.seed(123)
Data <- tbl_df(
data.frame(
slice_by_1 = as.factor(rep(c("Male", "Female"), times = 120)),
slice_by_2 = as.factor(rep(c("Happy", "Neutral", "Sad"), each = 80)),
compare_by = as.factor(rep(c("blue", "green", "brown"), times = 80)),
length = c(sample(1:10, 120, replace=T), sample(5:12, 120, replace=T)),
pulse = runif(240, 60, 120),
preasure = c(rnorm(80,1,2),rnorm(80,1,2.1),rnorm(80,1,3))
)
) %>%
group_by(slice_by_1, slice_by_2)
Run Code Online (Sandbox Code Playgroud)
我们来看数据:
Source: local data frame [240 …Run Code Online (Sandbox Code Playgroud) 如何更改下图中的字体大小stat_compare_means?即,更改“ Kruskal-Wallis,p = 1.5e-09”和其他p值字体大小?我想使用比默认字体小的字体大小...
按照数据示例...
library(ggpubr)
data("ToothGrowth")
compare_means(len ~ dose, data = ToothGrowth)
# Visualize: Specify the comparisons I want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
# Plotting
ggboxplot(ToothGrowth, x = "dose", y = "len",
color = "dose", palette = "jco")+
stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 50) # Add global p-value
Run Code Online (Sandbox Code Playgroud)
情节:
我想使用一个分组变量对 data.frame 中的某些数值变量运行 KW 测试。我更喜欢在循环中执行此操作,而不是输入所有测试,因为它们有很多变量(比下面的示例中的变量更多)。
模拟数据:
library(dplyr)
set.seed(123)
Data <- tbl_df(
data.frame(
muttype = as.factor(rep(c("missense", "frameshift", "nonsense"), each = 80)),
ados.tsc = runif(240, 0, 10),
ados.sa = runif(240, 0, 10),
ados.rrb = runif(240, 0, 10))
) %>%
group_by(muttype)
ados.sim <- as.data.frame(Data)
Run Code Online (Sandbox Code Playgroud)
下面的代码在循环之外工作得很好。
kruskal.test(formula (paste((colnames(ados.sim)[2]), "~ muttype")), data =
ados.sim)
Run Code Online (Sandbox Code Playgroud)
但它不在循环内:
for(i in names(ados.sim[,2:4])){
ados.mtp <- kruskal.test(formula (paste((colnames(ados.sim)[i]), "~ muttype")),
data = ados.sim)
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
terms.formula(formula, data = data) 中的错误:模型公式中的项无效
有谁知道如何解决这个问题吗?
我有如下数据集
df = pd.DataFrame({'numbers':range(9), 'group':['a', 'b', 'c']*3})
group numbers
0 a 0
1 b 1
2 c 2
3 a 3
4 b 4
5 c 5
6 a 6
7 b 7
8 c 8
Run Code Online (Sandbox Code Playgroud)
我想创建向量
a = [0, 3, 6]
b = [1, 4, 7]
c = [2, 5, 8]
Run Code Online (Sandbox Code Playgroud)
用于 Kruskal-Wallis H 测试 python
stats.kruskal(a, b, c)
Run Code Online (Sandbox Code Playgroud)
或者可能是 R 中的类似物(数字〜组)
我有一个很长的数据集(N=499)。我从中比较了指数在 8 种不同处理中的表现(每种处理中的样本数量不同)。
我已经做了 Kruskal-Wallis 并且它很重要(p 值 < 2.2e-16)。
现在,对于事后测试,我正在考虑使用 Dunn 测试,但我读过 Wilcoxon 也可能有用,有什么建议吗?
非常感谢大家。
我有一个 27 列的数据框(26 列是数字变量,第 27 列告诉我每行与哪个组关联)。总共有 7 个组,我试图将 Kruskal-Wallis 检验应用于每个变量,按组划分,以确定是否存在显着差异。
我试过了:
df.groupby(['treatment']).apply(kruskal)
Run Code Online (Sandbox Code Playgroud)
这会引发错误“在 stats.kruskal() 中需要至少 2 组两组”。
我的其他尝试也没有产生输出。我将定期使用更大的数据集进行类似的分析。有人可以帮助我理解这个问题以及如何解决它吗?