Why a Derived-Feature Mapping Must Be Saved
A model that uses cluster membership or principal-component scores depends on more than its fitted coefficients. It also depends on how the input variables were prepared and on the fitted clustering or PCA transformation. If those steps are estimated again when new records arrive, the resulting predictors may not mean the same thing as they did during model development.
For example, a cluster label is not a permanent property of a record. It is the result of applying a specified distance rule to inputs expressed on a specified scale, using a particular set of cluster centers. Likewise, a PCA score is a projection using particular centering, scaling, loading, ordering, and sign conventions. A reusable mapping preserves those choices.
This tutorial focuses on documenting and serializing those choices in R, then applying them consistently to future observations. The objective is not to refit the feature-generation procedure, but to make its fitted mapping available alongside the prediction model. This practice is aligned with the SOA Exam PA (Predictive Analytics) syllabus.
What to Preserve
A saved mapping should be sufficient to answer two questions for every future record: which original inputs are required, and exactly what operations turn those inputs into the derived features? Store the fitted values, not merely the instructions to estimate them again.
- Input schema: required variable names, order, units, data types, and any permitted categorical levels.
- Preprocessing: fitted imputation values, transformation choices, centering values, scale values, and the policy for missing or out-of-range values.
- Clustering: the stored centers in the coordinates in which distances are calculated, the distance measure, the number and order of clusters, and a deterministic rule for ties.
- PCA: the variables and their order, centering and scaling settings, loading matrix, component order, signs, and the selected components to return.
- Provenance: a mapping version, training-data identifier or date, fitting choices, and relevant software information. A random seed can help document how a stochastic fit was obtained, but it does not replace the fitted centers or loadings.
The input order matters because a loading vector or center is interpreted position by position. Variable names alone do not protect against a reordered matrix unless the application code checks names and explicitly restores the required order. Units and transformations matter for the same reason: a value measured in dollars cannot silently be substituted for one measured in thousands of dollars.
Worked Example: Reusing a Standardization Mapping
A hypothetical underwriting model uses two numeric inputs. During development, the stored mean and standard deviation for annual service cost are 10 thousand dollars and 2 thousand dollars. The future record has annual service cost of 13 thousand dollars. Apply the saved standardization rule and identify what must be saved.
Method. Use the development-data mean and standard deviation, with \(z=(x-\bar{x})/s\). The mean and standard deviation are already fitted mapping parameters; they are not recomputed from future records.
The standardized value is 1.5. The required stored information includes the input name, its unit (thousands of dollars), the development mean 10, the development standard deviation 2, and the rule of subtracting the mean and dividing by the standard deviation. It should also record how missing values are handled, if missing values are possible.
Check. Reversing the transformation gives \(10+1.5(2)=13\), the original cost. A common mistake is to calculate a new mean and standard deviation from the future batch. That changes the coordinate system, so the value 1.5 may no longer be obtained and may no longer have the same interpretation.
Saving and Applying Cluster Assignments
For a clustering method based on nearest centers, each record is assigned to the center with the smallest distance in the feature space used when fitting. If the inputs were standardized before fitting, future inputs must be standardized with the stored training means and standard deviations before distances are calculated. The centers must be retained in that same standardized coordinate system.
For \(K\) stored centers \(c_1,\ldots,c_K\), the assignment for a transformed record \(z\) under Euclidean distance is the index of the closest center:
Here, \(\|z-c_k\|_2\) is Euclidean distance. The formula specifies an assignment rule only when the distance metric and input representation are also fixed. If two centers are equally close, an implementation needs a deterministic tie rule; for example, assign to the lowest stored center index. The exact rule should be documented, even if ties are unlikely.
Worked Example: Assigning a Future Record to a Stored Center
A hypothetical segmentation procedure was fitted to two standardized measurements. Its two stored centers are \(c_1=(0,0)\) and \(c_2=(3,4)\). A future record, after applying the saved preprocessing parameters, is \(z=(2,1)\). Assign it using Euclidean distance.
Method. Calculate the distance from the transformed record to each stored center, then choose the center with the smaller distance. The input is already stated in the coordinates used to fit the centers.
Since \(\sqrt{5}<\sqrt{10}\), assign the record to cluster 1. The comparison can also be made using squared distances, 5 and 10, because squaring preserves the order of nonnegative distances. The result is checked by confirming that the selected center has the smaller distance.
A common mistake is to compare the raw future values with centers that were fitted to standardized values. The method avoids that error by requiring the preprocessing step to be applied first and by storing centers in the same transformed coordinate system.
Saving and Applying PCA Scores
PCA scores are computed by centering and, when specified, scaling the input variables, then projecting the resulting vector onto stored loading vectors. In R, a fitted object from prcomp retains its fitted centering, scaling, and rotation information. The predict method applies the fitted transformation to new data. If preprocessing was performed separately before PCA, those separate parameters and steps must also be retained and applied first.
Names and order of variables must match those used to fit the PCA. The selected component columns must also be fixed. PCA loading signs can be reversed without changing the underlying component direction, but the resulting scores change sign. Thus, a saved mapping must preserve the signs and ordering used by the prediction model; a newly refitted PCA is not an interchangeable substitute.
Worked Example: Calculating a Saved PCA Score
A hypothetical model uses the first principal-component score from two measurements. Their saved development means are 10 and 6, and their saved standard deviations are both 2. The stored first-component loading vector, in the order of the two inputs, is \((0.8,0.6)\). A future record has values 14 and 4. Calculate its score.
Method. Standardize each input with its saved mean and standard deviation, then take the dot product with the stored loading vector.
The future record's first-component score is 1.0. The calculation is checked by confirming that both standardized inputs use the appropriate variable-specific mean and scale, and that the loading order matches the input order. A common mistake is to multiply the raw values by the loadings, which would give \(0.8(14)+0.6(4)=13.6\), not the score defined by the fitted PCA mapping.
Why the PCA Projection Formula Holds
A loading vector gives the direction of a component in the transformed predictor space. The score is the coordinate of the centered and, if required, scaled observation in that direction. For a transformed observation \(z\) and a unit loading vector \(q\), the scalar projection onto that direction is the dot product \(z^\mathsf{T}q\). This follows from the geometric projection formula: the signed length of the projection is the length of \(z\) multiplied by the cosine of the angle between \(z\) and \(q\). Since \(q\) has length 1, that value is \(z^\mathsf{T}q\).
For a matrix of transformed observations \(Z\) and a stored loading matrix \(Q\), the corresponding score matrix is \(ZQ\). Each column of \(Q\) supplies one component direction, so each column of \(ZQ\) contains the projections onto that direction. This explains why changing the scaling, order, or signs in \(Q\) changes the feature mapping, even if a refitted PCA describes a similar low-dimensional structure.
Packaging a Mapping in R
A practical R workflow is to place the fitted parameters and explicit conventions in a named list, then serialize that list with saveRDS. For example, a bundle can contain a version label, expected input names and units, preprocessing parameters, stored cluster centers, the distance and tie rules, a PCA object or loading matrix, selected component names, and fitting metadata. readRDS restores the saved object for application.
When a fitted prcomp object is retained, predict(pca_fit, newdata) applies its fitted centering, scaling, and rotations, provided newdata has the required variables in the expected form. For an externally standardized PCA, apply the saved external transformation first. Similarly, a clustering package's prediction method should be used only if it implements the same documented assignment rule; otherwise calculate distances to the saved centers directly.
A safe application routine checks input names, rejects or explicitly handles missing required variables, orders columns to match the saved schema, applies only stored transformations, and returns derived features with stable names. It should not estimate means, standard deviations, centers, or loadings from the incoming records. Keep the mapping and the prediction model linked by a version identifier so that the model is not accidentally paired with a different mapping.
Save input names, order, units, preprocessing parameters, fitted centers or loadings, selected outputs, and explicit rules for missing values and ties.
Store the mapping as an R object using saveRDS, with a version and provenance fields. Preserve the fitted values rather than only the fitting call.
At prediction time, validate the input schema and use the saved transformation parameters, cluster assignment rule, or PCA projection.
Apply the saved object to a small set of known records and compare the derived features with stored expected results before using it operationally.
Common Failure Modes
Serialization preserves an object; it does not by itself guarantee that the correct object is being applied correctly. Common failures include re-estimating preprocessing on a new batch, changing units, supplying columns in a different order, omitting a factor level, applying PCA loadings with a different sign convention, or using centers expressed on a different scale. Any of these can alter a derived predictor while leaving the downstream prediction code apparently unchanged.
A useful verification is a parity check: take selected records for which the intended derived features are known, run them through the saved mapping, and confirm that the outputs match within a stated numerical tolerance. Also check output names, dimensions, missing-value behavior, and cluster labels. A mapping should fail clearly when required inputs do not satisfy its schema rather than silently producing plausible but inconsistent features.
Check Your Understanding
For each problem, assume the stated saved mapping is the one used by the prediction model. Do not refit it on future records.
- A saved transformation has mean 20 and standard deviation 4. A future value is 26. Calculate its standardized value. (Answer: 1.5)
- Two stored one-dimensional centers are 2 and 8. A transformed future value is 6. Using nearest Euclidean center and lowest-index tie-breaking if needed, which center is selected? (Answer: Center 2, since the distances are 4 and 2.)
- A saved PCA uses means \((5,10)\), standard deviations \((2,5)\), and first loading vector \((0.6,0.8)\). A future record is \((7,5)\). Calculate its first-component score. (Answer: \(0.6(1)+0.8(-1)=-0.2\))
- A cluster model was fitted after standardizing two variables with stored means \((10,20)\) and standard deviations \((2,4)\). A future record is \((12,24)\). What transformed point must be compared with the stored centers? (Answer: \((1,1)\))
- A saved PCA used input order A, then B, and its first loading vector is \((0.8,0.6)\). For transformed values \(z_A=1\) and \(z_B=2\), calculate the score. Why must the order be preserved? (Answer: \(0.8(1)+0.6(2)=2.0\); swapping inputs would produce a different score.)