justin1.618
justin1.618

Reputation: 701

Visualizing GAM models in R

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

Answers (1)

Roland
Roland

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)

resulting plot

See help("plot.gam") for other options.

Upvotes: 2

Related Questions