swaprinc {swaprinc}R Documentation

Swap in Principal Components

Description

Compare a regression model using raw variables with another model where principal components are extracted from a subset of the raw independent variables, and a user-defined number of these principal components are then used to replace the original subset of variables in the regression model.

Usage

swaprinc(
  data,
  formula,
  engine = "stats",
  prc_eng = "stats",
  pca_vars,
  n_pca_components,
  norun_raw = FALSE,
  lpca_center = "none",
  lpca_scale = "none",
  lpca_undo = FALSE,
  gifi_transform = "none",
  gifi_trans_vars,
  gifi_trans_dims,
  no_tresp = FALSE,
  miss_handler = "none",
  model_options = "noaddpars",
  prcomp_options = "noaddpars",
  gifi_princals_options = "noaddpars",
  gifi_trans_options = "noaddpars"
)

Arguments

data

A dataframe

formula

A quoted model formula

engine

The engine for fitting the model. Options are 'stats' or 'lme4'.

prc_eng

Then engine or extracting principal components. Options are 'stats', 'Gifi', and 'stats_Gifi'. The stats_Gifi engine uses tidyselect::where(is.numeric) to select the pca_vars for stats::prcomp and -tidyselect::where(is.numeric) to select the pca_vars for Gifi::princals. Read Rossiter (2021) for more on princals.

pca_vars

Variables to include in the principal component analysis. These variables will be swapped out for principal components

n_pca_components

The number of principal components to include in the model. If using a complex prc_eng (i.e., stats_Gifi) then provide a named vector (i.e., n_pca_components = c("stats" = 2, "Gifi" = 3)).

norun_raw

Include regression on raw variables if TRUE, exclude if FALSE.

lpca_center

Center data as in the Step-by-Step PCA vignette (Harvey & Hanson, 2022). Only numeric variables will be included in the centering. Parameter takes values 'all' to center raw and pca variables, 'raw' to only center variables for the raw variable model fitting, 'pca' to only center pca_vars before pca regression model fitting, and 'none' to skip lpca centering.

lpca_scale

Scale data as in the Step-by-Step PCA vignette. Only numeric variables will be included in the scaling. Parameter takes values 'all' to scale raw and pca variables, 'raw' to only scale variables for the raw variable model fitting, 'pca' to only scale pca_vars before pca regression model fitting, and 'none' to skip lpca scaling.

lpca_undo

Undo centering and scaling of pca_vars as in the Step-by-Step PCA vignette.

gifi_transform

Use Gifi optimal scaling to transform a set of variables. Parameter takes values 'none', 'all', 'raw', and 'pca'

gifi_trans_vars

A vector of variables to include in the Gifi optimal scaling transformation

gifi_trans_dims

Number of dimensions to extract in the Gifi optimal scaling transformation algorithm

no_tresp

When set to TRUE, no_tresp (No transform response) will exclude the response variable from from pre-modeling and pre-pca transformations. Specifically, setting no_tresp to TRUE will exclude the response variable from the transformation specified in lpca_center and lpca_scale.

miss_handler

Choose how swaprinc handles missing data on the input data. Default is 'none'. Use 'omit' for complete case analysis.

model_options

Pass additional arguments to statistical modeling functions (i.e., stats::lm, stats::glm, lme4::lmer, lme4::glmer) Default is 'noaddpars' (no additional parameters)

prcomp_options

Pass additional arguments to stats::prcomp for prc_eng = 'stats' and prc_eng = 'stats_Gifi' call. Default is 'noaddpars' (no additional parameters)

gifi_princals_options

Pass additional arguments to Gifi::princals for prc_eng = 'Gifi' and prc_eng = 'stats_Gifi' call. Default is 'noaddpars' (no additional parameters)

gifi_trans_options

Pass additional arguments to Gifi::princals for gifi_transform. Default is 'noaddpars' (no additional parameters)

Value

A list with fitted models

References

  1. Rossiter, D. G. Nonlinear Principal Components Analysis: Multivariate Analysis with Optimal Scaling (MVAOS). (2021) https://www.css.cornell.edu/faculty/dgr2/_static/files/R_html/NonlinearPCA.html

  2. Harvey, D. T., & Hanson, B. A. Step-by-Step PCA. (2022) https://cran.r-project.org/package=LearnPCA/vignettes/Vig_03_Step_By_Step_PCA.pdf

Examples

data(iris)
res <- swaprinc(iris,
"Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width",
pca_vars = c("Sepal.Width", "Petal.Length", "Petal.Width"),
n_pca_components = 2)

[Package swaprinc version 1.0.1 Index]