Learn R Programming

nptest (version 1.2)

np.aov.test: Nonparametric One-Way and RM ANOVA Tests

Description

Assuming a one-way (fixed effects) ANOVA model of the form $$Y_{ij} = \mu + \tau_j + \epsilon_{ij}$$ or a one-way repeated measures ANOVA model of the form $$Y_{ij} = \mu + \beta_i + \tau_j + \epsilon_{ij}$$ this function implements permutation tests of \(H_0: (\forall j) \tau_j = \tau\) versus \(H_1: (\exists j) \tau_j \neq \tau\). Note that \(\mu\) is the overall mean/median ignoring block and group, \(\beta_i\) is the \(i\)-th subject's block effect, \(\tau_j\) is the \(j\)-th group's treatment effect, and \(\epsilon_{ij}\) is an error term with mean/median zero.

Usage

np.aov.test(x, groups, blocks = NULL, 
            var.equal = FALSE, median.test = FALSE, 
            R = 9999, parallel = FALSE, cl = NULL,
            perm.dist = TRUE, na.rm = TRUE)

Value

statistic

Test statistic value.

p.value

p-value for testing \(H_0: (\forall j) \tau_j = \tau\).

perm.dist

Permutation distribution of statistic.

repeated

Repeated-measures ANOVA?

var.equal

Assuming equal variances?

median.test

Testing the median?

R

Number of resamples.

method

Method used for permutation test. See Examples.

ngroups

Number of groups = nlevels(group)

nblocks

Number of blocks = nlevels(blocks) (if applicable)

Arguments

x

Numeric vector (or matrix) of data values (see Details).

groups

Factor vector giving the treatment group for each element/row of x.

blocks

Factor vector giving the block identification for each element/row of x.

var.equal

Logical indicating whether to treat the \(k\) group's variances as being equal.

median.test

Logical indicating whether the location test is for the median. Default is FALSE, i.e., \(\mu\) is the mean.

R

Number of resamples for the permutation test (positive integer).

parallel

Logical indicating if the parallel package should be used for parallel computing (of the permutation distribution). Defaults to FALSE, which implements sequential computing.

cl

Cluster for parallel computing, which is used when parallel = TRUE. Note that if parallel = TRUE and cl = NULL, then the cluster is defined as makeCluster(2L) to use two cores. To make use of all available cores, use the code cl = makeCluster(detectCores()).

perm.dist

Logical indicating if the permutation distribution should be returned.

na.rm

If TRUE (default), the arguments x and groups (and blocks if provided) are passed to the na.omit function to remove cases with missing data.

Author

Nathaniel E. Helwig <helwig@umn.edu>

Details

One-way ANOVA: the input x should be of length \(N = \sum_{j=1}^k n_j\) where \(n_j\) is size of \(j\)-th group.

RM ANOVA: the input x should be of length \(N = n k\) where \(n\) is number of blocks and \(k\) is number of groups.

For multivariate models, the input x should be a matrix with \(N\) rows and \(m\) columns, where each column has \(N = \sum_{j=1}^k n_j\) or \(N = n k\) observations.

References

Helwig, N. E. (2019a). Statistical nonparametric mapping: Multivariate permutation tests for location, correlation, and regression problems in neuroimaging. WIREs Computational Statistics, 11(2), e1457. doi: 10.1002/wics.1457

Helwig, N. E. (2019b). Robust nonparametric tests of general linear model coefficients: A comparison of permutation methods and test statistics. NeuroImage, 201, 116030. doi: 10.1016/j.neuroimage.2019.116030

See Also

plot.np.aov.test S3 plotting method for visualizing the results

Examples

Run this code

###***###   ONE-WAY ANOVA   ###***###

# data generation design
N <- 90
k <- 3
g <- factor(rep(LETTERS[1:k], each = N/k))
tau <- c(-1/2, 0, 1/2)
sd <- c(1/2, 1, 2)

# generate data
set.seed(0)
x <- rnorm(N, mean = tau[g], sd = sd[g]) 

# mean test with unequal variances (robust W statistic)
set.seed(1)
np.aov.test(x, g)

# mean test with equal variances (classic F statistic)
set.seed(1)
np.aov.test(x, g, var.equal = TRUE)

# median test with unequal variances (robust Kruskal-Wallis statistic)
set.seed(1)
np.aov.test(x, g, median.test = TRUE)

# median test with equal variances (classic Kruskal-Wallis test)
set.seed(1)
np.aov.test(x, g, var.equal = TRUE, median.test = TRUE)

# Kruskal-Wallis test (asymptotic p-value)
kruskal.test(x, g)


if (FALSE) {

###***###   REPEATED MEASURES ANOVA   ###***###

# data generation design
N <- 90
k <- 3
n <- 30
g <- factor(rep(LETTERS[1:k], each = N/k))
b <- factor(rep(paste0("sub", 1:n), times = k),
            levels = paste0("sub", 1:n))
tau <- c(-1/2, 0, 1/2)
sd <- c(1/2, 1, 2)

# generate random block effects
set.seed(773)
beta <- runif(30, -1, 1)

# generate data
set.seed(0)
x <- rnorm(N, mean = tau[g] + beta[b], sd = sd[g]) 

# mean test with unequal variances (robust W statistic)
set.seed(1)
np.aov.test(x, g, b)

# mean test with equal variances (classic F statistic)
set.seed(1)
np.aov.test(x, g, b, var.equal = TRUE)

# median test with unequal variances (robust Friedman statistic)
set.seed(1)
np.aov.test(x, g, b, median.test = TRUE)

# median test with equal variances (classic Friedman test)
set.seed(1)
np.aov.test(x, g, b, var.equal = TRUE, median.test = TRUE)

# Friedman test (asymptotic p-value)
friedman.test(x, g, b)
}

Run the code above in your browser using DataLab