RStudio中meta包留一法分析森林图绘制报错求助
留一法Meta分析森林图绘制报错:
Error in round(x, digits) : non-numerical argument to mathematical function 问题场景
首次使用RStudio的meta包开展HR的Meta分析:
- 主分析(
metagen函数)正常完成,森林图可正常生成 - 留一法分析(
metainf函数)计算结果正常,但调用forest(l1o_hfh)绘制森林图时触发上述报错
主分析代码与输出
hfh.m <- metagen(TE = hr, upper = upper, lower = lower, n.e = n.e, n.c = n.c, data=Question, studlab=author, method.tau="REML", sm="HR", transf = F) hfh.m # 输出结果: Number of studies: k = 7 Number of observations: o = 26400 (o.e = 7454, o.c = 18946) HR 95%-CI z p-value Common effect model 0.5875 [0.4822; 0.7158] -5.28 < 0.0001 Random effects model 0.5656 [0.4471; 0.7154] -4.75 < 0.0001 Quantifying heterogeneity (with 95%-CIs): tau^2 = 0.0161 [0.0000; 0.2755]; tau = 0.1270 [0.0000; 0.5249] I^2 = 0.0% [0.0%; 70.8%]; H = 1.00 [1.00; 1.85] Test of heterogeneity: Q d.f. p-value 5.54 6 0.4769 Details of meta-analysis methods: - Inverse variance method - Restricted maximum-likelihood estimator for tau^2 - Q-Profile method for confidence interval of tau^2 and tau - Calculation of I^2 based on Q
留一法分析代码与输出
l1o_hfh <- metainf(hfh.m, pooled="random") l1o_hfh # 输出结果: Leave-one-out meta-analysis HR 95%-CI p-value tau^2 tau I^2 Omitting 1 0.5610 [0.4389; 0.7170] < 0.0001 0.0198 0.1407 9.7% Omitting 2 0.6167 [0.4992; 0.7618] < 0.0001 0 0 0% Omitting 3 0.5186 [0.3747; 0.7177] < 0.0001 0.0450 0.2121 6.4% Omitting 4 0.5670 [0.4418; 0.7276] < 0.0001 0.0197 0.1405 7.3% Omitting 5 0.5058 [0.3834; 0.6673] < 0.0001 0.0058 0.0760 0% Omitting 6 0.5780 [0.4532; 0.7371] < 0.0001 0.0155 0.1244 0.7% Omitting 7 0.6054 [0.4932; 0.7432] < 0.0001 0.0010 0.0310 0% Random effects model 0.5656 [0.4471; 0.7154] < 0.0001 0.0161 0.1270 0% Details of meta-analysis methods: - Inverse variance method - Restricted maximum-likelihood estimator for tau^2 - Calculation of I^2 based on Q
报错信息
forest(l1o_hfh) # 报错: Error in round(x, digits) : non-numerical argument to mathematical function
使用的数据集
| author | hr | lower | upper |
|---|---|---|---|
| 1 | 0.6 | 0.14 | 2.49 |
| 2 | 0.42 | 0.24 | 0.73 |
| 3 | 0.633 | 0.43 | 0.931 |
| 4 | 0.49 | 0.19 | 1.28 |
| 5 | 0.7 | 0.52 | 0.94 |
| 6 | 0.442478 | 0.19685 | 0.990099 |
| 7 | 0.3 | 0.12 | 0.76 |
解决方案
方案1:转换author列为字符型
报错核心原因之一是author列是数值型,metainf生成的对象中研究标签(studlab)为数值,导致forest函数处理时出现类型冲突。修改代码如下:
# 先将author列转为字符型 Question$author <- as.character(Question$author) # 重新运行主分析与留一法分析 hfh.m <- metagen(TE = hr, upper = upper, lower = lower, n.e = n.e, n.c = n.c, data=Question, studlab=author, method.tau="REML", sm="HR", transf = F) l1o_hfh <- metainf(hfh.m, pooled="random") # 再绘制森林图 forest(l1o_hfh)
方案2:修复metainf对象中的I^2字段
metainf输出的I^2是带百分号的字符型,部分版本的forest.metainf函数会尝试对其做数值运算导致报错。手动转换为数值:
# 将I^2列的百分号去除并转为数值 l1o_hfh$I2 <- as.numeric(sub("%", "", l1o_hfh$I2)) # 绘制森林图 forest(l1o_hfh)
方案3:更新meta包
若上述方法无效,可能是旧版本meta包的bug,更新到最新版本:
install.packages("meta") library(meta)
内容的提问来源于stack exchange,提问作者Rafael Miranda
相关产品推荐
相关产品推荐

