\name{loglikBIRT}
\alias{loglikBIRT}

\title{
  Calculate the log-likelihood values of each person for each posterior parameter sample}
\description{
  To facilitate model fit checking (e.g., leave one out cross-validation, WAIC), this function can be used to calculate the log-likelihood values of each person for each posterior parameter sample in a \code{BoundIRT}-object.}

\usage{
loglikBIRT(object, marginal=TRUE,nquad=15, silent=FALSE)

}
\arguments{

  \item{object}{A \code{BoundIRT}-object}
  \item{marginal}{If \code{TRUE} the marginal loglikelihood is calculated for each subject (default), otherwise the conditional likelihood is calculated for each subject-item combination. See \bold{Details}.}
  \item{nquad}{If \code{marginal=TRUE} this argument determines the number of quadrature points used to approximate the integral in the marginal likelihood.}
  \item{silent}{If TRUE no details are displayed about the progress}
}

\details{
For latent variable models like the ones in \code{BoundIRT}, Merkle et al., (2019) recommend the use of the likelihood function that is marginalized over the persons parameters (i.e., not over the item parameters) in calculating fit statistics like the WAIC and LOOIC. Using \code{marginal=TRUE} will output this marginal likelihood value for each person and each sample of the posterior. If the interest is in calculating the conventional fit measures (e.g., conditional WAIC and DIC), \code{marginal=FALSE} should be used to obtain the likelihood values for each person conditional on the person and item parameters of each posterior sample.
  }


\value{
  The function returns a matrix of dimensions (\code{nsamples}) by (\code{N}) containing the (marginal) log-likelihood values of the subjects for each posterior sample.).
}
\references{
Gabry J, Simpson D, Vehtari A, Betancourt M, Gelman A (2019). Visualization in
  Bayesian workflow. \emph{Journal of the Royal Statistical Society, A}, \bold{182}, 389-402. \doi{10.1111/rssa.12378}

Merkle EC, Furr D, Rabe-Hesketh S. (2019).Bayesian Comparison of Latent Variable Models: Conditional Versus Marginal Likelihoods. \emph{Psychometrika}, \bold{84}(3), 802-829. \doi{10.1007/s11336-019-09679-0}

}
\author{ Dylan Molenaar \email{d.molenaar@uva.nl}}

\seealso{

\code{\link{fitBIRT}} to estimate various bounded continuous IRT models to data.

\code{\link{plot.BoundIRT}} to plot the item and test information curves and predicted densities for the different models.

\code{\link{coef.BoundIRT}} to extract item parameter estimates from a \code{BoundIRT} object.

\code{\link{repBIRT}} to simulate replicated datasets using (a subset of) the posterior parameter samples. For posterior predictive model checking.

\code{\link{bridgeBIRT}} to use bridge sampling to calculate the marginal loglikelihood for a model in a \code{BoundIRT} object.

\code{\link{latregBIRT}} to conduct a latent regression of the person parameters on some observed predictor variables.

}

\examples{
 #read in the prefitted results from the Abasement data (see ?Abasement and ?out_beta)
 data(out_beta)

 # Calculate marginal log-likelihood
 \donttest{
   logL=loglikBIRT(out_beta)

   #logL can now be used for e.g.,model fit assessment using loo() from R-package loo

   #library loo should be available as it is part of rstan which is an import of BoundIRT
   if (requireNamespace("loo", quietly = TRUE)) {
     library(loo)
     loo(logL)   # gives the marginal LOOIC
     waic(logL) # gives the marginal WAIC
 }
 }
 }
\keyword{models}
\keyword{multivariate}
