Title: | Bayesian Variable Selection and Model Choice for Generalized Additive Mixed Models |
---|---|
Description: | Bayesian variable selection, model choice, and regularized estimation for (spatial) generalized additive mixed regression models via stochastic search variable selection with spike-and-slab priors. |
Authors: | Fabian Scheipl [aut, cre], Bettina Gruen [ctb] |
Maintainer: | Fabian Scheipl <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1-20 |
Built: | 2024-11-21 05:51:33 UTC |
Source: | https://github.com/fabian-s/spikeslabgam |
Get summaries of the posterior (predictive) distribution of the linear predictor of a model term
evalTerm( label, model, newdata = NULL, aggregate = mean, quantiles = c(0.1, 0.9), returnData = TRUE )
evalTerm( label, model, newdata = NULL, aggregate = mean, quantiles = c(0.1, 0.9), returnData = TRUE )
label |
(character) the label of one of the terms in |
model |
a |
newdata |
|
aggregate |
(function) a summary statistic that is applied over the
mcmc-samples of the linear predictor. Defaults to |
quantiles |
(numeric) a vector of quantiles for borders of credible regions of the linear predictor. Defaults to 10 and 90 percent quantiles, i.e. a (point-wise) 80 percent credible region. |
returnData |
should the relevant original variables be included in the
returned |
A data.frame
that contains the relevant variables from newdata
(if returnData
is TRUE), the aggregate
-summary and the
requested quantiles
.
Fabian Scheipl
Generate design for a factor covariate
fct(x, contr = "contr.sum")
fct(x, contr = "contr.sum")
x |
a covariate that can be treated as a factor |
contr |
(character) name of the |
design matrix for a factor
Fabian Scheipl
Get the posterior distribution of the linear predictor of a model term
getPosteriorTerm(label = NULL, model, betaInd = NULL)
getPosteriorTerm(label = NULL, model, betaInd = NULL)
label |
(character) one of the terms in |
model |
a |
betaInd |
(optional) the column indices of the part of the design matrix for which the linear predictor is to be evaluated. |
a matrix containing the samples of the linear predictor associated
with label
; with attribute 'coef'
that contains the posterior
samples of the associated coefficients.
Fabian Scheipl
Generate orthogonal polynomial base for a numeric covariate without intercept
lin(x, order = 1)
lin(x, order = 1)
x |
covariate |
order |
order of the polynomial, defaults to 1 |
an orthogonal basis for a centered polynomial function in x
Fabian Scheipl
The returned design is (a low-rank approximation to) the matrix square root
of the implied covariance of the centered MRF. The function stops if
'islands', i.e. regions without any neighbors are found. Regions without
observations have to be removed from the neighborhood matrix and there is
currently no predict
-functionality for regions without observations in
the original data.
mrf(x, N, decomposition = c("ortho", "MM"), tol = 1e-10, rankZ = 0.995)
mrf(x, N, decomposition = c("ortho", "MM"), tol = 1e-10, rankZ = 0.995)
x |
a factor: which observation belongs to which region |
N |
the neighborhood (adjacency) matrix: a symmetric matrix with one
column/row for every level of |
decomposition |
use a (truncated) spectral decomposition of the implied
prior covariance of |
tol |
count singular/eigenvalues smaller than this as zero |
rankZ |
how many eigenvectors to retain from the eigen decomposition: either a number > 3 or the proportion of the sum of eigenvalues the retained eigenvectors must represent at least. Defaults to .999. |
a transformed design matrix for the Markov Random Field
Fabian Scheipl
Fahrmeir, L., Lang, S. (2001) Bayesian inference for generalized additive mixed models based on Markov random field priors. Applied Statistics, 50(2):201–220.
This function plots the estimated linear predictors of the terms in a model on
a grid of values. By default displays all 3-way, 2-way interactions and main
effects present in the model. Starting with ggplot-0.9.2 these are no longer
aligned by their axes due to internal changes in grid and ggplot2. Uses
gridExtra's marrangeGrob
to arrange the plots
for the terms, also over multiple pages if necessary. This means the graphics
device type is temporarily set to the value of interactive.dev
in
interactive use in RStudio if necessary since the RStudioGD
graphical
device does not support opening multiple pages.
## S3 method for class 'spikeSlabGAM' plot( x, labels = NULL, cumulative = TRUE, commonEtaScale = FALSE, aggregate = mean, quantiles = c(0.1, 0.9), gridlength = 20, base_size = 12, ggElems = list(), nrow = min(ceiling(sqrt(length(plotList))), 3), ncol = min(ceiling(length(plotList)/nrow), 3), interactive.dev = c("x11", "quartz", "windows"), ... )
## S3 method for class 'spikeSlabGAM' plot( x, labels = NULL, cumulative = TRUE, commonEtaScale = FALSE, aggregate = mean, quantiles = c(0.1, 0.9), gridlength = 20, base_size = 12, ggElems = list(), nrow = min(ceiling(sqrt(length(plotList))), 3), ncol = min(ceiling(length(plotList)/nrow), 3), interactive.dev = c("x11", "quartz", "windows"), ... )
x |
a fitted |
labels |
a character vector of names of model terms to be plotted |
cumulative |
Defaults to TRUE, in which case all lower order terms that are involved in an interaction are cumulated and then plotted (e.g, if a model contains 2 smooth effects and their interaction, ONLY the sum of the marginal smooth and linear terms and all their interactions are plotted.) If FALSE, a separate plot for every term in the model is produced. |
commonEtaScale |
use the same limits for all vertical axes of the different panels? Defaults to FALSE. Can be useful to compare effect sizes more easily between panels, but tends to mess up the scales. |
aggregate |
(function) which summary statistic to use for the posterior of the model terms. Defaults to the mean. |
quantiles |
which quantiles to use for the borders of credible regions. Defaults to 10% and 90% percentiles. Set to NULL to omit plotting credible regions. |
gridlength |
length of the (univariate) grids for the covariates on which to evaluate the posterior. Defaults to 20. |
base_size |
default base font size for plot (see e.g.
|
ggElems |
a list of plot elements to give to |
nrow |
number of rows per page, defaults to min(sqrt(no. of plots), 3).
See |
ncol |
number of columns per page, defaults to min((no. of plots)/nrow,
3). See |
interactive.dev |
alternative device to use in interactive mode in RStudio if output needs to be spread on multiple pages, since the RStudio graphical device does not support opening multiple displays. |
... |
arguments passed to |
a list of ggplot
-objects (invisible)
Note that cumulative = TRUE
will only find all relevant terms to
accumulate if, for all numeric covariates that have a smooth term, the
smooth term is specified after the linear term in the formula.
Fabian Scheipl
plotTerm
for more details on the specific plots
#see ?spikeSlabGAM
#see ?spikeSlabGAM
Plots the estimated linear predictor for a model term, i.e a main effect or a
(2- or 3-way) interaction. Plots for the joint effect of two numerical
covariates show an overlay if quantiles were specified: Regions where the
pointwise credible intervals do not contain zero are plotted in muted red
() and blue (
), overlaid by coloured contour lines that show
the
aggregate
values. Contour lines are shown only inside the convex
hull of the original observations. Plots for
srf
:lin
terms show the spatially varying
coefficient, i.e. the contour lines represent the change in the linear
predictor when the lin
-covariate increases by 1 standard deviation.
For this reason, a cumulative plot makes no sense and the routine will set
cumulative = FALSE
with a warning.
plotTerm( label, m, cumulative = TRUE, aggregate = mean, quantiles = c(0.1, 0.9), gridlength = 40, contours = 30, ggElems = list() )
plotTerm( label, m, cumulative = TRUE, aggregate = mean, quantiles = c(0.1, 0.9), gridlength = 40, contours = 30, ggElems = list() )
label |
(character) the label of the effect to be plotted. |
m |
a fitted |
cumulative |
Defaults to TRUE, in which case the lower order terms that
are associated with the covariates involved in |
aggregate |
(function) which summary statistic to use for the posterior of the effect. Defaults to the mean. |
quantiles |
which quantiles to use for the borders of credible regions. Defaults to 10 regions. Cannot deal with more than two quantiles. |
gridlength |
length of the (univariate) grids on which to evaluate the posterior. |
contours |
use how many contour lines for the joint effect of two numerical covariates. Defaults to 30. |
ggElems |
a list of plot elements to give to
|
Limitations: Plots for 4-way (or higher) interactions are not implemented.
Requesting such a plot will return the NULL object with a warning. Plots for
mrf
just treat the grouping variable as a conventional factor
i.e. will not incorporate any neighborhood or map information.
an object of class ggplot
. Use print
or wrap the call
to plotTerm
in parentheses to directly render on a graphic device.
Fabian Scheipl
#see help for spikeSlabGAM
#see help for spikeSlabGAM
Obtain posterior predictive/credible intervals from a spike-and-slab model
## S3 method for class 'spikeSlabGAM' predict( object, newdata = NULL, type = c("response", "link", "terms"), terms = NULL, aggregate = mean, quantiles = NULL, addIntercept = is.null(terms), ... )
## S3 method for class 'spikeSlabGAM' predict( object, newdata = NULL, type = c("response", "link", "terms"), terms = NULL, aggregate = mean, quantiles = NULL, addIntercept = is.null(terms), ... )
object |
a |
newdata |
an optional |
type |
the type of prediction required. The default is on the scale of
response, the alternative |
terms |
an optional character vector of term labels or variable names for which to return fits/predictions/credible regions. If variable names instead of term labels are supplied, the function returns predictions/estimates for all terms associated with these variables, i.e. their main effects (usually both linear and smooth for numeric covariates) and all interaction terms they are involved in. |
aggregate |
(function) the summary statistic of the posterior predictive
of the linear predictor. Defaults to |
quantiles |
(numeric) an optional vector of quantiles for borders of credible regions of the returned values. Defaults to NULL. |
addIntercept |
include global intercept term in prediction/estimate?
Defaults to TRUE if |
... |
arguments passed from or to other methods (not used) |
If type ="terms"
, a list of data.frame
s containing the
requested pointwise summary statistics for the supplied terms (use e.g.
Reduce("+", ...)
to get row-wise sums of the list-entries).
Otherwise, a data.frame
containing the requested pointwise summary
statistics of the posterior predictive of the linear predictor
(type ="link"
) or the conditional expectation of the response
(type ="response"
) is returned.
Fabian Scheipl
spikeSlabGAM
fitThe model table shows at least the 10 and at most the 20 most probable models as found by SSVS, or enough models to account for 90% of the probability mass in the model space as found by SSVS by default.
## S3 method for class 'summary.spikeSlabGAM' print( x, digits = 3, printPGamma = TRUE, printModels = TRUE, showModels = NULL, ... )
## S3 method for class 'summary.spikeSlabGAM' print( x, digits = 3, printPGamma = TRUE, printModels = TRUE, showModels = NULL, ... )
x |
an object of class |
digits |
see |
printPGamma |
print marginal inclusion probabilities and norm of the linear predictor for each term? Defaults to TRUE |
printModels |
print most probable models visited by the SSVS? Defaults to TRUE |
showModels |
how many of th most probable models to display. See details for default. |
... |
arguments based from or to other methods (not used) |
invisibly returns x
Fabian Scheipl
Generate design for a random intercept
rnd(x, C = NULL)
rnd(x, C = NULL)
x |
a covariate that can be treated as a factor |
C |
an optional known correlation structure of the random effects associated with x |
design matrix for a random intercept for x
Fabian Scheipl
The returned matrix is a low-rank approximation of the original P-spline
basis (unless decomposition = "asIs"
), that is projected into the
complement of the nullspace of the associated penalty (unless
centerBase = FALSE
), i.e. for the default second order difference
penalty, the resulting basis cannot reproduce linear or constant functions
and parameterizes the "wiggly" part of the influence of x
only. This
means that it very rarely makes sense to run a model with sm(x)
without also using lin(x)
or u(x)
. The projection
improves the separability between the linear and smooth parts of the
influence of x
and centers the resulting function estimates s.t
.
sm( x, K = min(length(unique(x)), 20), spline.degree = 3, diff.ord = 2, rankZ = 0.999, centerBase = T, centerx = x, decomposition = c("ortho", "MM", "asIs"), tol = 1e-10 )
sm( x, K = min(length(unique(x)), 20), spline.degree = 3, diff.ord = 2, rankZ = 0.999, centerBase = T, centerx = x, decomposition = c("ortho", "MM", "asIs"), tol = 1e-10 )
x |
covariate |
K |
number of basis functions in the original basis (defaults to 20) |
spline.degree |
defaults to 3 for cubic B-plines |
diff.ord |
order of the difference penalty, defaults to 2 for penalizing deviations from linearity |
rankZ |
how many eigenvectors to retain from the eigen decomposition: either a number > 3 or the proportion of the sum of eigenvalues the retained eigenvectors must represent at least. Defaults to .999. |
centerBase |
project the basis of the penalized part into the complement of the column space of the basis of the unpenalized part? defaults to TRUE |
centerx |
vector of x-values used for centering (defaults to |
decomposition |
use a truncated spectral decomposition of the implied
prior covariance of |
tol |
count eigenvalues smaller than this as zero |
a basis for a smooth function in x
Fabian Scheipl
Kneib, T. (2006). Mixed model based inference in structured additive regression. Dr. Hut. https://edoc.ub.uni-muenchen.de/archive/00005011/
This function sets up a spike-and-slab model for variable selection and model
choice in generalized additive models and samples its posterior. It uses a
blockwise Metropolis-within-Gibbs sampler and the redundant multiplicative
parameter expansion described in the reference. This routine is not meant to
be called directly by the user – spikeSlabGAM
provides a
formula-based interface for specifying models and takes care of (most of) the
housekeeping. Sampling of the chains is done in parallel using package
parallel
. A "SOCK" cluster is set up under Windows to do so (and
closed after computations are done, I try to clean up after myself), see
makeCluster
etc. Use options(mc.cores =<foo>)
to set the (maximal) number of processes forked by the parallelization. If
options()$mc.cores
is unspecified, it is set to 2.
spikeAndSlab( y, X, family = c("gaussian", "binomial", "poisson"), hyperparameters = list(), model = list(), mcmc = list(), start = list() )
spikeAndSlab( y, X, family = c("gaussian", "binomial", "poisson"), hyperparameters = list(), model = list(), mcmc = list(), start = list() )
y |
response |
X |
design matrix |
family |
(character) the family of the response, defaults to normal/Gaussian response |
hyperparameters |
a list of hyperparameters controlling the priors (see details) |
model |
a list with information about the grouping structure of the model (see details) |
mcmc |
(optional) list setting arguments for the sampler (see details) |
start |
(optional) list containing the starting values for |
Details for model specification:
hyperparameters
w
hyperparameters for the -prior for
;
defaults to
c(alphaW = 1, betaW = 1)
, i.e. a uniform distribution.
tau2
hyperparameters for the -prior of the
hypervariances
; defaults to
c(a1 = 5, a2 = 25)
gamma
sets , the ratio between the spike and slab
variances, defaults to
c(v0 = 0.00025)
sigma2
hyperparameters for -prior for error
variance; defaults to
c(b1 = 1e-4, b2 = 1e-4)
. Only relevant for Gaussian
response.
varKsi
variance for prior of , defaults to
1
ksiDF
defaults to 0 for a gaussian prior for , else
induces a t-prior for
with ksiDF
degrees of freedom.
model
groupIndicators
a factor that maps the columns of X to the different model terms
H
a matrix containing the hierarchy of the penalized model terms
n
number of observations
q
length of
scale
scale/weights of
the response, defaults to rep(1, n)
, use this to specify number of
trials for binomial data
offset
defaults to rep(0,
n)
mcmc
nChains
how many parallel chains to run: defaults to 3
chainLength
how many samples should be generated per chain, defaults to 500
burnin
how many initial iterations should be discarded, defaults to 100
thin
save only every thin
-th
iteration, defaults to 5
verbose
verbose output and report progress? defaults to TRUE
returnSamples
defaults to TRUE
sampleY
generate samples of y and its conditional expectation from posterior predictive? defaults to FALSE
useRandomStart
use random draw or ridge estimate for beta as starting value? defaults to TRUE, i.e. random starting values.
blocksize
approx. blocksizes of the updates for . Defaults to 50 for gaussian responses and 5/15 for non-gaussian
responses.
scalemode
how to do term-wise rescaling of
subvectors of in each iteration: 0 means no rescaling, 1 means
rescaling s.t. each mean
, 2 means rescaling s.t. each
max
modeSwitching
probability to do P-IWLS
with the mode of the proposal set to the current value, which is useful if
the chain gets stuck. Defaults to . Increase this if acceptance rates
are too low.
reduceRet
don't return data and samples for
? defaults to FALSE
start
beta
starting
values for . Defaults to a modified approximate ridge-penalized ML
estimate. See vignette for details on default specification.
gamma
starting values for . Defaults to a vector of
1's if
mcmc$useRandomStart
is FALSE
, otherwise drawn from the
prior.
tau2
starting values for . Defaults to the
mode of the prior if
mcmc$useRandomStart
is FALSE
, otherwise
drawn from the prior.
sigma2
starting values for
. Only relevant for Gaussian response. Defaults to the variance
of the response divided by the number of covariates if
mcmc$useRandomStart
is FALSE
, otherwise drawn from the prior.
w
starting value for . Defaults to the mean of the prior
if
mcmc$useRandomStart
is FALSE
, otherwise drawn from the
prior.
seed
Sets RNG seed for reproducible results. Parallel chains are seeded with this seed incremented by the number of the chain.
a list with components:
formula
see arguments
data
see arguments
family
see arguments
y
see arguments
X
see arguments
hyperparameters
see arguments
model
see arguments
mcmc
see arguments
start
see arguments
posteriorPred
a list with entries mu
and
y
containing samples of the expected values and realizations of the
response from the posterior predictive
postMeans
a list containing the posterior means of the parameters:
beta
the regression coefficients
alpha
ksi
tau
hypervariances of the penalized model terms
gamma
inclusion indicator variables of the model terms
pV1
w
hyperparameter
for gamma
sigma2
error variance (for Gaussian data)
logLik
log likelihood
logPost
log of (unnormalized) posterior
samples
a list containing the posterior samples of the parameters, see above for explanation of the entries
DIC
a vector with .
Usually doesn't make much sense for this kind of model because of the
posterior's multimodality.
fitted
a matrix with the posterior mean of the linear predictor in the first column and the posterior mean of the expected response in the second.
runTime
of the sampler, in seconds
Fabian Scheipl, Daniel Sabanes Bove
Scheipl, F. (2010) Normal-Mixture-of-Inverse-Gamma Priors for Bayesian Regularization and Model Selection in Structured Additive Regression Models. LMU Munich, Department of Statistics: Technical Reports, No.84 (https://epub.ub.uni-muenchen.de/11785/)
This function fits structured additive regression models with spike-and-slab
priors via MCMC. The spike-and-slab prior results in an SSVS-like term
selection that can be used to aid model choice, e.g. to decide between linear
and smooth modelling of numeric covariates or which interaction effects are
relevant. Model terms can be factors (fct
), linear/polynomial
terms (lin
), uni- or bivariate splines (sm
,
srf
), random intercepts (rnd
) or Markov random
fields (mrf
) and their interactions, i.e. an interaction
between two smooth terms yields an effect surface, an interaction between a
linear term and a random intercept yields random slopes, an interaction
between a linear term and a smooth term yields a varying coefficient term
etc.
spikeSlabGAM( formula, data, ..., family = "gaussian", hyperparameters = list(), model = list(), mcmc = list(), start = list() )
spikeSlabGAM( formula, data, ..., family = "gaussian", hyperparameters = list(), model = list(), mcmc = list(), start = list() )
formula |
the model formula (see details below and
|
data |
the data containing the variables in the function |
... |
additional arguments for |
family |
(character) the family of the response, defaults to
normal/Gaussian response, |
hyperparameters |
A list of arguments specifying the prior settings. See
|
model |
A list of arguments describing the model structure. See
|
mcmc |
A list of arguments specifying MCMC sampler options. See
|
start |
A list of starting values for the MCMC sampler. See
|
Implemented types of terms:
u
unpenalized model terms associated with a flat prior (no selection)
lin
linear/polynomial terms
fct
factors
sm
univariate smooth functions
rnd
random intercepts
mrf
Markov random fields
srf
surface estimation
Terms in the formula that are
not in the list of specials (i.e. the list of term types above) are
automatically assigned an appropriate special wrapper, i.e. numerical
covariates x
are treated as lin(x) + sm(x)
,
factors f
(and numerical covariates with very few distinct values, see
ssGAMDesign
) are treated as fct(f)
. Valid model
formulas have to satisfy the following conditions:
All
variables that are involved in interactions have to be present as main
effects as well, i.e. y ~ x1 + x1:x2
will produce an error because
the main effect of x2
is missing.
Interactions between
unpenalized terms not under selection (i.e. terms of type u
)
and penalized terms are not allowed, i.e. y ~ u(x1)* x2
will produce an
error.
If main effects are specified as special terms, the interactions
involving them have to be given as special terms as well, i.e. y ~
lin(x1) + lin(x2) + x1:x2
will produce an error.
The default prior settings for Gaussian data work best for a response with unit variance. If your data is scaled very differently, either rescale the response (recommended) or adjust the hyperparameters accordingly.
Sampling of the chains is done in parallel using package multicore
if
available. If not, a socket cluster set up with snow
is used where
available. Use options(mc.cores = foo)
to set the (maximal) number of
processes spawned by the parallelization. If options()$mc.cores
is
unspecified, snow uses 8.
an object of class spikeSlabGAM
with methods
summary.spikeSlabGAM
, predict.spikeSlabGAM
, and
plot.spikeSlabGAM
.
Fabian Scheipl
Fabian Scheipl (2011). spikeSlabGAM
: Bayesian Variable
Selection, Model Choice and Regularization for Generalized Additive Mixed
Models in R. Journal of Statistical Software, 43(14), 1–24.
Fabian Scheipl, Ludwig Fahrmeir, Thomas Kneib (2012). Spike-and-Slab Priors for Function Selection in Structured Additive Regression Models. Journal of the American Statistical Association, 107(500), 1518–1532.
ssGAMDesign
for details on model specification,
spikeAndSlab
for more details on the MCMC sampler and prior
specification, and ssGAM2Bugs
for MCMC diagnostics. Check out
the vignette for theoretical background and code examples.
## 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) ## End(Not run)
## 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) ## End(Not run)
This function generates the design for a 2-D penalized spline using (almost)
radial basis functions. Use this type of term to account for spatial
variation. Smooth interactions between covariates are often better fitted via
the interactions of lin
and sm
terms, because
they allow a decomposition into (linear and smooth) marginal trends and
(linear-linear, linear-smooth/"varying coefficients", and smooth-smooth)
interactions. This decomposition usually makes no sense for spatial data.
srf( coords, K = min(50, sum(nd)/4), rankZ = 0.999, centerBase = TRUE, baseType = c("B", "thinPlate"), decomposition = c("ortho", "MM", "asIs"), tol = 1e-10 )
srf( coords, K = min(50, sum(nd)/4), rankZ = 0.999, centerBase = TRUE, baseType = c("B", "thinPlate"), decomposition = c("ortho", "MM", "asIs"), tol = 1e-10 )
coords |
a |
K |
(approximate) number of basis functions in the original basis
(defaults to 50). If |
rankZ |
how many eigenvectors to retain from the eigen decomposition: either a number > 3 or the proportion of the sum of eigenvalues the retained eigenvectors must represent at least. Defaults to .999. |
centerBase |
project the basis of the penalized part into the complement of the column space of the basis of the unpenalized part? defaults to TRUE |
baseType |
Defaults to |
decomposition |
use a (truncated) spectral decomposition of the implied
prior covariance of |
tol |
count eigenvalues smaller than this as zero |
Note that srf()
expects coords
to be a data.frame
within
the larger data.frame
supplied to spikeSlabGAM
in its
data
argument, i.e. coords
is considered a two-dimensional
covariate.
If baseType
is 'thinPlate'
, knot locations for the thin plate
spline basis are chosen via a space-filling algorithm (i.e. medoids returned
by clara
) as suggested in Ruppert/Wand/Carroll, ch.
13.5. Since the thin plate penalty term penalizes deviations from a linear
trend, it is recommended to add marginal linear trends and their interaction
to the model if baseType ="thinPlate"
to improve the fit.
Note that predictions outside the convex hull (sometimes even just close its border) of the original data tend to become rather unstable very quickly.
a design matrix for the 2-D spline.
Fabian Scheipl
Ruppert, D., Wand, M.P., Carroll, R.J. (2003). Semiparametric Regression. Cambridge University Press
spikeSlabGAM
into a
bugs
-objectUse plot
and print
on the returned
bugs
object for convergence diagnostics. Lack of
convergence for or
does not necessarily mean that the
sampler has not converged for
.
ssGAM2Bugs(m, rm = c("alpha", "ksi", "gamma"))
ssGAM2Bugs(m, rm = c("alpha", "ksi", "gamma"))
m |
a model fitted with |
rm |
which parameter blocks to omit from the conversion. Defaults to
omission of |
a bugs
object which has convenience
functions for assessing convergence based on parallel MCMC runs
Fabian Scheipl
#see help for spikeSlabGAM
#see help for spikeSlabGAM
spikeSlabGAM
This function generates the design matrix for a generalized additive (mixed)
model, based on smoothing spline ANOVA-like orthogonal decomposition of the
model terms and their interactions. It parses the formula given to
spikeSlabGAM
to provide all the arguments necessary for the
MCMC sampler.
ssGAMDesign( formula, data, specials = c("u", "lin", "fct", "sm", "rnd", "mrf", "srf"), minUniqueValues = 6, lowRankInteractions = TRUE, orthogonalizeInteractions = TRUE, decomposition = NULL )
ssGAMDesign( formula, data, specials = c("u", "lin", "fct", "sm", "rnd", "mrf", "srf"), minUniqueValues = 6, lowRankInteractions = TRUE, orthogonalizeInteractions = TRUE, decomposition = NULL )
formula |
the model formula. Follows the usual R syntax described in
|
data |
|
specials |
a vector of the types of possible model terms. These must be
implemented as functions generating a design matrix with a label attribute.
See also |
minUniqueValues |
the minimal number of unique values a covariate has to have in order to not be treated as a factor. Defaults to 6. |
lowRankInteractions |
should a low-rank approximation of the design matrix for interaction terms based on a (truncated) spectral decomposition of the implied covariance matrix be used? defaults to TRUE. |
orthogonalizeInteractions |
should the design matrices for interaction terms be projected into the complement of the column space of the respective main effects? Can help separate marginal and interaction effects. Defaults to TRUE. |
decomposition |
which decomposition to use, see |
Setting lowRankInteractions
to FALSE can result in very large models,
especially if higher-order interactions or interactions between terms with
lots of parameters are involved. Note that numeric covariates with fewer
unique values than minUniqueValues
are treated as factors unless
wrapped in a special argument.
This function is not meant to be called directly by the user,
spikeSlabGAM
is the user interface.
a list with components:
response |
the left hand side of the model formula |
Design |
the design matrix of the model specified in formula |
groupIndicators |
a factor that maps the columns of
|
H |
a matrix containing the hierarchy of the penalization groups (not used, included for backwards compatibility) |
Fabian Scheipl
spikeSlabGAM
fitReturns basic information about prior and model structure, posterior means of inclusion probabilities, term importance and the most probable models found by the SSVS.
## S3 method for class 'spikeSlabGAM' summary(object, threshold = 0.5, ...)
## S3 method for class 'spikeSlabGAM' summary(object, threshold = 0.5, ...)
object |
an object of class |
threshold |
threshold for inclusion of a model term. Defaults to 0.5. |
... |
arguments passed from or to other methods (not used) |
Two scalar summaries of term importance are included:
P(gamma = 1)
The posterior mean of , i.e. the
marginal posterior inclusion probability of the term.
pi
The
scaled dot products of the posterior mean of the linear predictor
associated with the
-th term and the total linear
predictor
:
. They sum to
one (but can be negative as well), and (for mutually orthogonal
)
provide a percentage decomposition of the sum of squares of
. See
references for details.
The summary also shows the dimensionality of the basis associated with a term.
The top row in the model table shows the relative frequency of the respective model, the bottom row shows cumulative relative frequencies for the models visited most often.
an object of class summary.spikeSlabGAM
Fabian Scheipl
Gu, Chong (2002). Smoothing Spline ANOVA models. Springer. (see chapter 3.6)
Basically a wrapper for model.matrix(~ x, ...)
.
u(x, ...)
u(x, ...)
x |
covariate |
... |
arguments passed to |
a design matrix for x
Fabian Scheipl