kw.test: Test for trend in proportions

View source: R/stat.R

kw.testR Documentation

Test for trend in proportions

Description

Performs a test for trend in an r x c contingency table with one nominal (rows, r > 2) and one ordinal (columns, c > 2) variable.

Usage

kw.test(x, ...)

## Default S3 method:
kw.test(x, g, ..., simulate.p.value = FALSE, B = 2000L)

## S3 method for class 'formula'
kw.test(formula, data, ...)

Arguments

x

a factor-like vector giving the (unordered) variable (equivalently the row variable of a contingency table); if x is also ordered, consider using jt.test

alternatively, x can be an r x c table or matrix with at least two rows (unordered) and three columns (ordered); x may also be a list of the row variable split by the ordered column variable in which case the list is assumed to be ordered, i.e., x[[1]] < x[[2]] < ... < x[[c]]; see examples

...

further arguments to be passed to or from methods

g

a factor-like vector giving the ordered group for each corresponding element of x, ignored with a warning if x is a list or table; if g is not a factor, it will be coerced, and groups will be ordered as sort(unique(g)); see factor

simulate.p.value

logical; if TRUE, p-value is computed using by Monte Carlo simulation

B

an integer specifying the number of replicates used in the Monte Carlo test

formula

a formula of the form row ~ column where row gives the (unordered) row variable and column gives the ordered column variable

data

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

Value

A list with class "htest" containing the following elements:

statistic

the chi-squared test statistic

parameter

the degrees of freedom of the approximate chi- squared distribution of the test statistic

p.value

the p-value of the test (two-sided)

method

a character string describing the test, and, optionally, the number of Monte Carlo replications, if applicable

data.name

a character string giving the names of the data

conf.int

optionally (if simulate.p.value = TRUE), the 99% confidence interval of the Monte Carlo p-value

summary

optionally (if simulate.p.value = TRUE), a summary of the simulated test statistics

See Also

jt.test for doubly-ordered tables; cuzick.test; ca.test; DescTools::CochranArmitageTest; prop.trend.test

Examples

## example from Exact Test (Mehta), figure 11.1
## system.file('docs', 'PASW_Exact_Tests.pdf', package = 'rawr')
dat <- data.frame(
  regimen  = rep(c('CTMX', 'CCNU', 'MTX', 'CTX+CCNU', 'CTX+CCNU+MTX'),
                 times = c(2, 2, 3, 4, 6)),
  response = c('NR', 'NR', 'NR', 'PR', 'NR', 'NR', 'NR',
               'NR', 'NR', 'PR', 'PR', 'NR', 'PR', 'CR', 'CR', 'CR', 'CR')
)
dat$response2 <- factor(dat$response, c('NR', 'PR', 'CR'))

kw.test(dat$regimen, dat$response)   ## incorrect
kw.test(dat$regimen, dat$response2)  ## correct


## the following are equivalent to the above
kw.test(dat$regimen ~ dat$response2)
kw.test(regimen ~ response2, dat)
kw.test(split(dat$regimen, dat$response2))
kw.test(table(dat$regimen, dat$response2))


## compare (note formula is reversed)
kruskal.test(response2 ~ regimen, dat)

## Not run: 
## simulate p-value with 5k replicates
set.seed(1)
kw.test(regimen ~ response2, dat, simulate.p.value = TRUE, B = 5000)

## End(Not run)


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.