R/testing.R

Defines functions lrt_approx

Documented in lrt_approx

#' Approximate likelihood ratio testing for Latent Variables Multivariate
#'    Mixed-type Response Regression
#'
#' @param fit_null The output from mmrr() fit under the null hypothesis
#' @param fit_full The output from mmrr() fit under the alternative hypothesis
#' @param df The degrees of freedom; if not supplied, set to the sum of the
#'    difference in number of predictors and number of restrictions on Sigma.
#' @param ... Other arguments passed to mmrr() when re-fitting the full model
#'   holding W fixed at fit_null$W.
#' @return A list with the test statistic, p-value, and degrees of freedom.
#' @export
lrt_approx <- function(fit_null, fit_full, df, ...)
{
  # Unconstrained fit with null W
  fit_new <- mmrr(Y = fit_full$Y, X = fit_full$X, type = fit_full$type,
                   psi = fit_full$psi, M = fit_full$M, maxit = c(10, 500, 500, 0),
                   Beta = fit_full$Beta, Sigma = fit_full$Sigma,
                   W = fit_null$W, ...)
  if(missing(df)){
    df <- ncol(fit_full$X) - ncol(fit_null$X)
    df <- df + sum(is.na(fit_full$M[upper.tri(fit_full$M, diag = TRUE)]))
    df <- df - sum(is.na(fit_null$M[upper.tri(fit_null$M, diag = TRUE)]))
  }
  test_stat <- 2 * (fit_null$obj - fit_new$obj)
  return(list(stat = test_stat,
              p = stats::pchisq(test_stat, df = df, lower.tail = FALSE),
              df = df))
}
koekvall/lvmmr documentation built on Dec. 13, 2021, 2:35 p.m.