Reputation: 3562
Is there a convenient way to extract the data from a gam plot, without actually plotting the gam object?
Here's a dummy example. plot.data
has the data I want in it, but I don't want the plot window to be affected.
library(mgcv)
x=1:10000/1000
y = sin(x)+rnorm(10000,sd=2)
m = gam(y~s(x))
plot.data<-plot(m,plot=F)
Upvotes: 2
Views: 1006
Reputation: 32426
It doesn't look like plot.gam
has the option for not plotting. But you could try
plot.data <- {
dev.new()
res <- plot(m)
dev.off()
res
}
or possibly
plot.data <- {
pdf(NULL)
res <- plot(m)
invisible(dev.off())
res
}
Upvotes: 6
Reputation: 5335
If you're using gam()
from the package gam
, then you can get those data in list form by calling preplot(m)
. Here's what that looks like for your data:
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"
The x
and y
components of that list are what I think you're after. Presumably, if you've got more than one smoothing term in your model, the ith component of the preplot
list will correspond to the ith smoothing term in your original call to gam()
.
Upvotes: 1