Learn R Programming

rtemis (version 1.4.3)

reconstruct: Reconstruct Data from a Decomposition

Description

Encode data with a fitted decomposition and decode it back, giving the rank-k approximation of the input in its original units and column layout.

Usage

reconstruct(decom, x, execution_config = NULL, verbosity = 1L)

Value

A data.frame with the same columns as x, holding the reconstruction of the decomposed features.

Arguments

decom

Decomposition object.

x

Tabular data (data.frame, data.table, or tibble): Data to reconstruct. Its columns must match those decom was fitted on.

execution_config

Optional ExecutionConfig object: Threads for encoding x, as in apply_decomposition(). NULL uses the host's default worker count.

verbosity

Integer: Verbosity level.

Author

EDG

Details

The round trip apply_decomposition() leaves half-done. The reconstruction is x with everything outside the first k components removed, i.e. a denoised x. The residual, x - reconstruct(decom, x), is what the decomposition could not represent: rowMeans(residual^2) scores how anomalous each case is, and colMeans(residual^2) shows which variables the decomposition fails to capture.

Available for algorithms whose invertible trait is TRUE; see decomposition_traits. tSNE, UMAP and Isomap embed without an inverse, so there is no map back for them at all.

The reconstruction is in the units of the data as it was passed to decomp, with any centering or scaling the backend applied internally undone, so it can be compared with x directly. Columns the fit did not decompose (when decom@config@features is set) pass through unchanged and column order is preserved, so the result lines up cell for cell with x.

Examples

Run this code
x <- exc(iris, "Species")
iris_pca <- decomp(x, algorithm = "PCA", config = setup_PCA(k = 2L))
reconstructed <- reconstruct(iris_pca, x)
# What the two components could not represent, per case:
head(rowMeans((as.matrix(x) - as.matrix(reconstructed))^2))

Run the code above in your browser using DataLab