Reputation: 701
Does anyone know how to visualize the smooth component of gam
models in R very well? I would really like to visualize something like the output of the function visreg
. This code below illustrates my problem
library(gam)
f=function(v){exp(v)}
n=100
x=runif(n)
t=runif(n)
y=x+f(t)+rnorm(n, sd=0.1)
fit=gam(y~x+s(t))
plot(t,y)
lines(t,as.numeric(fit$smooth))
#want something more like
library(visreg)
visreg(fit)
Upvotes: 0
Views: 1030
Reputation: 132706
You could use the plotting method for gam objects, but you'd have to use the data
parameter of gam
:
library(gam)
f <- function(v){exp(v)}
n <- 100
x <- runif(n)
t <- runif(n)
y <- x+f(t)+rnorm(n, sd=0.1)
DF <- data.frame(y, x, t)
fit <- gam(y~x+s(t), data = DF)
layout(t(1:2))
plot(fit, se=TRUE)
See help("plot.gam")
for other options.
Upvotes: 2