tests/testthat/test-convergence.R

test_that("readModels reports normal Mplus termination with a tri-state converged field", {
  model <- MplusAutomation::readModels(
    target = testthat::test_path("ex3.1.out"),
    what = "converged",
    quiet = TRUE
  )

  expect_true(is.logical(model$converged))
  expect_length(model$converged, 1L)
  expect_true(model$converged)
})

test_that("explicit conventional and Bayesian non-convergence messages take precedence", {
  ml <- MplusAutomation:::extractConvergence_1file(c(
    "WARNING: THE SAMPLE COVARIANCE MATRIX IS SINGULAR.",
    "NO CONVERGENCE.  NUMBER OF ITERATIONS EXCEEDED."
  ))
  bayes <- MplusAutomation:::extractConvergence_1file(c(
    "THE MODEL ESTIMATION TERMINATED NORMALLY",
    "THE CONVERGENCE CRITERION IS NOT SATISFIED."
  ))

  expect_false(ml$converged)
  expect_identical(ml$status, "not_converged")
  expect_identical(ml$reason, "max_iterations_exceeded")
  expect_false(bayes$converged)
  expect_identical(bayes$reason, "bayes_criterion_not_satisfied")
})

test_that("non-convergence messages are retained in errors", {
  input <- structure(list(), class = c("mplus.inp", "list"))
  attr(input, "start.line") <- 0L
  attr(input, "end.line") <- 0L
  output <- c(
    "THE CONVERGENCE CRITERION IS NOT SATISFIED.",
    "INCREASE THE MAXIMUM NUMBER OF ITERATIONS.",
    "",
    "MODEL FIT INFORMATION"
  )

  parsed <- MplusAutomation:::extractWarningsErrors_1file(output, "model.out", input)

  expect_s3_class(parsed$errors, "mplus.errors")
  expect_length(parsed$errors, 1L)
  expect_match(paste(parsed$errors[[1L]], collapse = " "), "CONVERGENCE CRITERION IS NOT SATISFIED")
})

test_that("convergence is unknown when Mplus provides no termination message", {
  model <- MplusAutomation:::extractConvergence_1file("MODEL RESULTS")

  expect_true(is.na(model$converged))
  expect_identical(model$status, "unknown")
  expect_true(is.na(model$reason))
  expect_length(model$message, 0L)
})

test_that("convergence describes the run and retains errors without a fit table", {
  file <- tempfile(fileext = ".out")
  on.exit(unlink(file))
  failure <- "NO CONVERGENCE.  NUMBER OF ITERATIONS EXCEEDED."
  header <- c("Mplus VERSION 9.1", "", "INPUT INSTRUCTIONS", "",
              paste0("  TITLE: ", failure), "", "INPUT READING TERMINATED NORMALLY", "")
  writeLines(c(header, "THE MODEL ESTIMATION TERMINATED NORMALLY", "",
               "TECHNICAL 9 OUTPUT", failure), file)
  expect_true(readModels(file, what = "converged")$converged)

  writeLines(c(header, failure), file)
  model <- readModels(file, what = c("converged", "warn_err"))
  expect_false(model$converged)
  expect_identical(model$errors[[1L]], failure)
})

Try the MplusAutomation package in your browser

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

MplusAutomation documentation built on Nov. 18, 2026, 1:06 a.m.