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

如何对lapply生成的列表数据执行正态性检验与t检验?

问题解决:从列表提取数值向量执行t检验与正态性检验

报错原因

shapiro.test()和t.test()仅接受数值型向量作为输入,但你通过lapply()生成的boot_degree2是列表结构(每个元素对应一个置换图的7个节点度值),直接传入函数会因数据类型不匹配触发报错。

解决方案

先从列表的每个元素中提取第1个节点的度值,转换为数值向量后再执行统计检验:

修正后的统计代码段

# 提取每个置换结果中第1个节点的度值,转换为数值向量
boot_degree_first <- sapply(boot_degree2, function(x) x[1])

# 正态性检验
shapiro.test(boot_degree_first)

# t检验
t.test(boot_degree_first, mu = 0)

代码说明

  • sapply(boot_degree2, function(x) x[1]):遍历boot_degree2列表的每个元素,提取每个元素的第1个值,并自动整理为数值向量。若需要更严格的类型控制,可替换为vapply(boot_degree2, function(x) x[1], numeric(1))。
  • 转换后的boot_degree_first完全符合检验函数对输入数据类型的要求,可正常执行分析。

完整修正代码

library(igraph)
vetor <- c(1, 5, 3, 8, 2, 9, 3, 2:15, 1, 5, 3, 6, 5, 9, 3 )
matrixnet<-matrix(vetor, 7, 7)
matcor <- cor(matrixnet, method = "spearman")

matrixnetwork = graph.adjacency(matcor, mode="undirected", weighted = TRUE, add.colnames=NULL, diag=FALSE)
plot(matrixnetwork)

##Boot
N <- 7L
set.seed(2023)
R <- 100L
BIPERMUT<- vector("list", length = R)
for(i in seq.int(R)) {
  indices <- sample(N, replace = TRUE)
  BIPERMUT[[i]] <- permute(matrixnetwork, sample(vcount(matrixnetwork)))  
}

plot (BIPERMUT[[1]])
degree(BIPERMUT[[1]], normalized = FALSE, loops = FALSE)
#degree analyse
boot_degree2 <- lapply(BIPERMUT, degree, normalized = FALSE, loops = FALSE)

# 修正后的统计检验代码
boot_degree_first <- sapply(boot_degree2, function(x) x[1])
shapiro.test(boot_degree_first)
t.test(boot_degree_first, mu = 0)

内容的提问来源于stack exchange,提问作者Ana Paula Castro

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 23:01:34