R中用transitiveweights建模赋值有向ERGM遇MCMC异常求助
问题:ergm.count拟合加权有向网络时MCMC不混合且进程停滞
背景
使用R的ergm.count包对84节点、75条加权边的赋值有向网络建模,邻接矩阵来自本地文件mat.csv。运行代码时CPU负载极高,进程长时间停滞,最终终止并抛出MCMC不混合的错误。
运行代码
library(ergm.count) mat <- as.matrix(read.csv("./mat.csv",header=TRUE)[,-1]) net<-as.network(mat , directed=TRUE, matrix.type="a", ignore.eval=FALSE, names.eval="cnts" ) m1<-ergm(net~ transitiveweights("min", "max", "min"), reference=~Poisson, response="cnts", control=control.ergm(parallel=6, parallel.type="PSOCK", main.method="MCMLE") )
现象与报错信息
- 运行期间CPU核心占用拉满
- 初始CD-MCMLE阶段收敛后,MCMLE阶段停滞3小时,最终终止输出:
> source("/root/transfer_analysis/test.R", encoding = "UTF-8") Evaluating network in model. Initializing unconstrained Metropolis-Hastings proposal: ‘ergm.count:MH_DiscTNT’. Initializing model... Model initialized. Using initial method 'CD'. Fitting initial model. Starting contrastive divergence estimation via CD-MCMLE: Iteration 1 of at most 60: Convergence test P-value:1.5e-06 The log-likelihood improved by 0.1578. Iteration 2 of at most 60: Convergence test P-value:2.6e-02 The log-likelihood improved by 0.02119. Iteration 3 of at most 60: Convergence test P-value:4.6e-01 The log-likelihood improved by 0.002026. Iteration 4 of at most 60: Convergence test P-value:1e-01 The log-likelihood improved by 0.01116. Iteration 5 of at most 60: Convergence test P-value:1.4e-01 The log-likelihood improved by 0.007586. Iteration 6 of at most 60: Convergence test P-value:1e+00 Convergence detected. Stopping. The log-likelihood improved by < 0.0001. Finished CD. Starting Monte Carlo maximum likelihood estimation (MCMLE): Density guard set to 10000 from an initial count of 75 edges. Iteration 1 of at most 60 with parameter: transitiveweights.min.max.min -1.483437 Starting unconstrained MCMC... Back from unconstrained MCMC. Error in ergm.MCMLE(init, s, s.obs, control = control, verbose = verbose, : Unconstrained MCMC sampling did not mix at all. Optimization cannot continue. Additional warning message: In ergm_MCMC_sample(s, control, theta = mcmc.init, verbose = max(verbose - : Unable to reach target effective size in iterations alotted.
环境信息
- R版本:4.3.2
- ergm版本:4.5.0
解决方案建议
1. 调整MCMC控制参数
默认MCMC迭代配置可能不匹配稀疏加权网络,尝试增加燃烧期、采样间隔与样本量:
control=control.ergm( parallel=6, parallel.type="PSOCK", main.method="MCMLE", MCMC.burnin = 10000, # 延长燃烧期,让MCMC稳定 MCMC.interval = 100, # 增加采样间隔,减少自相关性 MCMC.samplesize = 5000 # 提升样本量,保证有效样本数 )
2. 更换MCMC提议机制
默认的MH_DiscTNT提议可能不适合当前稀疏网络,改用Poisson类型提议:
control=control.ergm( parallel=6, parallel.type="PSOCK", main.method="MCMLE", MCMC.prop = "ergm.count:MH_Poisson" )
3. 简化模型基础结构
先拟合包含基础边项的模型,验证流程可行性后再加入高阶项:
m1<-ergm(net~ edges + transitiveweights("min", "max", "min"), reference=~Poisson, response="cnts", control=control.ergm(parallel=6, parallel.type="PSOCK", main.method="MCMLE") )
4. 预处理网络数据
- 检查权重是否存在极端值,对权重做标准化(如缩放至0-1区间)后再建模
- 确认网络稀疏性:84节点理论最大边数为6972,实际仅75条,极端稀疏可能导致MCMC难以探索样本空间
5. 优化并行配置
过多并行核数可能导致内存竞争,尝试减少核数并确保R有足够内存分配:
control=control.ergm( parallel=3, parallel.type="PSOCK", main.method="MCMLE" )
内容的提问来源于stack exchange,提问作者b39b332d
相关产品推荐
相关产品推荐

