## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(MplusAutomation)

# Temporary working files for this vignette.
tmp_dir <- tempfile("mplusModel_vignette_")
dir.create(tmp_dir)

# A placeholder path so this vignette can run without Mplus installed.
fake_mplus <- tempfile("fake_mplus_")
file.create(fake_mplus)

## -----------------------------------------------------------------------------
model_syntax <- "
TITLE: OLS regression with mtcars data;
DATA: FILE IS mtcars_demo.dat;
VARIABLE:
  NAMES = mpg wt hp cyl qsec;
  USEVARIABLES = mpg wt hp;
ANALYSIS:
  ESTIMATOR = ML;
MODEL:
  mpg ON wt hp;
OUTPUT:
  STANDARDIZED;
"

model_data <- mtcars[, c("mpg", "wt", "hp", "cyl", "qsec")]

m <- mplusModel(
  syntax = model_syntax,
  data = model_data,
  dir = tmp_dir,
  file_stem = "mtcars_demo",
  Mplus_command = fake_mplus
)

m$dir
m$file_stem
m$model_dir
m$inp_file
m$dat_file
m$variables

## -----------------------------------------------------------------------------
m$write_dat()
m$write_inp()

m_from_inp <- mplusModel(
  inp_file = m$inp_file,
  read = FALSE,
  Mplus_command = fake_mplus
)

head(m_from_inp$syntax, n = 8)

## -----------------------------------------------------------------------------
m$write_dat()
m$write_inp()

file.exists(m$dat_file)
file.exists(m$inp_file)

## -----------------------------------------------------------------------------
m_clone <- update(
  m,
  MODEL = ~ . + "mpg ON cyl;",
  ANALYSIS = ~ "ESTIMATOR = MLR;"
)

any(grepl("mpg ON cyl;", m$syntax, fixed = TRUE))
any(grepl("mpg ON cyl;", m_clone$syntax, fixed = TRUE))

## -----------------------------------------------------------------------------
m$update(MODEL = ~ . + "mpg ON qsec;")
any(grepl("mpg ON qsec;", m$syntax, fixed = TRUE))

## -----------------------------------------------------------------------------
m$variables

m$variables <- c("mpg", "wt", "hp", "cyl")
m$variables

m$variables <- NULL
m$variables

## -----------------------------------------------------------------------------
out_file <- system.file("extdata", "ex3.1.out", package = "MplusAutomation")
file.copy(out_file, file.path(tmp_dir, "ex3.1.out"), overwrite = TRUE)

m_out <- mplusModel(
  out_file = file.path(tmp_dir, "ex3.1.out"),
  data = data.frame(y1 = 0, x1 = 0, x3 = 0),
  read = TRUE,
  Mplus_command = fake_mplus
)

m_out$inp_file
m_out$syntax[1:4]
m_out$summaries[c("AIC", "BIC", "CFI", "RMSEA_Estimate")]
head(m_out$parameters$unstandardized[, c("paramHeader", "param", "est", "se", "pval")])

## ----eval=FALSE---------------------------------------------------------------
# # Run locally
# m$run(replaceOutfile = "modifiedDate")
# 
# # Submit to SLURM/Torque (arguments passed through to submitModels())
# m$submit(
#   scheduler = "slurm",
#   replaceOutfile = "modifiedDate",
#   memgb_per_model = 8L,
#   cores_per_model = 1L,
#   time_per_model = "01:00:00"
# )

## ----cleanup, include=FALSE---------------------------------------------------
unlink(tmp_dir, recursive = TRUE, force = TRUE)
unlink(fake_mplus, force = TRUE)

