| 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 |
| 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 |
nsim |
An integer specifying the number of retained draws per subject
(default |
individual_index |
An optional integer vector specifying which rows
(individuals) to sample. Defaults to |
method |
A character string specifying the sampling algorithm:
|
mh_burnin |
An integer specifying the burn-in period for the MH
algorithm (default |
mh_tune_c |
A positive numeric scaling factor for the MH proposal
covariance (default |
y_override |
Optional numeric scalar overriding the observed outcome
|
Value
A named list (indexed by character row numbers) where each element is a list containing:
-
y_observed: The outcome value used for that subject. -
marginal_means: Stage-1 empirical Bayes latent score vector\hat{\eta}_i = E[\eta_i \mid Z_i]. -
Means: Mean vectorE[\eta_i \mid Z_i, y_i]. -
Draws: Annsim x pmatrix of retained draws. -
mh_accept_rate: MH acceptance rate (NAwhenmethod = "SIR"). -
Summary: A data frame summarizing each latent factor'sMarginal_Mean, conditionalMean,SD,Median, and 95\
See Also
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 |
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 |
num.it |
An integer specifying the maximum number of iterations. Defaults to |
tol |
A numeric value specifying the convergence tolerance threshold. Defaults to |
cfa_estimator |
A character string specifying the |
family |
A character string specifying the generalized linear model family ( |
link |
A character string specifying the link function ( |
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 |
probit_exact |
A logical indicating whether to use exact analytical integration for the probit link. Defaults to |
Details
The algorithm operates in two modular stages:
-
Stage 1: Fits a measurement model using
lavaanto extract latent factor scores and their conditional covariance matrix. -
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:
-
Gamma: Modeled with Var(y) = dispersion * mean^2.
-
Negative Binomial: Modeled with Var(y) = mean + (mean^2 / dispersion).
Supported outcome distributions and link functions include:
-
family = "binomial": paired withlink = "logit"orlink = "probit" -
family = "poisson": paired withlink = "log" -
family = "negbin": paired withlink = "log" -
family = "gamma": paired withlink = "log"
Value
A list containing:
-
beta_hat: The final estimated structural parameters. -
dispersion_hat: The estimated dispersion parameter (applicable to Gamma/Negative Binomial). -
BREAK: An integer indicating if the algorithm did not converge (1) or ran normally (0). -
Num.it: The number of iterations completed. -
eb_beta: The empirical Bayes starting values extracted during initialization. -
beta_history: A matrix tracking parameter estimates across iterations. -
gradient_history: A vector tracking the gradient norm across iterations. -
scores: A matrix containing the Stage 1 empirical Bayes expected latent factor scores for each complete case. -
cov_scores: A matrix representing the approximate conditional covariance of the latent space, extracted from the measurement model. -
V_beta: The conditional variance-covariance matrix of the estimated structural parameters not accounting for Stage-1 uncertainties. -
converged: TRUE/FALSE indicating convergence.
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
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 |
R_boot |
Integer number of bootstrap replications (default |
ncores |
Integer number of CPU cores for parallel execution via a
cross-platform PSOCK cluster (default |
seed |
Optional integer seed for reproducible resampling. |
B |
Optional integer overriding the number of Monte Carlo draws inside
bootstrap replicates. Defaults to |
Value
A list containing:
-
VCOV: The(p+1) \times (p+1)bootstrap covariance matrix. -
SE: Named numeric vector of bootstrap standard errors. -
boot_beta: Matrix of converged bootstrap parameter estimates (rows = replicates, columns = parameters). -
n_converged: Number of converged bootstrap replicates. -
R_boot: Total number of requested bootstrap replicates.
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 |
... |
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 |
digits |
Minimum number of significant digits to print (default |
show_draws |
Logical; if |
... |
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 |
digits |
Minimum number of significant digits to print (default |
... |
Additional arguments passed to |
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 |
Vcov |
Optional precomputed variance-covariance matrix (e.g., from
|
stage1 |
Logical; if |
R_boot |
Integer number of bootstrap replications when
|
ncores |
Integer number of CPU cores when |
seed |
Optional integer seed when |
... |
Additional arguments passed to |
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 |
conditional |
Logical; if |
R_boot |
Integer number of bootstrap replications when
|
ncores |
Integer number of CPU cores for parallel bootstrap (default |
seed |
Optional integer seed for reproducible bootstrap resampling. |
... |
Additional arguments passed to |
Value
A (p+1) \times (p+1) variance-covariance matrix.