无法将数据适应到metaGAM

huangapple go评论85阅读模式
英文:

Cannot fit data into metaGAM

问题

我正在尝试使用此vignette中描述的方法通过metaGAM运行模拟数据,但每次尝试将GAM模型传递给metagam时,都会出现奇怪的错误。下面是模拟数据和拟合结果:

#### 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

我运行了models以检查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 

修改样条项以显式包括基础项(例如,s(x, bs = "cr) for CR splines)似乎没有帮助。我该如何修复这个问题?

英文:

I'm trying to run simulated data through a metaGAM using the method described in this vignette, but I keep getting a weird error every time I try to pass the GAM models into metagam. Below is the simulated data and fits:

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

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

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

#### Assign to Dataframe ####
df &lt;- data.frame(x,y1,y2,y3) %&gt;% 
  as_tibble()
df

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

#### Combine ####
models &lt;- list(cohort1 = fit1, 
               cohort2 = fit2,
               cohort3 = fit3)

When I try running it with the main command:

#### Fit into MetaGAM ####
metafit &lt;- metagam(models, 
                   terms = &quot;s(x)&quot;) 

It just gives me this error, which I assume means it can't find either the spline term or the data in my models:

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

I ran models to check if s(x) was somehow not included, but the output seems to indicate it is coded correctly:

$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 

Modifying the splines to explicitly include basis terms (e.g. s(x, bs = &quot;cr) for CR splines) does not seem to help. How do I fix this?

答案1

得分: 2

我明白了。由于某种原因,这个包要求您在通过metagam运行GAM之前始终使用strip_rawdata函数。同时,当拟合每个GAM时,似乎不能使用默认的样条,所以我将基础函数更改为bs="cr"。通过使用以下代码,立即解决了问题:

拟合数据

fit1 <- gam(y1 ~ s(x, bs = "cr"), data = df)
fit2 <- gam(y2 ~ s(x, bs = "cr"), data = df)
fit3 <- gam(y3 ~ s(x, bs = "cr"), data = df) # 更改每个基础函数

合并

models <- list(cohort1 = strip_rawdata(fit1),
cohort2 = strip_rawdata(fit2),
cohort3 = strip_rawdata(fit3)) # 去除数据

拟合和绘图

fit <- metagam(models)
plot(fit) # 绘制拟合

这将给我想要的结果:

无法将数据适应到metaGAM

英文:

I figured it out. For some reason this package requires that you always use the strip_rawdata function before running GAMs through metagam. It also appears you cannot use the default splines when fitting each GAM, so I changed the basis to bs=&quot;cr&quot;. This immediately fixed the problem by using this code:

#### Fit Data ####
fit1 &lt;- gam(y1 ~ s(x, bs = &quot;cr&quot;), data = df)
fit2 &lt;- gam(y2 ~ s(x, bs = &quot;cr&quot;), data = df)
fit3 &lt;- gam(y3 ~ s(x, bs = &quot;cr&quot;), data = df) # changes each basis

#### Combine ####
models &lt;- list(cohort1 = strip_rawdata(fit1), 
               cohort2 = strip_rawdata(fit2),
               cohort3 = strip_rawdata(fit3)) # strips data

#### Fit and Plot ####
fit &lt;- metagam(models)
plot(fit) # plots fit

Which gives me what I want:

无法将数据适应到metaGAM

答案2

得分: 1

"iterms" 类型的元模型默认预测类型不支持使用平滑器:

请注意,不支持类型为 "terms",因为它可能导致平滑项的估计标准差为零。

(来自 ?metagam

因此,您需要指定另一种预测类型("response" 或 "link"),例如:

metafit <- metagam(models, 
                   terms = "s(x)",
                   type = "response"
                   ) 

但这可能不是您想要的结果?

英文:

The use of smoothers for the metagams default prediction type "iterms" is not supported:

> Note that type="terms" is not supported, since it may result in estimated zero
> standard deviation for smooth terms.

(from ?metagam)

So you'd need to specify another prediction type ("response" or "link"), e. g.:

metafit &lt;- metagam(models, 
                   terms = &quot;s(x)&quot;,
                   type = &quot;response&quot;
                   ) 

However, that might not be what you're after?

huangapple
  • 本文由 发表于 2023年6月22日 13:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定