Class MissingDataUtils
java.lang.Object
edu.cmu.tetrad.data.missing.MissingDataUtils
Small shared utilities for the missing-data policies: listwise deletion, resolution of a possibly-null
MissingDataSpec to a policy (with a logged warning where the historical behavior was a silent switch), a
cheap missingness summary suitable for logging from constructors, and the effective-sample-size calculation for
analyses run on covariance matrices estimated from incomplete data.- Version:
- $Id: $Id
- Author:
- josephramsey
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringbriefSummary(DataSet dataSet) A one-line missingness summary cheap enough to compute in a constructor: one O(n * p) pass giving the overall missing rate, the number of complete rows, and the worst variable.static inteffectiveSampleSize(DataSet dataSet, MissingDataSpec spec) The effective sample size to use for penalized scores when the analysis runs on a covariance matrix estimated from incomplete data, per the spec'sMissingDataSpec.EffectiveSampleSizeMode.static DataModelemCovarianceIfRequested(DataModel dataModel, Parameters parameters, String caller) Implements the "em" missing-data policy at the wrapper level for covariance-consuming scores that have anICovarianceMatrixconstructor but no nativeMissingDataSpechandling (EBIC, GIC, Poisson prior, Zhang-Shen bound, and the like).static ICovarianceMatrixestimateCovariance(DataSet dataSet, MissingDataSpec spec) The covariance matrix of a dataset with missing values under a policy that estimates one: by EM with the spec's ridge, tolerance and iteration limit under EM_COVARIANCE, or by pairwise deletion (PairwiseCovarianceEstimator) under PAIRWISE_COVARIANCE.static MissingDataSpecfromParameters(Parameters parameters) Builds a MissingDataSpec from algcomparison/GUI/py-tetrad parameters (see the MISSING_* constants inParams), or returns null--meaning "legacy default behavior"--if the policy parameter is absent or "default".static DataModelgate(DataModel dataModel, Parameters parameters, boolean specAware, String caller) The wrapper-level missing-data gate, called at the top of every algcomparison test and score wrapper'sgetTest/getScoremethod.static DataModelThe finer-grained form ofgate(DataModel, Parameters, boolean, String), for components that support some native policies but not others (e.g., the Chi-Square test performs test-wise deletion natively but has no EM-covariance route, while Fisher Z supports both).static BlockSpecgate(BlockSpec blockSpec, Parameters parameters, String caller) The block-spec variant ofgate(DataModel, Parameters, boolean, String), for wrappers that construct block tests and scores from aBlockSpecrather than from the data model handed togetTest/getScore.static BlockSpecAsgate(BlockSpec, Parameters, String), for block components that implement some policies natively; seegate(DataModel, Parameters, java.util.Set, String)for the meaning of the native set.static DataSetlistwiseDelete(DataSet dataSet) Returns a new dataset consisting of the rows of the given dataset that have no missing entries (listwise deletion).static DataSetresolveDeletionPolicy(DataSet dataSet, MissingDataSpec spec, String caller) Applies a missing-data specification to a dataset for a component that supports only the deletion policies natively: returns the dataset itself when it is complete or the policy is TESTWISE (the component then does its own per-family or per-test row selection), its complete cases under LISTWISE, and throws otherwise.static MissingDataPolicyresolveOrWarn(DataSet dataSet, MissingDataSpec spec, String caller) Resolves a possibly-null spec to a policy for a component that historically switched silently to test-wise deletion on missing data.
-
Method Details
-
fromParameters
Builds a MissingDataSpec from algcomparison/GUI/py-tetrad parameters (see the MISSING_* constants inParams), or returns null--meaning "legacy default behavior"--if the policy parameter is absent or "default". Policy values (case-insensitive): "fail", "listwise", "testwise", "em" (or "emCovariance"), "pairwise" (or "pairwiseCovariance"; seeMissingDataPolicy.PAIRWISE_COVARIANCE), "mi" (or "multipleImputation"). ESS modes: "fullN", "minPairwise", "meanPairwise".- Parameters:
parameters- The parameters.- Returns:
- The spec, or null for the legacy default.
- Throws:
IllegalArgumentException- If the policy or ESS mode string is unrecognized.
-
resolveDeletionPolicy
Applies a missing-data specification to a dataset for a component that supports only the deletion policies natively: returns the dataset itself when it is complete or the policy is TESTWISE (the component then does its own per-family or per-test row selection), its complete cases under LISTWISE, and throws otherwise. A null spec on missing data is treated as FAIL.- Parameters:
dataSet- The dataset.spec- The spec, or null (FAIL on missing data).caller- The name of the calling component, for messages.- Returns:
- The dataset to analyze.
- Throws:
IllegalArgumentException- If the policy is FAIL or EM_COVARIANCE and the dataset has missing values.UnsupportedOperationException- If the policy is MULTIPLE_IMPUTATION.
-
listwiseDelete
Returns a new dataset consisting of the rows of the given dataset that have no missing entries (listwise deletion). The given dataset is not modified.- Parameters:
dataSet- The dataset.- Returns:
- The complete-case dataset.
- Throws:
IllegalArgumentException- If no complete rows remain.
-
resolveOrWarn
Resolves a possibly-null spec to a policy for a component that historically switched silently to test-wise deletion on missing data. If the spec is non-null, its policy is returned. If the spec is null and the dataset has missing values,MissingDataPolicy.TESTWISEis returned for backward compatibility, but a warning naming the caller, the policy, and its MCAR assumption--together with a brief missingness summary--is logged throughTetradLogger, replacing the silent switch. If the dataset is complete, the policy is irrelevant and TESTWISE is returned.- Parameters:
dataSet- The dataset.spec- The spec, or null if the user did not provide one.caller- The name of the calling component, for the log message.- Returns:
- The resolved policy.
-
briefSummary
A one-line missingness summary cheap enough to compute in a constructor: one O(n * p) pass giving the overall missing rate, the number of complete rows, and the worst variable. (The fullMissingDataAudit, which additionally computes pairwise complete counts in O(n * p^2), is opt-in.)- Parameters:
dataSet- The dataset.- Returns:
- This summary.
-
effectiveSampleSize
The effective sample size to use for penalized scores when the analysis runs on a covariance matrix estimated from incomplete data, per the spec'sMissingDataSpec.EffectiveSampleSizeMode. ForFULL_Nthis returns -1, the existing convention meaning "use the nominal sample size"; the pairwise modes compute the pairwise complete counts (O(n * p^2)) viaMissingDataAudit.- Parameters:
dataSet- The dataset the covariance matrix was estimated from.spec- The spec.- Returns:
- The effective sample size, or -1 for the nominal sample size.
-
estimateCovariance
The covariance matrix of a dataset with missing values under a policy that estimates one: by EM with the spec's ridge, tolerance and iteration limit under EM_COVARIANCE, or by pairwise deletion (PairwiseCovarianceEstimator) under PAIRWISE_COVARIANCE. Every component that accepts an EM covariance gets its matrix here, so each supports both; seeMissingDataPolicy.estimatesCovariance().- Parameters:
dataSet- The dataset.spec- The spec; its policy is expected to be EM_COVARIANCE or PAIRWISE_COVARIANCE.- Returns:
- The covariance matrix.
-
gate
public static DataModel gate(DataModel dataModel, Parameters parameters, boolean specAware, String caller) The wrapper-level missing-data gate, called at the top of every algcomparison test and score wrapper'sgetTest/getScoremethod. This is the point at which the explicit-choice contract for missing data is enforced for all user-facing interfaces (the GUI search box, algcomparison, and py-tetrad, all of which construct tests and scores through these wrappers).Semantics: if the data model is null, is not a tabular
DataSet(e.g., a covariance matrix, which cannot contain missing values), or is a complete dataset, it is returned unchanged and the policy is irrelevant. Otherwise the dataset contains missing values, and:- Policy "default" (or unset): an
IllegalArgumentExceptionis thrown asking the user to choose a policy. This replaces the previous behavior in which components silently (later: with a logged warning) switched to test-wise deletion. To restore the previous behavior explicitly, set the policy to "testwise". - "fail": an exception is thrown, per that policy's contract.
- "listwise": the complete-case dataset is returned. This is supported for every test and score, since it is a pure data transformation performed here, upstream of the component.
- "testwise" or "em": the dataset is passed through unchanged if
specAwareis true (the wrapper passes aMissingDataSpecto a component that implements these policies natively); otherwise an exception is thrown naming "listwise" as the supported alternative. - "mi": an exception is thrown. Multiple imputation runs the whole search over m imputed datasets via
ImputationSearchand is not the responsibility of a single test or score; it is not yet wired into the wrapper interfaces. (ImputationSearch hands complete datasets to the algorithm it runs, so this gate passes through in that context.)
- Parameters:
dataModel- The data model handed to the wrapper; may be null or a non-tabular model.parameters- The parameters, from whichParams.MISSING_DATA_POLICYis read.specAware- True if the wrapper passes a MissingDataSpec through to a component with native TESTWISE / EM_COVARIANCE support.caller- The user-facing name of the test or score, for error messages.- Returns:
- The data model, possibly replaced by its complete-case version under the "listwise" policy.
- Throws:
IllegalArgumentException- As described above.
- Policy "default" (or unset): an
-
gate
public static DataModel gate(DataModel dataModel, Parameters parameters, Set<String> nativePolicies, String caller) The finer-grained form ofgate(DataModel, Parameters, boolean, String), for components that support some native policies but not others (e.g., the Chi-Square test performs test-wise deletion natively but has no EM-covariance route, while Fisher Z supports both). The boolean form delegates here with{"testwise", "em"}or the empty set.- Parameters:
dataModel- The data model handed to the wrapper; may be null or a non-tabular model.parameters- The parameters, from whichParams.MISSING_DATA_POLICYis read.nativePolicies- The policies the underlying component implements natively, from among "testwise" and "em". ("fail" and "listwise" are implemented here for every component, and "mi" is a search-level policy.)caller- The user-facing name of the test or score, for error messages.- Returns:
- The data model, possibly replaced by its complete-case version under the "listwise" policy.
- Throws:
IllegalArgumentException- As for the boolean form.
-
gate
The block-spec variant ofgate(DataModel, Parameters, boolean, String), for wrappers that construct block tests and scores from aBlockSpecrather than from the data model handed togetTest/getScore. The embedded dataset is checked; under the "listwise" policy a new BlockSpec is returned wrapping the complete-case dataset with the same blocks, block variables, and ranks (blocks index columns, which row deletion does not disturb). This form declares no native policies; block components with native test-wise support (e.g., the Wilks blocks test) use the four-argument form.- Parameters:
blockSpec- The block spec.parameters- The parameters.caller- The user-facing name of the test or score, for error messages.- Returns:
- The block spec, possibly rebuilt on the complete-case dataset under the "listwise" policy.
- Throws:
IllegalArgumentException- As for the data-model gate.
-
gate
public static BlockSpec gate(BlockSpec blockSpec, Parameters parameters, Set<String> nativePolicies, String caller) Asgate(BlockSpec, Parameters, String), for block components that implement some policies natively; seegate(DataModel, Parameters, java.util.Set, String)for the meaning of the native set.- Parameters:
blockSpec- The block spec.parameters- The parameters.nativePolicies- The policies the block component implements natively, from among "testwise" and "em".caller- The user-facing name of the test or score, for error messages.- Returns:
- The block spec, possibly rebuilt on the complete-case dataset under the "listwise" policy.
- Throws:
IllegalArgumentException- As for the data-model gate.
-
emCovarianceIfRequested
public static DataModel emCovarianceIfRequested(DataModel dataModel, Parameters parameters, String caller) Implements the "em" missing-data policy at the wrapper level for covariance-consuming scores that have anICovarianceMatrixconstructor but no nativeMissingDataSpechandling (EBIC, GIC, Poisson prior, Zhang-Shen bound, and the like). Called aftergate(DataModel, Parameters, java.util.Set, String)with "em" in the native set: if the data model is a dataset with missing values and the chosen policy is "em", the EM-estimated covariance matrix (valid under MAR for approximately multivariate normal data) is returned in its place, with its sample size adjusted per the spec's effective-sample-size mode if a pairwise mode is chosen; the wrapper's covariance branch then constructs the score from it. In every other case the data model is returned unchanged.- Parameters:
dataModel- The data model, already passed through the gate.parameters- The parameters.caller- The user-facing name of the score, for the log message.- Returns:
- The data model, or the EM-estimated covariance matrix in its place under the "em" policy.
-