Code Help

  • methods {utils}
  • getAnywhere {utils}
[1] var.test.default* var.test.formula*
see '?methods' for accessing help and source code

A single object matching 'var.test.default' was found
It was found in the following places
  registered S3 method for var.test from namespace stats
with value

function (x, y, ratio = 1, alternative = c("two.sided", "less", 
    "greater"), conf.level = 0.95, ...) 
    if (!((length(ratio) == 1L) && is.finite(ratio) && (ratio > 
        stop("'ratio' must be a single positive number")
    alternative <- match.arg(alternative)
    if (!((length(conf.level) == 1L) && is.finite(conf.level) && 
        (conf.level > 0) && (conf.level < 1))) 
        stop("'conf.level' must be a single number between 0 and 1")
    DNAME <- paste(deparse(substitute(x)), "and", deparse(substitute(y)))
    if (inherits(x, "lm") && inherits(y, "lm")) {
        DF.x <- x$df.residual
        DF.y <- y$df.residual
        V.x <- sum(x$residuals^2)/DF.x
        V.y <- sum(y$residuals^2)/DF.y
    else {
        x <- x[is.finite(x)]
        DF.x <- length(x) - 1L
        if (DF.x < 1L) 
            stop("not enough 'x' observations")
        y <- y[is.finite(y)]
        DF.y <- length(y) - 1L
        if (DF.y < 1L) 
            stop("not enough 'y' observations")
        V.x <- var(x)
        V.y <- var(y)
    ESTIMATE <- V.x/V.y
    PARAMETER <- c(`num df` = DF.x, `denom df` = DF.y)
    PVAL <- pf(STATISTIC, DF.x, DF.y)
    if (alternative == "two.sided") {
        PVAL <- 2 * min(PVAL, 1 - PVAL)
        BETA <- (1 - conf.level)/2
        CINT <- c(ESTIMATE/qf(1 - BETA, DF.x, DF.y), ESTIMATE/qf(BETA, 
            DF.x, DF.y))
    else if (alternative == "greater") {
        PVAL <- 1 - PVAL
        CINT <- c(ESTIMATE/qf(conf.level, DF.x, DF.y), Inf)
    else CINT <- c(0, ESTIMATE/qf(1 - conf.level, DF.x, DF.y))
    names(STATISTIC) <- "F"
    names(ESTIMATE) <- names(ratio) <- "ratio of variances"
    attr(CINT, "conf.level") <- conf.level
    RVAL <- list(statistic = STATISTIC, parameter = PARAMETER, 
        p.value = PVAL, = CINT, estimate = ESTIMATE, 
        null.value = ratio, alternative = alternative, method = "F test to compare two variances", = DNAME)
    attr(RVAL, "class") <- "htest"
<bytecode: 0x000000002707ca58>
<environment: namespace:stats>

A single object matching 'var.test.formula' was found
It was found in the following places
  registered S3 method for var.test from namespace stats
with value

function (formula, data, subset, na.action, ...) 
    if (missing(formula) || (length(formula) != 3L) || (length(attr(terms(formula[-2L]), 
        "term.labels")) != 1L)) 
        stop("'formula' missing or incorrect")
    m <- = FALSE)
    if (is.matrix(eval(m$data, parent.frame()))) 
        m$data <-
    m[[1L]] <- quote(stats::model.frame)
    m$... <- NULL
    mf <- eval(m, parent.frame())
    DNAME <- paste(names(mf), collapse = " by ")
    names(mf) <- NULL
    response <- attr(attr(mf, "terms"), "response")
    g <- factor(mf[[-response]])
    if (nlevels(g) != 2L) 
        stop("grouping factor must have exactly 2 levels")
    DATA <- setNames(split(mf[[response]], g), c("x", "y"))
    y <-"var.test", c(DATA, list(...)))
    y$ <- DNAME
<bytecode: 0x0000000027072588>
<environment: namespace:stats>

  • tools:::Rd2HTML
  • utils:::.getHelpFile
var.test R Documentation

F Test to Compare Two Variances


Performs an F test to compare the variances of two samples from normal populations.


var.test(x, ...)

## Default S3 method:
var.test(x, y, ratio = 1,
         alternative = c("two.sided", "less", "greater"),
         conf.level = 0.95, ...)

## S3 method for class 'formula'
var.test(formula, data, subset, na.action, ...)


x, y

numeric vectors of data values, or fitted linear model objects (inheriting from class “lm”).


the hypothesized ratio of the population variances of x and y.


a character string specifying the alternative hypothesis, must be one of “two.sided” (default), “greater” or “less”. You can specify just the initial letter.


confidence level for the returned confidence interval.


a formula of the form lhs ~ rhs where lhs is a numeric variable giving the data values and rhs a factor with two levels giving the corresponding groups.


an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula).


an optional vector specifying a subset of observations to be used.


a function which indicates what should happen when the data contain NAs. Defaults to getOption(“na.action”).

further arguments to be passed to or from methods.


The null hypothesis is that the ratio of the variances of the populations from which x and y were drawn, or in the data to which the linear models x and y were fitted, is equal to ratio.


A list with class “htest” containing the following components:


the value of the F test statistic.


the degrees of the freedom of the F distribution of the test statistic.


the p-value of the test.

a confidence interval for the ratio of the population variances.


the ratio of the sample variances of x and y.


the ratio of population variances under the null.


a character string describing the alternative hypothesis.


the character string “F test to compare two variances”.

a character string giving the names of the data.

See Also

bartlett.test for testing homogeneity of variances in more than two samples from normal distributions; ansari.test and mood.test for two rank based (nonparametric) two-sample tests for difference in scale.


x <- rnorm(50, mean = 0, sd = 2)
y <- rnorm(30, mean = 1, sd = 1)
var.test(x, y)                  # Do x and y have the same variance?
var.test(lm(x ~ 1), lm(y ~ 1))  # The same.