pca_warper: Principal components transformation of feature space

View source: R/pca_warper.R

pca_warperR Documentation

Principal components transformation of feature space

Description

This function generates a warper object based on a principal component analysis of the features (or more precisely, of selected features xvars).

Usage

pca_warper(
  xdata,
  xvars,
  wvars = "PC",
  yvar,
  uvars = NULL,
  center = TRUE,
  scale = TRUE,
  positive = TRUE,
  title = wvars
)

Arguments

xdata

A data frame containing the observations in the original feature space.

xvars

A character vector with the column names of features in xdata that should be transformed.

wvars

A character vector with the names of all transformed predictors, or (more often) a simple character string to which the numbers 1, ... will be appended as needed.

yvar

Name of the response variable (not to be transformed)

uvars

Names of additional variables that should remain untouched.

scale, center

Logical arguments indicating whether the data should be centered and then scales. Both should be turned on (TRUE), which is the default behaviour.

positive

Logical argument (default: TRUE) indicating whether the signs of the loadings should be adjusted so that the most strongly weighted PC gets a positive sign.

title

Optional name of the transformation, may be used for printing summaries or for plotting.

Details

There should be no overlap between xvars, uvars and yvar. The rotation matrix ⁠$full_rotation⁠ in the results object is of size length(xvars)+length(uvars) and includes an identity transformation diag(length(uvars)) for the features in uvars.

Value

An object of class warper, rotation_warper and pca_warper.

Examples

### Create principal components warper for Maipo data set:
xvars <- c(paste("ndvi0", 1:8, sep = ""), paste("ndwi0", 1:8, sep = ""),
           paste("b", outer(1:8,2:7,paste,sep = ""), sep = ""))
fo <- as.formula(paste("class ~", paste(xvars, collapse=" +" )))
d <- maipofields

wrp <- pca_warper(d, xvars = xvars, yvar = "class")
plot(wrp)
round(wrp$pca$rotation[,1:3], digits = 2)
summary(wrp)

# Create a data frame with transformed data:
wd <- warp(d, warper = wrp)
summary(wd)

# Backtransform it, should be identical to d:
d2 <- unwarp(wd, warper = wrp)
all.equal(d, d2[,colnames(d)])
# Default tolerance works for this data set, but you may have to use
# e.g. tol = 10^(-6) for less well conditioned data sets and
# transformations.

alexanderbrenning/wiml documentation built on Sept. 29, 2023, 4:45 a.m.