# NOT RUN {
## examples not run due to time constraints on CRAN checks.
## full examples below should take about 2-4 minutes.
set.seed(91179)
n <- 400
d <- data.frame(f1 = sample(gl(3, n/3)), f2 = sample(gl(4,
n/4)), x1 = runif(n), x2 = runif(n), x3 = runif(n))
# true model:
# - interaction between f1 and x1
# - smooth interaction between x1 and x2,
# - x3 and f2 are noise variables without influence on y
nf1 <- as.numeric(d$f1)
d$f <- with(d, 5 * (nf1 + 2 * sin(4 * pi * (x1 - 0.2) *
(x2 - 0.7)) - nf1 * x1))
d$y <- with(d, scale(f + rnorm(n)))
d$yp <- with(d, rpois(n, exp(f/10)))
# fit & display the model:
m1 <- spikeSlabGAM(y ~ x1 * f1 + f1 * f2 + x3 * f1 +
x1 * x2, data = d)
summary(m1)
# visualize estimates:
plot(m1)
plot(m1, cumulative = FALSE)
(plotTerm("fct(f1):fct(f2)", m1, aggregate = median))
print(p <- plotTerm("sm(x1):sm(x2)", m1, quantiles = c(0.25,
0.75), cumulative = FALSE))
# change MCMC settings and priors:
mcmc <- list(nChains = 3, burnin = 100, chainLength = 1000,
thin = 3, reduceRet = TRUE)
hyper <- list(gamma = c(v0 = 5e-04), tau2 = c(10,
30), w = c(2, 1))
# complicated formula example, poisson response:
m2 <- spikeSlabGAM(yp ~ x1 * (x2 + f1) + (x2 + x3 + f2)^2 -
sm(x2):sm(x3), data = d,
family = "poisson", mcmc = mcmc,
hyperparameters = hyper)
summary(m2)
plot(m2)
# quick&dirty convergence diagnostics:
print(b <- ssGAM2Bugs(m2))
plot(b)
# }
Run the code above in your browser using DataLab