diff --git a/NAMESPACE b/NAMESPACE index 64d2dad..f597931 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,11 +4,11 @@ S3method(as_draws,measrfit) S3method(as_measrfit,default) S3method(c,measrprior) S3method(fit_m2,measrdcm) +S3method(loglik_array,measrdcm) S3method(loo,measrfit) S3method(loo_compare,measrfit) S3method(measr_extract,measrdcm) S3method(predict,measrdcm) -S3method(prep_loglik_array,measrdcm) S3method(reliability,measrdcm) S3method(waic,measrfit) export("%>%") @@ -32,6 +32,7 @@ export(fit_m2) export(fit_ppmc) export(get_parameters) export(is.measrprior) +export(loglik_array) export(loo) export(loo_compare) export(measr_dcm) diff --git a/R/loo-methods.R b/R/loo-methods.R index 83ea1b5..fa8174b 100644 --- a/R/loo-methods.R +++ b/R/loo-methods.R @@ -38,7 +38,7 @@ loo.measrfit <- function(x, ..., r_eff = NA, force = FALSE) { #nolint "estimated with `method = \"mcmc\"`.")) } - log_lik_array <- prep_loglik_array(model) + log_lik_array <- loglik_array(model) loo::loo(log_lik_array, r_eff = r_eff, ...) } @@ -70,7 +70,7 @@ waic.measrfit <- function(x, ..., force = FALSE) { #nolint "estimated with `method = \"mcmc\"`.")) } - log_lik_array <- prep_loglik_array(model) + log_lik_array <- loglik_array(model) loo::waic(log_lik_array, ...) } diff --git a/R/model-evaluation.R b/R/model-evaluation.R index 32dbe8e..7cf49e4 100644 --- a/R/model-evaluation.R +++ b/R/model-evaluation.R @@ -139,7 +139,7 @@ add_criterion <- function(x, criterion = c("loo", "waic"), overwrite = FALSE, all_criteria <- c(new_criteria, redo_criteria) if (length(all_criteria) > 0) { - log_lik_array <- prep_loglik_array(model) + log_lik_array <- loglik_array(model) } if ("loo" %in% all_criteria) { diff --git a/R/utils-loo.R b/R/utils-loo.R index 1d3ca4e..0f81dc1 100644 --- a/R/utils-loo.R +++ b/R/utils-loo.R @@ -1,9 +1,21 @@ -prep_loglik_array <- function(model) { - UseMethod("prep_loglik_array") +#' Extract the log-likelihood of an estimated model +#' +#' The `loglik_array()` methods for [measrfit] objects calculates the +#' log-likelihood for an estimated model via the generated quantities +#' functionality in *Stan* and returns the draws of the `log_lik` parameter. +#' +#' @param model A [measrfit] object. +#' +#' @return A "[`draws_array`][posterior::draws_array()]" object containing the +#' log-likelihood estimates for the model. +#' @export +loglik_array <- function(model) { + UseMethod("loglik_array") } +#' @rdname loglik_array #' @export -prep_loglik_array.measrdcm <- function(model) { +loglik_array.measrdcm <- function(model) { score_data <- model$data$data clean_qmatrix <- model$data$qmatrix %>% dplyr::select(-"item_id") %>% diff --git a/man/loglik_array.Rd b/man/loglik_array.Rd new file mode 100644 index 0000000..adeca93 --- /dev/null +++ b/man/loglik_array.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-loo.R +\name{loglik_array} +\alias{loglik_array} +\alias{loglik_array.measrdcm} +\title{Extract the log-likelihood of an estimated model} +\usage{ +loglik_array(model) + +\method{loglik_array}{measrdcm}(model) +} +\arguments{ +\item{model}{A \link{measrfit} object.} +} +\value{ +A "\code{\link[posterior:draws_array]{draws_array}}" object containing the +log-likelihood estimates for the model. +} +\description{ +The \code{loglik_array()} methods for \link{measrfit} objects calculates the +log-likelihood for an estimated model via the generated quantities +functionality in \emph{Stan} and returns the draws of the \code{log_lik} parameter. +} diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index 8374181..7044f4a 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -106,6 +106,7 @@ reference: - loo_compare.measrfit - loo.measrfit - waic.measrfit + - loglik_array - subtitle: Add evaluations to model objects desc: > diff --git a/tests/testthat/test-mcmc.R b/tests/testthat/test-mcmc.R index f8cd29a..54ce82e 100644 --- a/tests/testthat/test-mcmc.R +++ b/tests/testthat/test-mcmc.R @@ -73,7 +73,7 @@ test_that("get_mcmc_draws works as expected", { test_that("log_lik is calculated correctly", { skip_on_cran() - log_lik <- prep_loglik_array(cmds_mdm_lcdm) + log_lik <- loglik_array(cmds_mdm_lcdm) # expected value from 2-class LCA fit in Mplus expect_equal(sum(apply(log_lik, c(3), mean)), -331.764, tolerance = 1.000)