permtest: Permutation Test on Long-Format Data

View source: R/permtest.R

permtestR Documentation

Permutation Test on Long-Format Data

Description

Performs a permutation test on long-format data to evaluate differences between two groups using a specified test statistic.

Usage

permtest(x, y = NULL, statistic, alternative = "two.sided",
   Q = seq(0.1, 0.9, by = 0.1), qt = 7, R = 10000)

Arguments

x

A data frame or matrix containing the data in long format.

y

An optional second data frame or matrix. If provided, the data will be combined with x and converted to long format.

statistic

The function name to compute the statistic of interest.

alternative

Type of alternative hypothesis: "two.sided", "less", or "greater". The default is "two.sided", corresponding to a two-tailed test.

Q

A vector of quantile probabilities specifying the quantiles to be compared. The default is seq(0.1, 0.9, by = 0.1), corresponding to the 10th through 90th percentiles (P10 to P90).

qt

An integer between 0 and 9 specifying the quantile calculation method. The default is 7.

R

An integer indicating the number of permutations. The default is 10000.

Details

The function allows researchers to perform robust statistical testing by utilizing permutations. This approach does not rely on distributional assumptions and is particularly useful when the sample size is small or the data distribution is unknown. The test generates an empirical distribution of the test statistic by repeatedly permuting group labels and recalculating the statistic, providing p-values based on these permutations.

Value

A list containing the following components:

t0

The estimated statistics of the differences.

t

A matrix of the permuted values of the statistics.

pval

A numeric vector of p-values for each statistic.

alternative

The specified alternative hypothesis.

R

The number of permutations.

pvalsk

A numeric vector of p-values for skewness and kurtosis with two-sided alternative. These are used to check the similarity of distributions to decide whether to use a bootstrap and a permutation test for inference.

call

The matched call.

Author(s)

Zeynel Cebeci, A. Firat Ozdemir, Engin Yildiztepe

See Also

bootstrap

Examples


# Group1 normal, Group 2 right skewed with equal means and different variances
set.seed(1199)  
grp1 <- rnorm(20, 45, 5)
grp2 <- c(rnorm(10, 45, 10), rnorm(10, 52, 20))
df <- data.frame(cbind(grp1=grp1, grp2=grp2))
head(df)
bivarplot(df)

# Reshape the data into long format
df <- wide2long(df)
head(df)

# Differences between the basic stats
calcstatdif(df)

# Permutation test for the differences between the basic stats of two groups
result <- permtest(df, statistic = calcstatdif, alternative = "two.sided", R = 500)
result$pval

# Permutation with custom statistics
# A custom function to compute the differences between the group means
meancomp <- function(x, ...){
   meandif <- diff(rev(tapply(x[,1], x[,2], mean)))
   return(meandif)
}

# Permutation test with meancomp function
result <- permtest(x = df, statistic = meancomp, alternative="less", R=500)
result$pval


groupcompare documentation built on June 26, 2025, 1:08 a.m.