icamp_result <- icamp.big(comm = comm,
+ tree = tree,
+ pd.desc = pd_big$pd.file,
+ pd.spname = tree$tip.label,
+ pd.wd = pd_big$pd.wd,
+ rand = 99, # 随机化次数
+ ds = 0.2, # 系统发育距离阈值
+ bin.size.limit = 20, # bin 的最小物种数
+ nworker = 1, # Windows 下建议设为 1
+ output.wd = getwd()) # 结果输出目录
The names are re-ranked.
Now calculating max phylogenetic distance.
----------Now binning-----------------Sun May 24 10:56:11 2026
Now computing path. begin at Sun May 24 10:56:12 2026. Please wait...
Now computing path. begin at Sun May 24 10:56:13 2026. Please wait...
Now computing dist to root. begin at Sun May 24 10:56:14 2026. Please wait...
----------Now binning com and sp without omitting small bins-----------------Sun May 24 10:56:15 2026
----bMPD bin i=1 in 22 ---- Sun May 24 10:56:16 2026
Now calculating observed betaMPD. Begin at Sun May 24 10:56:16 2026. Please wait...
Now randomizing by parallel computing. Begin at Sun May 24 10:56:16 2026. Please wait...
Now fixing special cases. Begin at Sun May 24 10:56:20 2026. Please wait...
----bMPD bin i=2 in 22 ---- Sun May 24 10:56:20 2026
Now calculating observed betaMPD. Begin at Sun May 24 10:56:20 2026. Please wait...
Now randomizing by parallel computing. Begin at Sun May 24 10:56:21 2026. Please wait...
Now fixing special cases. Begin at Sun May 24 10:56:25 2026. Please wait...
All match very well.
Now calculating observed MPD. Begin at Sun May 24 10:56:26 2026. Please wait...
错误于checkForRemoteErrors(val):
one node produced an error: invalid 'size' argument
此外: 警告信息:
1: In .Internal(gc(verbose, reset, full)) :
2: 'memory.limit()' is no longer supported
3: 'memory.limit()' is no longer supported
4: 'memory.limit()' is no longer supported
5: 'memory.limit()' is no longer supported
导致 **`invalid 'size' argument`(无效的大小参数)** 的核心原因通常是:**由于某些 bin(物种组)内包含的物种数量(或样本量)太少,导致在进行随机抽样(如 `sample()` 函数)时,传入的抽样数量变为了 `0`、负数、或者是 `NA`。**
下面是导致该错误的两个最主要原因以及排查修复方法:
---
### 原因 1:某些 Bin 的物种数太少,且没有被正确忽略
看你的日志:
> `----------Now binning com and sp without omitting small bins-----------------`
这意味着 `iCAMP` 正在对所有 bin 进行计算,**没有忽略那些极小的 bin**。虽然你设置了 `bin.size.limit = 20`,但在 `iCAMP` 的逻辑中,如果严格分 bin 导致某些“无法归类”或特殊的 bin 里面物种数少于某个抽样临界值,底层代码在计算观测 MPD 或随机化时就会因为 `sample(..., size = x)` 中的 `x < 1` 而崩溃。
#### 💡 解决方法:
尝试调整 `bin.size.limit` 或者增加系统发育树的过滤。你可以把 `bin.size.limit` 稍微调大(例如 24 或 30),或者检查你的群落数据 `comm` 中是否包含大量的低丰度/稀有物种(单件/双件物种),**建议在跑 iCAMP 之前,先过滤掉在极少数样本中**出现**的稀有物种**:
```R
# 过滤掉在少于 3 个样本中**出现**的物种(根据你的数据自行调整)
comm_filtered <- comm[, colSums(comm > 0) >= 3]
# 记得同步过滤 tree
library(ape)
tree_filtered <- keep.tip(tree, colnames(comm_filtered))
```
---
### 原因 2:Windows 环境下的内存与并行节点冲突
你的警告信息出卖了环境:
> `2: 'memory.limit()' is no longer supported`
> `错误于checkForRemoteErrors(val)`
这说明你使用的是 **Windows 系统**,且 R 版本较新(R 4.2.0 之后已经废弃了 `memory.limit()`)。
虽然你显式设置了 `nworker = 1`,但 `iCAMP` 的某些子函数(例如底层调用 `parallel` 或 `snow` 包时)在 Windows 下创建单节点本地集群时,由于内存限制或变量传递失败,导致子节点传回了一个空值(`NULL` 或 `NA`),主节点在拿这个空值去作为 `size` 进行计算时引发了报错。
#### 💡 解决方法:
在运行 `icamp.big` 之前,手动在 R 中关闭所有潜在的并行冲突,并尝试清除内存:
```R
# 1. 显式关闭可能残留的并行集群
try(parallel::stopCluster(cl), silent = TRUE)
# 2. 深度清理 R 的内存垃圾
gc(full = TRUE)
# 3. 重新运行,并将 nworker 设为 0 或 1 尝试
# 有些包的逻辑中,nworker = 0 才会完全不初始化并行环境,彻底走单线程
```
---
### 🛠️ 终极排查建议(两步走)
如果上述调整后依然在 `iCAMP` 的固定位置(如 `bin i=2` 之后)报错,说明是**输入数据的问题**。请重点检查你的输入数据:
1. **检查群落与树的匹配**:确保 `colnames(comm)` 和 `tree$tip.label` **完全一致**。虽然日志显示 `The names are re-ranked.`(名字已重新排序),但如果有物种名字在树里有、在群落里没有,或者反过来,容易导致分 bin 出现空集。
```R
# 严格确保二者物种完全对齐
sp.match <- match.name(comm = comm, tree = tree)
comm <- sp.match$comm
tree <- sp.match$tree
```
2. **检查 `pd_big$pd.file` 是否完整**:报错发生在 `Now calculating observed MPD`,这是要从大距离矩阵中提取距离。请确认你的硬盘空间足够,且 `pd.wd` 目录下生成的大矩阵文件没有损坏。