# Calculation - Variance - Variance

This tool can be used to compute the variance of the numbers in a table, the variance of matching elements of several tables, or the case-wise variance of several variables in your data set. The result will depend on which items in your document you select before using this tool:

- If a single table is selected, the result will show the variance of the numbers in that table.
- If two or more tables are selected, the result will show the variance of the matching parts of those tables. Matching is done based on common row and column labels.
- If two or more variables are selected, this tool will create a new variable that contains the variance of those variables for each case.

## Usage

- Select one or more tables on your page, two or more variables in the Variables and Questions tab , or a single question in the Variables and Questions tab
- From the toolbar, select
**Automate > Browse Online Library >****Calculation > Variance > Variance**

## Example

You can use this tool to compute the variance of the numbers in a single table. For example, this table shows the average number of orders per month that people placed at a selection of restaurants.

The result of applying **Variance** is:

Note that by default, the variance calculation will exclude any `SUM` or `NET` rows or columns in the table, and you can control which rows or columns to exclude in the options (see below).

If you have many tables that share common row and column labels, then you can use this tool to compute the variance for each element that matches between those tables. For example, these tables show the average orders placed at a selection of restaurants. They have common row labels, although a mistake has led the rows of the last table to be out of order:

Applying **Variance** to these tables produces a new table that contains the variance of each matching item among the four tables:

Note that the tool has correctly matched up all of the common rows, despite the rows in the last table being out of order.

## Options

The output showing the results of the calculation has the following options available in the Object Inspector.

**Input** The tables to be used in the calculation.

**Calculate for inputs with incomplete data** If this option is checked, than any missing values in any of the inputs will be ignored in the calculation. If unchecked, then missing values are not removed before calculation and will propagate as missing values in the output.

**Variance formula / Standard Deviation formula** This option allows you to choose whether the Population or Sample formula is used to compute the Variance or Standard Deviation (see below).

**Automatically match elements** Only shown when there are multiple inputs to **Input**. This controls how and whether matching is done between the labels of the inputs. The default, "Yes - hide unmatched", will look for matching labels in the rows and columns of the inputs before proceeding with the calculation, and any rows/columns that are not contained in all the inputs will not be included in the output. See the **Example**. For a full description of the matching algorithm, see the **Technical Details**. "Yes - show unmatched" will also perform matching, but any unmatched rows (columns) will appear in the output as rows (columns) of all missing values. Selecting "No" for this option will cause any labels in the data to be ignored and not perform any matching. Selecting "Custom" will bring up two additional controls that allow for specifying the matching behavior for rows and columns separately.

**Match rows** Only shown if **Automatically match elements** is set to "Custom". Specifies the matching behavior when comparing row labels of the inputs. "Yes - show unmatched" and "Yes - hide unmatched" look for exact matches in the row labels in the inputs. "Fuzzy - show unmatched" and "Fuzzy - hide unmatched" perform fuzzy matching so that labels that differ only by a single character are considered to be a match.

**Match columns** Only shown if **Automatically match elements** is set to "Custom". The options are the same as **Match rows**, but control the matching between columns.

**Rows to exclude** Here you can type in row labels that should be excluded from the calculation.

**Columns to exclude** As above, but for columns.

## Technical Details

The default option is to compute the variance or standard deviation using the *sample* variance formula rather than the *population* variance formula. You have the option to choose between these two formulas so that you can apply whichever is relevant to your calculation. If you are a Q user, or are comparing results to those obtained in Q, please note that this default is different to Q's **Insert Ready-Made Formulas > Variance** and **Insert Ready-Made Formulas > Standard Deviation** which always use the *population* formula. In both cases, the standard deviation is the square root of the variance, and the two variance formulas are:

**Sample Variance**

[math]\displaystyle{ \sigma_{sample}^2=\frac{\sum^n_{i=1}(x_i -\frac{\sum^n_{i=1}x_i }{n})^2}{n-1} }[/math]

**Population Variance**

[math]\displaystyle{ \sigma_{population}^2=\frac{\sum^n_{i=1}(x_i -\frac{\sum^n_{i=1}x_i }{n})^2}{n} }[/math]

When there are multiple inputs, inputs that contain only a single row (column) may be recycled to a matrix/table with the same number of rows (columns) as the other inputs. For example, if the supplied inputs are a table with three rows and two columns and another table with two rows and a single column, the single column will be expanded by rows into a table with three rows and two columns with each row identical to the original column.

When **Automatically match elements** is set to **Yes - show unmatched** or **Yes - hide unmatched**,
both exact matches and fuzzy matches (as described above) are considered, and the order of elements may be permuted so that the names match. It also may transpose an input if, for example, the column names of one input match the row names of another input.

```
includeWeb('QScript Functions for Calculations');
includeWeb('QScript Functions to Generate Outputs');
applyCalculationOnSelections('Variance');
```

## See also

- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.

Calculation

Q Technical Reference

Q Technical Reference

Q Technical Reference > Setting Up Data > Creating New Variables

Q Technical Reference > Updating and Automation > Automation Online Library

Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library