transform_vector_fp: Transform a Variable Using Fractional-Polynomial Powers

View source: R/transformations.R

transform_vector_fpR Documentation

Transform a Variable Using Fractional-Polynomial Powers

Description

Constructs fractional-polynomial design columns from specified powers, analogous to fracgen in Stata.

Usage

transform_vector_fp(
  x,
  power = 1,
  scale = 1,
  shift = 0,
  name = NULL,
  zero = FALSE,
  check_binary = TRUE
)

Arguments

x

A numeric vector of predictor values.

power

A numeric vector of FP powers. Default is 1 (linear). NA values are ignored; if all powers are NA, NULL is returned.

scale

Positive numeric scaling factor applied to x after shifting. Default 1 (no scaling). If NULL, the scale factor is estimated automatically. Scaling does not change the selected powers.

shift

shift required for shifting x to positive values. Default is 0, meaning no shift is applied. If NULL then the shift is estimated automatically using the Royston and Sauerbrei formula iff any x <= 0. Ignored when zero = TRUE.

name

character used to define names for the output matrix. Default is NULL, meaning the output will have unnamed columns.

zero

Logical indicating whether only values with x > 0 should be transformed, with exact-zero values retained as structural zeros. If TRUE, x must be nonnegative and must have been validated by the caller; negative values must be explicitly recoded if they should represent the zero group. No shift is applied when zero = TRUE. If FALSE (default), all values are shifted (if needed) to ensure positivity.

check_binary

a logical indicating whether or not input x is checked if it is a binary variable (i.e. has only two distinct values). The default TRUE usually only needs to changed when this function is to be used to transform data for predictions. See Details.

Details

This is a low-level utility for constructing fractional-polynomial design columns from specified powers. It does not perform model or power selection. Most users should specify continuous variables with fp() inside an mfp2() formula instead.

The FP transformation constructs design columns as follows. For each power pi in power = (p1, p2, ..., pn) it computes x^pi and returns the collection as a matrix. The input may be shifted and scaled before transformation. Centering, if desired, should be applied separately after transformation (see the Data processing section).

A special case are repeated powers, i.e. when some pi = pj. In this case, the fp transformations are given by x^pi and x^pi * log(x). In case more than 2 powers are repeated they are repeatedly multiplied with log(x) terms, e.g. pi = pj = pk leads to x^pi, x^pi * log(x) and x^pi * log(x)^2.

Note that the powers pi are assumed to be sorted. That is, this function sorts them, then proceeds to compute the transformation. For example, the output will be the same for power = c(1, 1, 2) and power = c(1, 2, 1). This is done to make sense of repeated powers and to uniquely define FPs. In case an ACD transformation is used, there is a specific order in which powers are processed, which is always the same (but not necessarily sorted). Thus, throughout the whole package powers will always be given and processed in either sorted, or ACD specific order and the columns of the matrix returned by this function will always align with the powers used throughout this package.

Binary variables are not transformed unless check_binary = FALSE. With check_binary = FALSE, the requested FP power is applied even when x has one or two distinct values; use the fitted shift and scale parameters when transforming prediction values. For model fit, check_binary should always be at its default value.

Value

Returns a matrix of transformed variable(s). The number of columns depends on the number of powers provided, the number of rows is equal to the length of x. The columns are sorted by increased power. If all powers are NA, then this function returns NULL. Binary x with check_binary = TRUE is returned untransformed as a one-column matrix.

Data processing

Variables are shifted and then scaled before any power transformation is applied. Shifting ensures positive values; scaling avoids numerically extreme magnitudes. Scaling does not change the selected powers.

Centering is not performed by this function. When centering is desired, it should be applied after transformation, because centering before transformation alters the correlation structure among predictors. In mfp2(), centering is applied only to the final selected model when center = TRUE. See Sauerbrei et al (2006) for discussion.

If zero = TRUE, x must be nonnegative. Exact-zero values are not shifted and their transformed components remain zero; transformation is applied only to values with x > 0. Structural-zero membership is determined before the power transformation, so a positive value that transforms to zero is still part of the positive component. For performance, this low-level helper assumes that callers have already enforced the nonnegative-input contract. The public fitting and prediction interfaces perform that validation once, before any transformation work.

Domain restrictions

Fractional-polynomial transformations are applied after shifting and scaling. Users should ensure that the shifted and scaled values are in the domain required by the specified powers. In particular, powers involving logarithms require strictly positive input. This includes power = 0, which represents log(x), and repeated powers such as c(2, 2), where the second transformed variable is proportional to x^2 * log(x).

Negative powers are undefined at zero, and non-integer powers are not real-valued for negative input. Therefore, when using powers such as 0, -1, -0.5, 0.5, or repeated powers, the values of (x + shift) / scale should be strictly positive unless zero = TRUE.

Positive integer powers such as 1, 2, and 3 can be evaluated at zero and negative values, provided they are not part of a repeated-power FP basis.

These low-level transformation functions assume that the supplied x, shift, scale, and power values are compatible. If incompatible values are supplied, the transformation may produce non-finite values or fail with an error from the underlying numerical code. In normal use through mfp2(), shift and scale parameters are estimated and checked during model fitting. During prediction, predict.mfp2() checks the fitted powers and stops with an informative error if new data fall outside the required transformation domain.

If zero = TRUE, exact-zero values are treated structurally: they are not shifted, and their transformed continuous components are set to zero.

References

Sauerbrei, W., Meier-Hirmer, C., Benner, A. and Royston, P., 2006. Multivariable regression model building by using fractional polynomials: Description of SAS, STATA and R programs. Comput Stat Data Anal, 50(12): 3464-85.

Examples

z = 1:10
transform_vector_fp(z)

mfp2 documentation built on Nov. 14, 2026, 1:06 a.m.