Dimension Reduction - Principal Components Analysis Biplot

From Q
Jump to: navigation, search

Principal Components Analysis Biplots (PCA Biplot) is a principal components analysis of the table itself, where each column is a variable. It produces a scatterplot showing how the rows (columns) differ in terms of the column (rows) attributes. For more information on how to interpret the chart see Principal Components Analysis Biplot

Example

Options

Input table The name of a table containing data to be analyzed. The table should only contain a single statistic (e.g., Total %).

Paste or type table Opens up a blank spreadsheet into which tabular data can be manually entered or pasted. This is an alternative to Input table.

Normalization The method used to normalize the coordinates of the chart (this changes the chart, but not the outputs of analysis). The default method is Principal which charts the principal coordinates (i.e., the standard coordinates multiplied by the singular values). Row principal and Column principal chart the standard coordinates of the columns (rows) against the principal coordinates.

Output:

Scatterplot
Moonplot
Text

Rows to ignore, Columns to ignore The names of any rows or columns to be removed from the table prior to analysis.

Row title, Column title Title of the row/column attributes.

Row attribute color, Column attribute color Color of the points shown in the labelled scatterplot.

Code

var allow_control_groups = Q.fileFormatVersion() > 10.9; // Group controls for Displayr and later versions of Q
form.setHeading('PCA Biplot of a Table');
function isEmpty(x) { return (x == undefined || x.getValue() == null && (x.getValues() == null || x.getValues().length == 0)) }
function isBlankSheet(x) { return (x.getValue() == null || x.getValue().length == 0) }

var controls = [];

if (!form.dataEntry)
{
    var tableInput = form.dropBox({label: "Input table", types:["table", "RItem"], name: "formTableToAnalyse",  multi : false});
    controls.push(tableInput);
}
else
{
    var tableInput = form.dropBox({label: "Input table", types:["table", "RItem"], name: "formTableToAnalyse",  multi : false, required: false,
                                   prompt: "Table of data to analyze"});
    var pasteInput = form.dataEntry({label: "Paste or type table", name: "formEnteredData", prompt: "Opens a spreadsheet into which you can paste data."})
    if (!allow_control_groups || !isEmpty(tableInput) || isBlankSheet(pasteInput))
        controls.push(tableInput);
    if (!allow_control_groups || !isBlankSheet(pasteInput) || isEmpty(tableInput))
        controls.push(pasteInput);
}
var norm = form.comboBox({label: "Normalization", alternatives:["Principal", "Row principal", "Column principal", "Symmetrical", "None"], name: "formNormalization",
                          multi : false, default_value: "Principal", prompt: "Scaling of coordinated by singular values"});
controls.push(norm);
var outOpt = form.comboBox({label: "Output", alternatives:["Scatterplot", "Moonplot", "Text"], name: "formOutput",  multi : false, default_value: "Scatterplot"});
controls.push(outOpt);
var rowIgnore = form.textBox({label: "Rows to ignore", type: "text", default_value: "NET, Total, SUM", name: "formIgnoreRows", required: false,
                              prompt: "Rows of the table that are excluded from the analysis"});
controls.push(rowIgnore);
var colIgnore = form.textBox({label: "Columns to ignore", type: "text", default_value: "NET, Total, SUM", name: "formIgnoreColumns", required: false,
                              prompt: "Columns of the table that are excluded from the analysis"});
controls.push(colIgnore);
var rowTitle = form.textBox({label: "Row title", type: "text", default_value: "Rows", name: "formRowTitle", required: false});
controls.push(rowTitle);
var colTitle = form.textBox({label: "Column title", type: "text", default_value: "Columns", name: "formColumnTitle", required: false});
controls.push(colTitle);
if (outOpt.getValue() == "Scatterplot")
{
    var rowColor = form.colorPicker({label: "Row attributes color", name: "rowColor", default_value:"#5B9BD5"});
    controls.push(rowColor);
    var colColor = form.colorPicker({label: "Column attributes color", name: "colColor", default_value:"#ED7D31"});
    controls.push(colColor);
}
form.setInputControls(controls);
library(flipTransformations)
library(flipDimensionReduction)

x <- get0("formTableToAnalyse")
if (is.null(x))
    x <- flipTransformations::ParseEnteredData(formEnteredData, want.data.frame = TRUE, want.col.names = TRUE, want.row.names = TRUE)

attr(x, "row.column.names") <- c(formRowTitle, formColumnTitle)
row.color <- ifelse(formOutput=="Scatterplot", rowColor, "")
col.color <- ifelse(formOutput=="Scatterplot", colColor, "")

biplot <- PrincipalComponentsBiplot(x,
                                    normalization = formNormalization,
                                    output = formOutput,
                                    row.names.to.remove = formIgnoreRows,
                                    column.names.to.remove = formIgnoreColumns,
                                    row.color = row.color,
                                    col.color = col.color)

See Also