
Evaluate model fit of an estimated causal graph
Source:R/evaluate_model_fit.r
evaluate_model_fit.RdFits the causal graph implied by adjacency_matrix as a structural
equation model (SEM) via lavaan::sem() and returns standard SEM fit
measures (CFI, RMSEA, AIC/BIC, etc.). This is an R port of the Python
lingam.utils.evaluate_model_fit(), which delegates to the Python
package semopy; this R version delegates to lavaan instead.
Arguments
- adjacency_matrix
p x p numeric adjacency matrix (NA allowed for latent confounder pairs), or a lingamr result object (e.g.
LingamResult,ParceLingamResult,LiMResult) with anadjacency_matrixelement, from which the matrix is extracted automatically- X
numeric matrix or data frame (n_samples x p) with no missing values
- is_ordinal
logical or 0/1 vector of length p.
TRUEmarks a variable as ordinal (categorical), fit withlavaan's WLSMV-based estimator.NULL(default) treats all variables as continuous.
Value
A one-row data.frame of fit measures: DoF, DoF Baseline, chi2,
chi2 p-value, chi2 Baseline, CFI, GFI, AGFI, NFI, TLI, RMSEA, AIC,
BIC, LogLik. When is_ordinal is used, AIC/BIC/LogLik and some other
measures are not defined by the WLSMV estimator and are returned as
NA.
Details
Optional dependency: this function requires the lavaan package (listed in
Suggests, notImports). Install it withinstall.packages("lavaan").Latent confounders: an
NAelementB[i, j](as produced by e.g.lingam_parce()for a suspected latent confounder between variables i and j) is represented as a residual covariancexi ~~ xjin the lavaan model. This is algebraically equivalent to the two-indicator latent common cause (one loading fixed to 1) used by the Pythonsemopyimplementation, but is expressed with lavaan's standard residual-covariance idiom rather than an explicit latent variable.Numerical values will not match
semopyexactly:lavaanandsemopyuse different default estimators/options. The fit measures returned are the same statistics, but exact numbers can differ.Convention:
adjacency_matrixfollows the lingamr conventionB[i, j]= causal coefficient from variable j to variable i (j -> i).
References
Rosseel, Y. (2012). lavaan: An R Package for Structural Equation Modeling. Journal of Statistical Software, 48(2), 1-36. doi:10.18637/jss.v048.i02
Examples
if (requireNamespace("lavaan", quietly = TRUE)) {
dat <- generate_lingam_sample_6()
result <- lingam_direct(dat$data, reg_method = "ols")
evaluate_model_fit(result, dat$data)
}
#> DoF DoF Baseline chi2 chi2 p-value chi2 Baseline CFI GFI AGFI NFI TLI RMSEA
#> 1 0 15 0 NA 23023.7 1 1 1 1 1 0
#> AIC BIC LogLik
#> 1 1860.598 1958.753 -910.2991