Package {TSQLEM}


Title: Two Stage Estimation for Generalized Structural Equation Models
Version: 0.2.0
Description: Provides a framework to estimate generalized structural equation models using two stage quasi-likelihood. The structural model supports binomial (logit and probit), Poisson, negative binomial, and gamma distributions for the outcome variable, and includes parallel nonparametric bootstrap covariance estimation. Hattab (2026) "A Two Stage Quasi-Likelihood Estimation Method for High Dimensional Generalized Structural Equation Models" <doi:10.48550/arXiv.2608.16017>.
License: GPL (≥ 3)
Encoding: UTF-8
RoxygenNote: 8.0.0
Depends: R (≥ 4.1.0)
Imports: lavaan, Matrix, MASS, parallel, stats
Suggests: psych
NeedsCompilation: no
Packaged: 2026-11-27 01:39:24 UTC; Admin
Author: Mohammad Hattab ORCID iD [aut, cre, cph]
Maintainer: Mohammad Hattab <mwhattab@unm.edu>
Repository: CRAN
Date/Publication: 2026-11-30 03:20:02 UTC

Sample the Full Conditional Distribution of the Latent Space

Description

Extracts the full conditional distribution f(\eta_i \mid Z_i, y_i) of the latent variables given the observed indicators and the structural outcome from a fitted TSQLEM object via Metropolis-Hastings (MH) MCMC or Sampling Importance Resampling (SIR).

Usage

CondLatent(
  object,
  nsim = 10000,
  individual_index = NULL,
  method = "MH",
  mh_burnin = 2000,
  mh_tune_c = 1,
  y_override = NULL
)

Arguments

object

A fitted TSQLEM object.

nsim

An integer specifying the number of retained draws per subject (default 10000).

individual_index

An optional integer vector specifying which rows (individuals) to sample. Defaults to NULL (all subjects).

method

A character string specifying the sampling algorithm: "MH" (Metropolis-Hastings, default) or "SIR" (Sampling Importance Resampling).

mh_burnin

An integer specifying the burn-in period for the MH algorithm (default 2000).

mh_tune_c

A positive numeric scaling factor for the MH proposal covariance (default 1.0; tune to target an acceptance rate of roughly 0.23 to 0.44).

y_override

Optional numeric scalar overriding the observed outcome y_i for all selected individuals (default NULL, using the observed outcome). Useful for counterfactual comparisons (e.g., comparing y_override = 0 vs. y_override = 1).

Value

A named list (indexed by character row numbers) where each element is a list containing:

See Also

TSQLEM

Examples

# 1. Prepare the data
library(psych)
data(bfi)
df <- na.omit(bfi[, c(1:25, 26)])

# 2. Define measurement model
model_cfa <- '
  Agree =~ A1 + A2 + A3 + A4 + A5
  Cons  =~ C1 + C2 + C3 + C4 + C5
  Extra =~ E1 + E2 + E3 + E4 + E5
  Neuro =~ N1 + N2 + N3 + N4 + N5
  Open  =~ O1 + O2 + O3 + O4 + O5'

# 3. Fit TSQLEM model
Fit <- TSQLEM(df = df,
              y_name = "gender",
              lav_syn_cfa = model_cfa,
              cfa_estimator = "ML",
              family = "binomial",
              link = "probit",
              probit_exact = TRUE)

# 4. Sample full conditional latent distributions for selected individuals
mcmc_results <- CondLatent(Fit,
                           nsim = 100, # Increase to > 1000
                           individual_index = c(10, 376, 1057),
                           method = "MH",
                           mh_burnin = 30, # Increase
                           mh_tune_c = 1.25)

print(mcmc_results)

# Counterfactual density plot for individual 10 (y = 0 vs. y = 1)

cf0 <- CondLatent(Fit, nsim = 1000, mh_burnin = 300, individual_index = 10,
                  y_override = 0, mh_tune_c = 1.25)
cf1 <- CondLatent(Fit, nsim = 1000, mh_burnin = 300, individual_index = 10,
                  y_override = 1, mh_tune_c = 1.25)


d0 <- density(cf0[["10"]]$Draws[, "Agree"])
d1 <- density(cf1[["10"]]$Draws[, "Agree"])
plot(d0, col = "firebrick", lwd = 2,
     xlim = range(c(d0$x, d1$x)), ylim = range(c(d0$y, d1$y)),
     xlab = "Agreeableness", ylab = "Density",
     main = "Counterfactual Conditional Density (Individual 10)")
lines(d1, col = "steelblue", lwd = 2)
legend("topright", legend = c("y = 0", "y = 1"),
       col = c("firebrick", "steelblue"), lwd = 2, bty = "n")

Two-Stage Quasi-Likelihood Estimation for Generalized Structural Equation Models

Description

Fits high-dimensional generalized structural equation models (GSEMs) utilizing a Two-Stage Quasi-Likelihood Expectation-Maximization (TSQLEM) framework.

Usage

TSQLEM(
  df,
  y_name,
  lav_syn_cfa,
  B = 2000,
  start_beta = NA,
  num.it = 20,
  tol = 10^-5,
  cfa_estimator = "ML",
  family = "binomial",
  link = "logit",
  DebiasBern = TRUE,
  probit_exact = TRUE
)

Arguments

df

A data frame containing the observed indicators and the outcome variable. Rows with missing values are omitted internally.

y_name

A character string specifying the name of the outcome variable in the dataset (must be binary for binomial families).

lav_syn_cfa

A character string specifying the measurement model syntax in lavaan format.

B

An integer specifying the number of Monte Carlo draws for the E-step when simulation is required (default = 2000).

start_beta

A numeric vector of initial starting values for the structural parameters. Defaults to NA (auto-initialized).

num.it

An integer specifying the maximum number of iterations. Defaults to 20.

tol

A numeric value specifying the convergence tolerance threshold. Defaults to 10^-5.

cfa_estimator

A character string specifying the lavaan estimator for Stage 1. Defaults to "ML".

family

A character string specifying the generalized linear model family ("binomial", "poisson", "gamma", "negbin"). Defaults to "binomial".

link

A character string specifying the link function ("logit", "probit", "log"). Defaults to "logit".

DebiasBern

A logical indicating whether to apply a second-order Delta Method correction to debias weights for Bernoulli outcomes when MC simulations are utilized. Defaults to TRUE.

probit_exact

A logical indicating whether to use exact analytical integration for the probit link. Defaults to TRUE.

Details

The algorithm operates in two modular stages:

  1. Stage 1: Fits a measurement model using lavaan to extract latent factor scores and their conditional covariance matrix.

  2. Stage 2: Employs marginal quasi-likelihood estimating equations via an iteratively reweighted least squares (IRLS) scheme, utilizing either exact analytical integration or Monte Carlo simulations.

For Gamma and Negative Binomial models, extra dispersion parameters are estimated internally via profile working models using standard variance parameterizations:

Supported outcome distributions and link functions include:

Value

A list containing:

Author(s)

Mohammad W. Hattab

References

Hattab, M. W. (2026). A Two Stage Quasi-Likelihood Estimation Method for High Dimensional Generalized Structural Equation Models. arXiv:2608.16017. doi:10.48550/arXiv.2608.16017

See Also

CondLatent

Examples

#PREP THE DATA
# ==========================================
library(psych)
data(bfi)
df <- na.omit(bfi[, c(1:25, 26)])
model_cfa <- '
   Agree =~ A1 + A2 + A3 + A4 + A5
   Cons  =~ C1 + C2 + C3 + C4 + C5
   Extra =~ E1 + E2 + E3 + E4 + E5
   Neuro =~ N1 + N2 + N3 + N4 + N5
   Open  =~ O1 + O2 + O3 + O4 + O5'

Fit <- TSQLEM(df = df,
              y_name = "gender",
              lav_syn_cfa = model_cfa,
              cfa_estimator = "ML",
              family = "binomial",
              link = "probit",
              probit_exact = TRUE)

print(Fit)
coef(Fit)
vcov(Fit) # Stage-2 conditional covariance
summary(Fit)
# Account for Stage-1 uncertainty via nonparametric bootstrap
# (Increase R_boot >= 500 and set ncores > 1 in practice)
boot_Fit <- boot_covTSQLEM(Fit, R_boot = 20, ncores = 1, seed = 2022)
summary(Fit, Vcov = boot_Fit)

Nonparametric Bootstrap Covariance for TSQLEM Objects

Description

Estimates the variance-covariance matrix of the estimated structural parameters in a fitted TSQLEM model via parallel nonparametric bootstrap.

Usage

boot_covTSQLEM(object, R_boot = 200, ncores = 1, seed = NULL, B = NULL)

Arguments

object

A fitted TSQLEM object.

R_boot

Integer number of bootstrap replications (default 200).

ncores

Integer number of CPU cores for parallel execution via a cross-platform PSOCK cluster (default 1).

seed

Optional integer seed for reproducible resampling.

B

Optional integer overriding the number of Monte Carlo draws inside bootstrap replicates. Defaults to object$B.

Value

A list containing:


Extract Structural Coefficients from a TSQLEM Object

Description

Extract Structural Coefficients from a TSQLEM Object

Usage

## S3 method for class 'TSQLEM'
coef(object, ...)

Arguments

object

A fitted TSQLEM object.

...

Additional arguments (currently unused).

Value

A named numeric vector of estimated structural parameters.


Print Method for CondLatent Objects

Description

Print Method for CondLatent Objects

Usage

## S3 method for class 'CondLatent'
print(x, digits = 4, show_draws = FALSE, ...)

Arguments

x

An object returned by CondLatent.

digits

Minimum number of significant digits to print (default 4).

show_draws

Logical; if FALSE (default), suppresses printing raw draws. If TRUE, prints the first 6 rows of each individual's Draws matrix.

...

Additional arguments (currently unused).

Value

Invisibly returns x.


Print Method for TSQLEM Objects

Description

Print Method for TSQLEM Objects

Usage

## S3 method for class 'TSQLEM'
print(x, digits = 4, ...)

Arguments

x

A fitted TSQLEM object.

digits

Minimum number of significant digits to print (default 4).

...

Additional arguments passed to print.default.

Value

Invisibly returns x.


Summary Method for TSQLEM Objects

Description

Prints a summary table of structural parameter estimates, standard errors, z-statistics, p-values, and 95\ errors can be computed either from the conditional covariance (stage1 = FALSE, default) or by accounting for Stage-1 measurement uncertainty via nonparametric bootstrap (stage1 = TRUE or by supplying a pre-computed bootstrap covariance matrix via Vcov).

Usage

## S3 method for class 'TSQLEM'
summary(
  object,
  Vcov = NULL,
  stage1 = FALSE,
  R_boot = 200,
  ncores = 1,
  seed = NULL,
  ...
)

Arguments

object

A fitted TSQLEM object.

Vcov

Optional precomputed variance-covariance matrix (e.g., from vcov(object, conditional = FALSE) or boot_covTSQLEM(object)$VCOV). Overrides stage1 when supplied.

stage1

Logical; if FALSE (default), uses conditional covariance object$V_beta (Stage-1 uncertainty not accounted for). If TRUE, runs boot_covTSQLEM to account for Stage-1 uncertainty in standard errors, p-values, and confidence intervals.

R_boot

Integer number of bootstrap replications when stage1 = TRUE (default 200).

ncores

Integer number of CPU cores when stage1 = TRUE (default 1).

seed

Optional integer seed when stage1 = TRUE.

...

Additional arguments passed to boot_covTSQLEM.

Value

Invisibly returns a list containing the coefficient matrix (coefficients) and the covariance matrix used (Vcov).


Extract Variance-Covariance Matrix from a TSQLEM Object

Description

Extracts either the covariance matrix assuming Stage-1 is known (conditional = TRUE, default) or the nonparametric bootstrap covariance matrix accounting for Stage-1 measurement uncertainty (conditional = FALSE).

Usage

## S3 method for class 'TSQLEM'
vcov(object, conditional = TRUE, R_boot = 200, ncores = 1, seed = NULL, ...)

Arguments

object

A fitted TSQLEM object.

conditional

Logical; if TRUE (default), returns object$V_beta (Stage-1 known). If FALSE, runs boot_covTSQLEM to account for Stage-1 uncertainty.

R_boot

Integer number of bootstrap replications when conditional = FALSE (default 200).

ncores

Integer number of CPU cores for parallel bootstrap (default 1).

seed

Optional integer seed for reproducible bootstrap resampling.

...

Additional arguments passed to boot_covTSQLEM.

Value

A (p+1) \times (p+1) variance-covariance matrix.