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

使用metaGAM整合GAM模型报错:Unknown term requested求助

问题:使用metagam时出现"Unknown term requested"错误

问题描述

尝试使用metaGAM对模拟数据进行元分析,但将拟合好的GAM模型传入metagam()函数时,始终返回错误:Error in FUN(X[[i]], ...) : Unknown term requested。即使显式指定样条基函数(如s(x, bs = "cr")),问题依然存在。

模拟数据与拟合代码

#### Libraries ####
library(tidyverse)
library(mgcv)
library(metagam)
set.seed(1)

#### Sim Data ####
n <- 100
x <- seq(0, 1, length.out = n)
fx1 <- sin(2 * pi * x)
fx2 <- sin(3 * pi * x)
fx3 <- sin(2.4 * pi * x)
y1 <- fx1 + rnorm(n, sd = 0.5)
y2 <- fx2 + rnorm(n, sd = .3)
y3 <- fx3 + rnorm(n, sd = .4)

#### Plot ####
par(mfrow=c(1,3))
plot(x, y1, main = "Simulated Data 1")  
lines(x, fx1, lwd = 2) 
plot(x, y2, main = "Simulated Data 2")
lines(x, fx2, lwd = 2) 
plot(x, y3, main = "Simulated Data 3") 
lines(x, fx3, lwd = 2) 

#### Assign to Dataframe ####
df <- data.frame(x,y1,y2,y3) %>% 
  as_tibble()
df

#### Fit Data ####
fit1 <- gam(y1 ~ s(x), data = df)
fit2 <- gam(y2 ~ s(x), data = df)
fit3 <- gam(y3 ~ s(x), data = df)

#### Combine ####
models <- list(cohort1 = fit1, 
               cohort2 = fit2,
               cohort3 = fit3)

错误重现

执行核心命令时触发错误:

#### Fit into MetaGAM ####
metafit <- metagam(models, 
                   terms = "s(x)") 

错误信息:

Error in FUN(X[[i]], ...) : Unknown term  requested

模型确认

查看模型列表,确认每个模型均包含s(x)项:

$cohort1

Family: gaussian 
Link function: identity 

Formula:
y1 ~ s(x)

Estimated degrees of freedom:
4.77  total = 5.77 

GCV score: 0.2317925     

$cohort2

Family: gaussian 
Link function: identity 

Formula:
y2 ~ s(x)

Estimated degrees of freedom:
6.64  total = 7.64 

GCV score: 0.1065703     

$cohort3

Family: gaussian 
Link function: identity 

Formula:
y3 ~ s(x)

Estimated degrees of freedom:
5.37  total = 6.37 

GCV score: 0.1722314 

解决方案

错误核心原因是所有模型共享同一个包含多个响应变量的数据框,且响应变量名称不同,导致metagam解析模型项时出现识别混乱。以下两种方法均可修复:

方法1:为每个模型创建独立数据框(推荐)

构造仅包含对应预测变量和响应变量的数据框,统一响应变量名称(如都命名为y):

# 创建独立数据框
df1 <- data.frame(x = x, y = y1)
df2 <- data.frame(x = x, y = y2)
df3 <- data.frame(x = x, y = y3)

# 拟合GAM模型
fit1 <- gam(y ~ s(x), data = df1)
fit2 <- gam(y ~ s(x), data = df2)
fit3 <- gam(y ~ s(x), data = df3)

# 组合模型列表
models <- list(cohort1 = fit1, cohort2 = fit2, cohort3 = fit3)

# 运行metagam
metafit <- metagam(models, terms = "s(x)")

方法2:为每个模型指定子集数据

无需重新构造数据框,拟合时仅选择模型需要的列:

# 拟合GAM模型,指定子集数据
fit1 <- gam(y1 ~ s(x), data = df %>% select(x, y1))
fit2 <- gam(y2 ~ s(x), data = df %>% select(x, y2))
fit3 <- gam(y3 ~ s(x), data = df %>% select(x, y3))

# 组合模型列表
models <- list(cohort1 = fit1, cohort2 = fit2, cohort3 = fit3)

# 运行metagam
metafit <- metagam(models, terms = "s(x)")

修改后,metagam可正确识别所有模型中的s(x)项,完成元分析。


内容的提问来源于stack exchange,提问作者Shawn Hemelstrand

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 06:15:19