Skip to contents

This function performs a contrast test on a given dataset to compare the means of multiple variables using weighted contrasts. It calculates several statistics, including the t-value, F-value, p-value, confidence intervals, and effect sizes (Cohen's f and partial eta squared).

Usage

contrast_test(
  dat,
  weight = NULL,
  paired = FALSE,
  alternative = c("two.sided", "less", "greater"),
  conf.level = 0.95,
  verbose = TRUE
)

Arguments

dat

A data frame or tibble containing the data to be analyzed. Each column represents a variable. For unbalanced data, please fill missing values with NA.

weight

A numeric vector of weights to apply to each variable in the contrast. The sum of the weights must be zero.

paired

A logical indicating whether the data is paired (TRUE) or unpaired (FALSE). Default is FALSE.

alternative

A character string specifying the type of test. Possible values are "two.sided" (default), "less", or "greater".

conf.level

A numeric value indicating the confidence level for the confidence intervals. Default is 0.95.

verbose

Logical. If TRUE, prints additional messages.

Value

A data frame containing the following columns:

weights

The weights applied to each variable in the contrast.

estimate

The weighted contrast estimate.

lower

The lower bound of the confidence interval.

upper

The upper bound of the confidence interval.

t

The t-value for the contrast.

F

The F-value for the contrast.

df_error

The degrees of freedom for the error.

SS_contrast

The sum of squares for the contrast component.

SS_error

The sum of squares for the error.

SS_effect

The sum of squares for the effect.

SS_total

The toal sum of squares for the data.

p

The p-value for the contrast test.

eta2

The eta squared effect size.

peta2

The partial eta squared effect size.

cohens_f

The Cohen's f effect size.

n_total

The total number of observations in the unpaired data.

n_total_na

The number of missing values in the unpaired data.

n_pair

The number of non-missing pairs in the paired data.

n_pair_na

The number of missing pairs in the paired data.

Details

This function performs a contrast analysis on a given dataset using the provided weights. It computes statistics such as the t-value, F-value, confidence intervals, and effect sizes (Cohen's f and partial eta squared). If the data is paired, pair-wise exclusion is applied. The function supports two-tailed and one-tailed tests depending on the value of the alternative parameter.

See also

trend_test() for trend analysis using contrast tests.

Examples

# Example of linear trend analysis with unpaired data
dat <- data.frame(A = rnorm(100), B = rnorm(100), C = rnorm(100))
weight <- c(1, -1, 0)
contrast_test(dat, weight)
#> 
#> -----------------------------------------------
#> Contrast analysis for independent samples
#> -----------------------------------------------
#> Mean = ( 0.07079,  0.11486, -0.03005)
#> n = (100, 100, 100)
#> Weight = (1, -1, 0)
#> -----------------------------------------------
#> Contrast = -0.04407
#> CI = [-0.3332, 0.2451]
#> -----------------------------------------------
#> t = -0.2999, df = 297, p = 0.7644363
#> (F = 0.08996)
#> -----------------------------------------------
#> SS_contrast = 0.0971, SS_error = 320.56
#> SS_total = 321.66, SS_effect = 1.104
#> -----------------------------------------------
#> eta squared = 0.0003019
#> partial eta squared = 0.0003028, Cohen's f = 0.0174
#> -----------------------------------------------
#> Note:
#> The CI represents the frequentist confidence interval.
#> (Confidence level = 0.95)

# Example with paired data
dat <- data.frame(A = rnorm(50), B = rnorm(50), C = rnorm(50))
weight <- c(1, -2, 1)
contrast_test(dat, weight, paired = TRUE)
#> 
#> -----------------------------------------------
#> Contrast analysis for paired samples
#> -----------------------------------------------
#> Mean = ( 0.1699,  0.2073, -0.2158)
#> N = 50
#> Weight = (1, -2, 1)
#> -----------------------------------------------
#> Contrast = -0.4605
#> CI = [-1.136, 0.2145]
#> -----------------------------------------------
#> t = -1.354, df = 98, p = 0.1788597
#> (F = 1.833)
#> -----------------------------------------------
#> SS_contrast = 1.767, SS_error = 94.48
#> SS_total = 158.83, SS_effect = 64.35
#> -----------------------------------------------
#> eta squared = 0.01113
#> partial eta squared = 0.01836, Cohen's f = 0.1368
#> -----------------------------------------------
#> Note:
#> The CI represents the frequentist confidence interval.
#> (Confidence level = 0.95)