ehs-risk-sem
Back to ehs-risk-sem

model

ModelSpec

ModelSpec(latents: 'Dict[str, Sequence[str]]', paths: 'Dict[str, Sequence[str]]' = <factory>) -> None

Declaration of a structural equation model.

Parameters ---------- latents Ordered mapping from latent variable name to its indicator column names. Order determines the order of rows and columns in ``Phi``. paths Mapping from each endogenous latent name to the latents that point directly into it. Latents absent from this mapping are exogenous.

Example ------- >>> spec = ModelSpec( ... latents={ ... "OperationalStress": ["ot_hours", "backlog", "staffing_gap"], ... "SystemCondition": ["equip_age", "pm_overdue", "alarm_rate"], ... "IncidentRate": ["trir", "dart", "near_miss"], ... }, ... paths={"IncidentRate": ["OperationalStress", "SystemCondition"]}, ... )

Used by: dgp.py, study_01_sample_size.py, study_03_misspecification.py, study_04_coefficient_interpretation.py

SEMResults

SEMResults(spec: 'ModelSpec', n_obs: 'int', measurement: 'Dict[str, FactorSolution]', composite_correlations: 'np.ndarray', phi_estimated: 'np.ndarray', phi_implied: 'np.ndarray', phi_repaired: 'bool', paths: 'Dict[str, PathEstimates]', beta_matrix: 'np.ndarray', effects: 'Dict[str, np.ndarray]', observed_correlation: 'np.ndarray', implied_correlation: 'np.ndarray', fit_indices: 'FitIndices', identification: 'IdentificationReport', discriminant_validity: 'List[Tuple[str, str, float, float, bool]]', htmt_matrix: 'np.ndarray', warnings: 'List[str]' = <factory>) -> None

Everything a fitted model produced, including what went wrong.

bootstrap_paths

bootstrap_paths(spec: 'ModelSpec', data: 'pd.DataFrame', n_boot: 'int' = 500, seed: 'Optional[int]' = None, level: 'float' = 0.95) -> 'pd.DataFrame'

Nonparametric bootstrap of the structural coefficients.

Resamples rows with replacement and refits the entire model -- measurement model included -- so the resulting interval carries the uncertainty in the estimated loadings and reliabilities that the analytic standard errors ignore.

Returns a DataFrame with the point estimate, the bootstrap standard error, and percentile interval bounds. Replications that fail to fit are counted in the ``n_failed`` column rather than being silently discarded.

Used by: study_01_sample_size.py

fit

fit(spec: 'ModelSpec', data: 'pd.DataFrame', repair_improper_phi: 'bool' = True) -> 'SEMResults'

Estimate a model.

Parameters ---------- spec Model declaration. data Wide DataFrame with one column per indicator. Rows with any missing indicator are dropped, and the number dropped is reported as a warning; this package does not implement FIML or multiple imputation, and pretending listwise deletion is harmless would be dishonest. repair_improper_phi When the disattenuated latent correlation matrix is indefinite, project it onto the positive-definite cone so that estimation can continue. The repair is always recorded in ``SEMResults.phi_repaired`` and in the warnings, because an indefinite ``Phi`` is a finding about the model, not a numerical nuisance.

Raises ------ ValueError If the model fails the necessary identification conditions.

Used by: study_01_sample_size.py, study_03_misspecification.py, study_04_coefficient_interpretation.py

implied_covariance

implied_covariance(spec: 'ModelSpec', loadings_by_latent: 'Dict[str, np.ndarray]', uniquenesses_by_latent: 'Dict[str, np.ndarray]', beta_matrix: 'np.ndarray', exogenous_phi: 'np.ndarray', disturbance_variances: 'np.ndarray') -> 'Tuple[np.ndarray, np.ndarray]'

Rebuild the model-implied indicator covariance matrix.

``Sigma = Lambda Phi Lambda' + Theta`` with ``Phi = (I - B)^-1 Psi (I - B)^-T``, where ``Psi`` holds the exogenous latent covariances and the endogenous disturbance variances.

Returns ``(Sigma, Phi)``. ``Phi`` should have a unit diagonal; departures from it indicate an improper solution and are reported by :func:`fit`.