有没有一种方便的方法可以从 gam 图中提取数据,而无需实际绘制 gam 对象?
这是一个虚拟示例。 plot.data
里面有我想要的数据,但我不希望绘图窗口受到影响。
library(mgcv)
x=1:10000/1000
y = sin(x)+rnorm(10000,sd=2)
m = gam(y~s(x))
plot.data<-plot(m,plot=F)
它看起来plot.gam
没有不绘图的选项。但是你可以试试
plot.data <- {
dev.new()
res <- plot(m)
dev.off()
res
}
或者可能
plot.data <- {
pdf(NULL)
res <- plot(m)
invisible(dev.off())
res
}
如果您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"
该列表的x
和y
组件是我认为您所追求的。据推测,如果您的模型中有多个平滑项,则preplot
列表的第 i 个组件将对应于您最初调用gam()
.