You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言Wilcoxon符号秩检验无输出问题求助

问题:两组tbl_graph物种关联权重的Wilcoxon检验无输出解决方法

问题背景

需要检验两组基于亲和矩阵和简单比率指数计算的物种间成对关联权重是否存在差异,使用两个tbl_graph格式数据集,但运行指定代码后未得到可读取的输出。

数据集

数据集1

dat1 <- structure(list(5, FALSE, c(1, 3, 4, 2, 3, 4, 3, 4, 4), c(0, 0, 
0, 1, 1, 1, 2, 2, 3), c(0, 3, 1, 4, 6, 2, 5, 7, 8), c(0, 1, 2, 
3, 4, 5, 6, 7, 8), c(0, 0, 1, 2, 5, 9), c(0, 3, 6, 8, 9, 9), 
    list(c(1, 0, 1), structure(list(), names = character(0)), 
        list(name = c("BABO", "BW", "MANG", "RC", "SKS"), n_obs = c(219L, 
        1377L, 197L, 1881L, 1232L), n_grps = c(34L, 535L, 61L, 
        665L, 339L)), list(weight = c(0.227890554864407, 0.291851490123247, 
        0.222986891666136, 0.273019105913787, 0.270047304490458, 
        0.308713136488867, 0.31127336565632, 0.313224653422152, 
        0.270579464114338))), <environment>), class = c("tbl_graph", 
"igraph"), active = "nodes")

数据集2

dat2 <- structure(list(5, FALSE, c(4, 3, 3, 4, 4), c(0, 1, 2, 2, 3), 
    c(1, 2, 0, 3, 4), c(0, 1, 2, 3, 4), c(0, 0, 0, 0, 2, 5), 
    c(0, 1, 2, 4, 5, 5), list(c(1, 0, 1), structure(list(), names = character(0)), 
        list(name = c("BABO", "BW", "MANG", "RC", "SKS"), n_obs = c(23L, 
        8L, 117L, 29L, 668L), n_grps = c(2L, 6L, 21L, 10L, 25L
        )), list(weight = c(0.282369461120595, 0.321658527239868, 
        0.307638016777122, 0.322400613641658, 0.342550960146532
        ))), <environment>), class = c("tbl_graph", "igraph"), active = "nodes")

尝试的代码

pairwise.wilcox.test(dat1$weight, dat2$weight) -> wilcox.test

问题分析与解决方法

核心问题

  1. 函数参数使用错误:pairwise.wilcox.test是用于多组样本的两两比较,要求第一个参数是观测值向量,第二个参数是分组向量(对观测值进行分组)。传入两个独立向量不符合函数逻辑,导致无有效输出。
  2. tbl_graph数据提取不规范:直接用dat1$weight提取权重可能因active设置出错,更稳妥的方式是用tidygraph工具明确提取边数据。

正确解决步骤

步骤1:规范提取权重向量

library(tidygraph)

# 提取dat1的边权重
weights1 <- dat1 %>% activate(edges) %>% pull(weight)
# 提取dat2的边权重
weights2 <- dat2 %>% activate(edges) %>% pull(weight)

步骤2:选择对应检验方法

情况1:独立样本比较

两组无配对关系,使用独立样本Wilcoxon秩和检验:

# 独立样本Wilcoxon检验
wilcox_result <- wilcox.test(weights1, weights2)
# 查看结果
print(wilcox_result)
情况2:配对样本比较

若为同一物种对在两个数据集的权重,需先对齐配对关系再做检验:

# 提取边的物种对信息
edges1 <- dat1 %>% activate(edges) %>% as_tibble() %>% 
  mutate(from_name = dat1 %>% activate(nodes) %>% pull(name)[from],
         to_name = dat1 %>% activate(nodes) %>% pull(name)[to]) %>%
  mutate(pair = paste(pmin(from_name, to_name), pmax(from_name, to_name), sep = "-"))

edges2 <- dat2 %>% activate(edges) %>% as_tibble() %>% 
  mutate(from_name = dat2 %>% activate(nodes) %>% pull(name)[from],
         to_name = dat2 %>% activate(nodes) %>% pull(name)[to]) %>%
  mutate(pair = paste(pmin(from_name, to_name), pmax(from_name, to_name), sep = "-"))

# 合并得到配对权重
paired_weights <- inner_join(edges1 %>% select(pair, weight1 = weight),
                             edges2 %>% select(pair, weight2 = weight),
                             by = "pair")

# 配对Wilcoxon检验
paired_wilcox_result <- wilcox.test(paired_weights$weight1, paired_weights$weight2, paired = TRUE)
print(paired_wilcox_result)

结果说明

运行后会输出检验统计量、p值等关键信息,用于判断两组权重是否存在显著差异。


内容的提问来源于stack exchange,提问作者Marnee Roundtree

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 22:34:56