从 gam 图中保存数据而不实际绘制数据?

Save data from a gam plot without actually plotting the data?

有没有方便的方法从 gam 图中提取数据,而无需实际绘制 gam 对象?

这是一个虚拟示例。 plot.data里面有我想要的数据,但是我不想window剧情受到影响

library(mgcv)    
x=1:10000/1000
y = sin(x)+rnorm(10000,sd=2)
m = gam(y~s(x))
plot.data<-plot(m,plot=F)

如果您使用包 gam 中的 gam(),则可以通过调用 preplot(m) 以列表形式获取这些数据。您的数据如下所示:

library(gam)
x = 1:10000/1000
y = sin(x)+rnorm(10000,sd=2)
m = gam(y~s(x))
preplot(m)

List of 1
 $ s(x):List of 5
  ..$ x   : num [1:10000] 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01 ...
  ..$ y   : Named num [1:10000] 0.421 0.421 0.421 0.421 0.421 ...
  .. ..- attr(*, "names")= chr [1:10000] "1" "2" "3" "4" ...
  ..$ se.y: Named num [1:10000] 0.0783 0.0782 0.0781 0.0781 0.078 ...
  .. ..- attr(*, "names")= chr [1:10000] "1" "2" "3" "4" ...
  ..$ xlab: chr "x"
  ..$ ylab: chr "s(x)"
  ..- attr(*, "class")= chr "preplot.gam"
 - attr(*, "class")= chr "preplot.gam"

该列表的 xy 部分就是我认为您想要的。据推测,如果您的模型中有多个平滑项,preplot 列表的第 i 个分量将对应于您对 gam().

的原始调用中的第 i 个平滑项

plot.gam好像没有不打印的选项。不过你可以试试

plot.data <- {
    dev.new()
    res <- plot(m)
    dev.off()
    res
}

或者可能

plot.data <- {
    pdf(NULL)
    res <- plot(m)
    invisible(dev.off())
    res
}