\name{marginalLikelihood}
\alias{marginalLikelihood}
\title{ Marginal (or integrated) likelihood density of the observed data
  for an individual model handled by modelSelection (regression, GLM,
  GAM, accelerated failure time, regression with Normal, two-piece
  Normal, Laplace or two-piece Laplace residuals }
\description{
  The marginal density of the data, i.e. the likelihood
  integrated with respect to the prior distribution on the
  regression coefficients of the variables included in the model.
}
\usage{

marginalLikelihood(sel, y, x, data, smoothterms, nknots=9, groups=1:ncol(x),
family="normal", priorCoef, priorGroup,
priorVar=igprior(alpha=0.01,lambda=0.01), priorSkew=momprior(tau=0.348),
neighbours, 
phi, method='auto', adj.overdisp='intercept', hess='asymp', optimMethod,
optim_maxit, initpar='none', B=10^5, logscale=TRUE, XtX, ytX)

}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{sel}{Vector with indexes of columns in x to be included in the model.
  Ignored if \code{y} is a formula}
\item{y}{Either a formula with the regression equation or a vector with
  observed responses. The response can be either continuous or of class
  \code{Surv} (survival outcome). If \code{y} is a formula then \code{x},
  \code{groups} and \code{constraints} are automatically created}
\item{x}{Design matrix with linear covariates for which we want to
  assess if they have a linear effect on the response. Ignored if
  \code{y} is a formula}
\item{data}{If \code{y} is a formula then \code{data} should be a data
  frame containing the variables in the model}
\item{smoothterms}{Formula for non-linear covariates (cubic splines),
  modelSelection assesses if the variable has no effect, linear or
  non-linear effect. \code{smoothterms} can also be a design matrix or
  data.frame containing linear terms, for each column modelSelection
  creates a spline basis and tests no/linear/non-linear effects}
\item{nknots}{Number of spline knots. For cubic splines the non-linear
  basis adds knots-4 coefficients for each linear term, we recommend
  setting \code{nknots} to a small/moderate value}
\item{groups}{If variables in \code{x} such be added/dropped in groups,
  \code{groups} indicates the group that each variable corresponds to
  (by default each variable goes in a separate group)}
\item{family}{Parametric distribution family. Possible values include
  'normal','laplace', 'binomial', 'poisson', see function 
\code{modelSelection} for the full list}
\item{priorCoef}{Prior on coefficients, created
  by \code{momprior}, \code{imomprior}, \code{emomprior} or
  \code{zellnerprior}.
  Prior dispersion is on coefficients/sqrt(scale) for Normal and
  two-piece Normal, and on coefficients/sqrt(2*scale) for Laplace
  and two-piece Laplace.}
\item{priorGroup}{Prior on grouped coefficients (e.g. categorical
  predictors with >2 categories, splines). Created by
  \code{groupmomprior}, \code{groupemomprior},
  \code{groupimomprior} or \code{groupzellnerprior}}
\item{priorVar}{Inverse gamma prior on scale parameter, created by
  \code{igprior(). For Normal variance=scale, for Laplace variance=2*scale.}}
\item{priorSkew}{Either a number fixing tanh(alpha) where alpha is the
  asymmetry parameter or a prior on residual skewness parameter,
  assumed to be of
  the same family as priorCoef. Ignored if \code{family} is 'normal' or
  'laplace'.}
\item{neighbours}{Only used if priorCoef is an icarplus prior.
  neighbours is a list with the same length as the design matrix.
  Its entry j should be a vector indicating the neighbours of j,
  and have 0 length if j has no neighbours.}
\item{method}{Method to approximate the integral. See
  \code{help(modelSelection)}.}
\item{adj.overdisp}{Only used for method=='ALA'. Over-dispersion adjustment for models with fixed
  dispersion parameter such as logistic and Poisson regression}
\item{hess}{Method to estimat the hessian in the Laplace approximation to the integrated
  likelihood under Laplace or asymmetric Laplace errors. When
  hess=='asymp' the asymptotic hessian is used, hess=='asympDiagAdj' a
  diagonal adjustment is applied (see Rossell and Rubio for details).}
\item{optimMethod}{Algorithm to maximize objective function when
  method=='Laplace'. Leave unspecified or set optimMethod=='auto' for an
  automatic choice. optimMethod=='LMA' uses modified
  Newton-Raphson algorithm, 'CDA' coordinate descent algorithm}
\item{optim_maxit}{Maximum number of iterations when method=='Laplace'}
\item{initpar}{Initial regression parameter values when finding the
  posterior mode to approximate the integrated likelihood. See
  help(modelSelection)}
\item{B}{Number of Monte Carlo samples to use (ignored unless
  \code{method=='MC'})}
\item{logscale}{If \code{logscale==TRUE} the log marginal density is returned.}
\item{XtX}{Optionally, specify the matrix X'X.
  Useful when the function must be called a large number of times.}
\item{ytX}{Optionally, specify the vector y'X.
  Useful when the function must be called a large number of times.}
\item{phi}{If the disperson parameter (e.g. error variance) is known, it
  can be specified here. Leave blank unless you know what you're doing}
}
\details{
  The marginal density of the data y under a given model is

  p(y | model) int p(y | theta) d P(theta | model)

  where P(theta | model) is the prior distribution on the parameters
  included by the model.   

}
\value{
  Marginal (or integrated) likelihood of the data under the specified prior.
}
\author{ David Rossell }
\seealso{ \code{\link{modelSelection}} to perform model selection based
  on product non-local priors. }
\examples{
x <- matrix(rnorm(100*2),ncol=2)
y <- x \%*\% matrix(c(.5,1),ncol=1) + rnorm(nrow(x))

#Marginal likelihood for 2 models under pMOM prior
marginalLikelihood(c(TRUE,FALSE), y=y, x=x, priorCoef=momprior())
marginalLikelihood(c(TRUE, TRUE), y=y, x=x, priorCoef=momprior())

#Same, under Normal prior with diagonal covariance
marginalLikelihood(c(TRUE,FALSE), y=y, x=x, priorCoef=normalidprior())
marginalLikelihood(c(TRUE, TRUE), y=y, x=x, priorCoef=normalidprior())

}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ models }
\keyword{ htest }

