Test - Variance - F-Test to Compare Two Variances

From Q
Jump to navigation Jump to search


Performs an F test to compare the variances of two samples, which are assumed to be normally distributed.

How to run this test

  1. In Displayr, go to Insert > More > Test > Variance > F-Test to Compare Two Variances. In Q, go to Create > Test > Variance > F-Test to Compare Two Variances
  2. Specify the two input variables under Inputs > F-Test to Compare Two Variances > Outcome and Inputs > F-Test to Compare Two Variances > Group. The Group variable should be categorical, but if it's numeric it will be converted.

Ftest.PNG

Example

An example output is shown below:

Options

Outcome The variable to analyse.
Group Variable specifying the group that each observation comes from. Where this variable is numeric, it is automatically grouped into two approximately-equally-sized groups, using the median.

Acknowledgements

Uses the var.test function from the stats R package.

Code

var heading_text = "F-Test to Compare Two Variances";
var plural_heading = heading_text.replace("Test", "Tests");
if (!!form.setObjectInspectorTitle)
    form.setObjectInspectorTitle(heading_text, plural_heading);
else
    form.setHeading(heading_text);

form.dropBox({label: "Outcome", types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
            name: "formOutcomeVariable", prompt: "Select the Variable to analyse"});
form.dropBox({label: "Group", types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
            name: "formGroupVariable", prompt: "Select the Variable specifying the group that each observation comes from"});
var formOutput = form.comboBox({label: "Output", alternatives: ["Summary", "R"],
                                name: "formOutput", default_value: "Summary", prompt: "Select the output type"});
if (formOutput.getValue() == "Summary")
{
    form.checkBox({label: "Variable names", name: "formNames", default_value: false,
                   prompt: "Display names instead of labels"});
    form.checkBox({label: "More decimal places", name: "formDecimals", default_value: false,
                   prompt: "Display numeric values with eight decimal places"});
}
library(flipTransformations)
library(flipFormat)

if (length(formOutcomeVariable) != length(formGroupVariable))
    stop("The outcome and group variables have different lengths. Please ensure that the variables are from the same data set or have the same length.")

data <- ProcessQVariables(QDataFrame(formOutcomeVariable, formGroupVariable))
if (is.factor(formOutcomeVariable))
    data[,1] <- AsNumeric(data[,1], binary=FALSE)
data[,2] <- CreatingBinaryVariableIfNecessary(data, names(data)[2])
data$subset <- QFilter
if (!is.null(QCalibratedWeight))
    data <- AdjustDataToReflectWeights(data, QCalibratedWeight)
formula <- QFormula(formOutcomeVariable ~ formGroupVariable)

test.output <- var.test(formula, alternative='two.sided', conf.level=.95, data=data, subset = subset)
variance.f.test <- if (formOutput == "Summary") {
    test.output$estimate <- NULL
    decimal.places <- if (formDecimals) 8 else NULL
    SignificanceTest(test.output, "F-Test to Compare Two Variances",
                     list(formOutcomeVariable, formGroupVariable),
                     filter = QFilter, weight = QCalibratedWeight,
                     show.labels = !formNames, decimal.places = decimal.places,
                     resample = TRUE)
} else
    test.output