## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)
library(nlpsem)

## ----fit-model, message=FALSE-------------------------------------------------
set.seed(20261024)
n <- 80L
times <- 0:4
intercept <- rnorm(n, mean = 10, sd = 2)
slope <- rnorm(n, mean = 1.5, sd = 0.3)
dat <- data.frame(ID = seq_len(n))
for (j in seq_along(times)) {
  dat[[paste0("T", j)]] <- times[j]
  dat[[paste0("Y", j)]] <- intercept + slope * times[j] + rnorm(n, sd = 0.5)
}

fit <- getLGCM(
  dat, t_var = "T", y_var = "Y", records = 1:5,
  curveFun = "LIN", intrinsic = FALSE, paramOut = TRUE
)

## ----inspect-model------------------------------------------------------------
print(fit)
summary(fit)

## ----fit-diagnostics----------------------------------------------------------
diagnostics <- getDiagnostics(fit)
diagnostics

## ----estimates----------------------------------------------------------------
coef(fit)
logLik(fit)
printTable(fit)

## ----specification------------------------------------------------------------
provenance <- getSpecification(fit)
names(provenance)
provenance$specification
provenance$versions

## ----plot-model, message=FALSE, warning=FALSE, fig.alt="The simulated mean outcome increases from about 10 at baseline to 16 after four years. The fitted linear mean closely follows the smoothed observed mean, with pointwise 95 percent confidence limits on either side."----
plot(fit, xstarts = 0, xlab = "Years since baseline", outcome = "Simulated outcome")

## ----collect-plot-------------------------------------------------------------
figures <- plot(fit, draw = FALSE, xstarts = 0, xlab = "Years since baseline",
                outcome = "Simulated outcome")
class(figures)

## ----simulate-data, message=FALSE---------------------------------------------
sim <- getSimData(n = 500, curveFun = "QUAD", nT = 6, seed = 20261027)
truth <- attr(sim, "truth")
sim_fit <- getLGCM(
  sim, t_var = "T", y_var = "Y", records = 1:6,
  curveFun = "QUAD", intrinsic = FALSE, paramOut = TRUE
)
estimates <- sim_fit@Estimates[match(names(truth), sim_fit@Estimates$Name), ]
data.frame(truth = truth, estimate = round(estimates$Estimate, 3), SE = round(estimates$SE, 3))

## ----simulate-time-origin-----------------------------------------------------
shifted <- getSimData(n = 30, curveFun = "QUAD", times = 5:14, jitter = 0, seed = 1)
cbind(LGCM = attr(shifted, "truth")[c("mueta0", "psi00")],
      LCSM = attr(shifted, "truth_lcsm")[c("mueta0", "psi00")])
attr(shifted, "truth_info")$first_observation_time

## ----simulate-families--------------------------------------------------------
bivariate <- getSimData(n = 200, sub_Model = "MGM", curveFun = "LIN", seed = 1)
with_tvc <- getSimData(n = 200, sub_Model = "TVC", curveFun = "LIN", decompose = 1, seed = 1)
mediation <- getSimData(n = 200, sub_Model = "MED", curveFun = "LIN", x_type = "baseline", seed = 1)
head(attr(mediation, "truth"))

## ----existing-api-------------------------------------------------------------
getSummary(model_list = list(fit@mxOutput))

## ----legacy-plot, eval=FALSE--------------------------------------------------
# plot(
#   old_fit, sub_Model = "LGCM", y_var = "Y", curveFun = "LIN",
#   t_var = "T", records = 1:5, xstarts = 0,
#   xlab = "Years since baseline", outcome = "Outcome"
# )

