R/methods.R

Defines functions logLik.GPvam nobs.GPvam residuals.GPvam fitted.GPvam vcov.GPvam coef.GPvam

Documented in coef.GPvam fitted.GPvam logLik.GPvam nobs.GPvam residuals.GPvam vcov.GPvam

#' Extract components of a fitted GPvam model
#'
#' Standard model methods expose the unrounded estimates used by the fitting
#' algorithm. The displayed \code{parameters} table is rounded for presentation.
#'
#' @param object A fitted \code{GPvam} object.
#' @param type For \code{fitted}, \code{"conditional"} includes the predicted
#'   random effects and \code{"marginal"} uses only the fixed effects. For
#'   \code{residuals}, these choices subtract the corresponding fitted values;
#'   \code{"scaled"} returns the conditional residuals transformed by a
#'   Cholesky factor of the inverse residual covariance.
#' @param ... Further arguments, currently unused.
#' @return \code{coef} returns a named fixed-effect coefficient vector;
#'   \code{vcov} returns its estimated covariance matrix. \code{fitted} and
#'   \code{residuals} return named numeric vectors in the order of the input rows
#'   with observed responses. Rows with missing responses are omitted.
#'   \code{nobs} returns the number of observed responses. \code{logLik} returns
#'   an object of class \code{logLik}, with degrees of freedom equal to the
#'   number of fixed-effect and covariance/persistence parameters.
#' @details
#' \code{logLik} returns the likelihood criterion actually fitted (ML or REML).
#' It does not refit or convert between likelihood types. The parameter-count
#' convention agrees with common R mixed-model packages. Compare REML likelihoods
#' or information criteria only for models with the same fixed-effect design and
#' the same observations; ML and REML criteria are not directly comparable.
#'
#' These methods apply to objects fitted by GPvam 3.3-0 or later, which retain
#' full-precision matrices and an input-row map for all model settings.
#' @examples
#' \donttest{
#' data(vam_data)
#' fit <- GPvam(vam_data, persistence = "CP", verbose = FALSE)
#' coef(fit)
#' vcov(fit)
#' head(fitted(fit))
#' head(residuals(fit, type = "marginal"))
#' logLik(fit)
#' nobs(fit)
#' }
#' @name GPvam-methods
NULL

#' @rdname GPvam-methods
#' @export
coef.GPvam <- function(object, ...) {
    stats::setNames(as.numeric(object$ybetas), colnames(object$X))
}

#' @rdname GPvam-methods
#' @export
vcov.GPvam <- function(object, ...) {
    out <- as.matrix(object$betacov)
    dimnames(out) <- list(colnames(object$X), colnames(object$X))
    out
}

#' @rdname GPvam-methods
#' @export
fitted.GPvam <- function(object, type = c("conditional", "marginal"), ...) {
    type <- match.arg(type)
    values <- if (type == "conditional") object$yhat else object$yhat.m
    index <- order(object$observation)
    stats::setNames(as.numeric(values)[index], rownames(object$X)[index])
}

#' @rdname GPvam-methods
#' @export
residuals.GPvam <- function(object, type = c("conditional", "marginal", "scaled"), ...) {
    type <- match.arg(type)
    values <- switch(type, conditional = object$cresid,
                     marginal = object$mresid, scaled = object$sresid)
    index <- order(object$observation)
    stats::setNames(as.numeric(values)[index], rownames(object$X)[index])
}

#' @rdname GPvam-methods
#' @export
nobs.GPvam <- function(object, ...) object$num.obs

#' @rdname GPvam-methods
#' @export
logLik.GPvam <- function(object, ...) {
    structure(as.numeric(object$loglik), df = nrow(object$parameters),
              nobs = object$num.obs, class = "logLik")
}

Try the GPvam package in your browser

Any scripts or data that you put into this service are public.

GPvam documentation built on Oct. 23, 2026, 1:06 a.m.