Why Deterministic Preprocessing Matters
MICE operates on variables as they are represented in the input data. Before imputation, a workflow may convert special numeric codes to missing values, define categorical variables, or transform a skewed measurement. If those steps depend on row order, observed values, or undocumented software defaults, two runs that appear to use the same data can give MICE different inputs. Reproducibility therefore requires more than saving a random seed: the preprocessing rules themselves must be explicit and repeatable.
This tutorial concerns the construction of a fixed MICE input from a specified raw data set and preprocessing specification. It does not imply that MICE is deterministic: imputation may involve random draws, and its results also depend on the imputation configuration and random-number handling. The earlier material on Using Configuration Files for Imputation Runs provides a place to record settings; here the focus is on making the transformations those settings describe unambiguous.
Determinism is a property of the rule and its inputs, not a claim that the rule is statistically appropriate. For example, a fixed but incorrect recoding is perfectly repeatable. The analyst must therefore justify each rule using the variable definition and data provenance, then make the implementation match that rule.
Specify Operations Before Applying Them
A preprocessing specification should answer three questions for each variable: which raw values are valid, what action is taken for each value or category, and how is the resulting variable represented? Avoid letting software infer answers from the values that happen to be observed in one run.
- For special missing codes, state exactly which codes are converted to missing and whether they apply to the whole variable or only under a defined condition.
- For categorical variables, state the complete permitted set of levels, their order, and any reference category used by a model.
- For numerical transformations, state the formula, the domain on which it is defined, and any constants or rounding rules.
- State how unexpected values are handled. A clear error is often safer than silently creating a new category or leaving an invalid value in place.
These choices should not depend on “first value encountered,” automatic alphabetical sorting, or the categories present in a particular batch of rows. Nor should a transformation silently estimate a new constant from each run when a fixed constant is intended. If a rule genuinely uses a data-derived quantity, its data source and calculation must be specified; the resulting operation is not a fixed row-wise map.
Worked Example: Recode a Sentinel Value Explicitly
Suppose a hypothetical raw variable, fasting glucose in mmol/L, uses -9 to mean “not recorded.” The five raw entries are 5.2, -9, 6.1, 4.8, and -9. The data dictionary confirms that -9 is a missing-value code, not a possible measurement. The preprocessing rule is: replace exactly -9 with a missing value; retain every other value unchanged.
Let \(x\) denote a raw entry. The rule is \(x^*=\text{missing}\) if \(x=-9\), and \(x^*=x\) otherwise. Applying it gives 5.2, missing, 6.1, 4.8, missing. The recorded measurements remain in mmol/L; no numeric sentinel is left for MICE to treat as an observed glucose value.
This rule gives the same result if the rows are reordered, because each entry is recoded by its value rather than by its position. By contrast, a rule such as “replace the lowest observed value with missing” would not express the data definition and could behave differently when the data change. The equality condition must be based on the documented code, and any values other than -9 should be retained or rejected according to an explicitly stated validity rule.
Fix Factor Levels and Their Order
A categorical variable is more than a collection of labels: software may use its level order when constructing model matrices, selecting a reference category, or displaying summaries. If levels are inferred from whichever categories occur in the current data, a category absent from one subset may disappear. If ordering is inferred from first appearance, shuffling rows can change the order. Both behaviours can alter the representation supplied to an imputation model even though the underlying category labels have not changed.
Declare the allowed levels from the variable definition, rather than discovering them afresh from each processed data set. If a model uses a reference level, name it explicitly and verify that it is among the declared levels. A level with no observations is not thereby evidence that its definition should be discarded; however, an empty or sparse category may cause model-fitting problems that need separate diagnosis.
Worked Example: Make a Binary Factor Independent of Row Order
A hypothetical data set records smoking status as “No” or “Yes.” In one order, the first three observed values are Yes, No, Yes; in a second order they are No, Yes, Yes. Suppose a software routine assigns factor levels in order of first appearance. The first ordering can therefore produce levels Yes, No, while the second produces No, Yes.
If the intended model uses “No” as the reference category, specify the ordered levels as No, Yes and state No as the reference. The first row ordering is then represented by the same two-level factor definition as the second. In a treatment-coded model, the intercept corresponds to the reference category, and the coefficient for Yes compares Yes with No. The explicit declaration prevents the reference from changing merely because the rows were rearranged.
Now suppose a particular processed batch contains only No values. If levels are inferred from observed values, Yes may be dropped; a later batch containing both categories can then have a different design. With declared levels No and Yes, the variable retains its defined set of levels in both batches. The absent category still has zero observations in the first batch, so a model may not be estimable there; deterministic coding does not solve that statistical limitation. It does make the change visible and prevents accidental redefinition of the variable.
Make Transformations and Constants Explicit
A transformation should specify both its mathematical operation and how it handles values outside the operation’s domain. For example, the logarithm \(\log(x)\) is defined for \(x>0\). If zero or negative values are possible, the preprocessing rule must say whether they are invalid, represent a special code, or require a different justified transformation. Silently producing undefined values is not a reproducible handling strategy.
Standardization is another common source of variation. Given a fixed reference mean \(a\) and positive scale \(b\), the standardized value is \(z=(x-a)/b\). The values \(a\) and \(b\) must be stated if the transformation is intended to be fixed. Recomputing a mean and standard deviation on each data set defines a different operation, potentially yielding different transformed values. Whether constants should be fixed or estimated is a substantive design decision, not a software default to leave undocumented.
Worked Example: Apply a Log Transformation with a Defined Domain
Consider hypothetical positive biomarker measurements of 2, 5, and 10 mg/L, and a planned transformation \(y=\log(x)\), using the natural logarithm. The domain condition is \(x>0\), which holds for all three entries. The transformed values are \(\log(2)\approx0.6931\), \(\log(5)\approx1.609\), and \(\log(10)\approx2.303\). The transformed variable is dimensionless because the logarithm is applied to the numerical values expressed on the stated mg/L scale.
If an entry were 0, the logarithm would not be finite; if it were negative, the real-valued logarithm would be undefined. The specified rule must therefore detect such values and stop, flag them, or apply a separately justified handling rule. It must not depend on the order in which values are processed. If a different measurement scale is used, the transformation’s scale convention must also be stated, since taking the logarithm of a numerical value expressed in different units changes its numerical result.
Why Row-Order Invariance Follows from a Fixed Rule
For a purely row-wise preprocessing rule, order invariance is straightforward to justify. Let the raw rows be \(x_1,\ldots,x_n\), and let a fixed rule \(f\) apply the same specified mapping to every row. The processed sequence is \(f(x_1),\ldots,f(x_n)\). If a permutation \(\pi\) reorders the raw rows, processing afterward gives \(f(x_{\pi(1)}),\ldots,f(x_{\pi(n)})\). That is exactly the original processed sequence in the same permuted order.
Thus, a fixed row-wise rule commutes with reordering: it does not create a different value for a row just because that row moved. This justification assumes the mapping is fixed and does not use row position, the order of first appearance, or a quantity recalculated from the changing data. Operations that estimate quantities from the whole data set need their own specification and checks; the simple argument above does not establish their invariance or appropriateness.
Define valid values, recodes, factor levels, transformation domains, and constants before running preprocessing.
Keep the raw values unchanged so the processed input can be traced back to its source.
Confirm that the output has the required variables and levels, and compare processed results after a row permutation, restoring row identity for comparison.
Check the processed values, missingness, category counts, and model-matrix coding before interpreting an imputation run.
Diagnostics, Alternatives, and Limitations
Useful diagnostics include comparing the processed data from repeated runs, checking that row-wise outputs match after aligning records by a stable identifier, and summarizing values before and after each recode or transformation. For categorical variables, inspect the declared level list, counts by level, and the intended reference category. For numerical variables, check the range and the number of values outside each transformation’s domain. Unexpected levels, remaining sentinel codes, changed reference categories, and unexplained new missing values are warning signs that preprocessing is not behaving as specified.
A common mistake is to assume that setting the same random seed guarantees identical MICE inputs. The seed controls random-number use; it does not fix a factor whose levels depend on row order, or a transformation whose constants are recalculated differently. The correction is to make preprocessing explicit and test it separately. Conversely, identical preprocessing does not guarantee identical imputations if software versions, imputation settings, or random streams differ.
There are two broad alternatives to implicit defaults. One is explicit rule-based preprocessing, which is generally easiest to audit and reproduce. Another is data-adaptive preprocessing, in which categories, cut points, or scaling constants are estimated from observed data. Data-adaptive choices can be appropriate, but must specify exactly which data determine them and how they are computed. Their implications for the imputation workflow require careful assessment; they are not interchangeable with fixed rules. This tutorial does not address whether a particular transformation is scientifically suitable or how to prevent information leakage.
Deterministic preprocessing also has limits. It cannot correct an inaccurate variable dictionary, establish that a missing-value code was used consistently, or guarantee that a chosen transformation fits the scientific question. Nor does an order-invariance test prove that all possible inputs are handled correctly. Test representative edge cases, including sentinel values, unexpected categories, missing values, and transformation-boundary values. A clear failure on an invalid input is often preferable to a plausible-looking but incorrect MICE data set.
Check Your Understanding
Answer each question, then compare your reasoning with the solutions.
- A variable uses 999 to mean “not measured.” What must be specified before recoding it, and why is replacing the largest observed value not an equivalent rule?
- A factor’s levels are inferred from first appearance. Explain how shuffling rows could change a model’s reference category, and give one preventive rule.
- For \(x=4\), calculate \(z=(x-a)/b\) when fixed constants are \(a=2\) and \(b=1\). What is the transformed value, and what condition on \(b\) is required for standardization by this formula?
- A log-transformed variable includes an observed value of 0. Is the transformation defined for that value? State a suitable diagnostic action without assuming an unreported recoding rule.
- Two runs use the same seed but different inferred factor levels. Does the seed establish that their MICE inputs are identical? Explain.
Solutions. (1) Confirm from the variable definition that 999 is a missing code, identify the variable or conditions to which it applies, and specify how all other values are treated. Replacing the largest observed value depends on the data and may remove a valid measurement; it does not implement the documented code. (2) The first encountered category can differ after shuffling, so the software may use a different level order and baseline. Declare the complete level order and reference category explicitly. (3) \(z=(4-2)/1=2\). The scale \(b\) must be positive and nonzero; standard deviation-based scaling conventionally uses a positive standard deviation. (4) No. The real-valued \(\log(x)\) is defined only for \(x>0\). Flag the zero or stop for review under a stated rule rather than silently inventing a recode. (5) No. A seed governs random-number use, not factor definitions. Different inferred levels mean the preprocessed representations, and potentially the model inputs, are not established to be identical.