The “chisq.test” Function in R
Package: Base R (no specific package required)
Purpose: Performs chi-squared tests of independence and goodness-of-fit.
General Class: Statistical Testing
Required Argument(s):
x: A contingency table or a matrix representing the observed counts.
Notable Optional Arguments:
y: An optional matrix representing expected counts for a goodness-of-fit test.
correct: A logical value indicating whether to apply a continuity correction for a 2x2 table. The default is TRUE.
simulate.p.value: A logical value indicating whether to compute a p-value by Monte Carlo simulation. The default is FALSE.
B: The number of replicates for Monte Carlo simulation. The default is 2000.
expected: A logical value indicating whether to return expected counts. The default is TRUE.
rescale.p: A logical value indicating whether to rescale p-values for Monte Carlo simulation. The default is FALSE.
Example:
# Example data for a chi-squared test of independence
set.seed(123)
obs_table <- matrix(c(25, 15, 10, 20), nrow = 2)
# Perform a chi-squared test
result <- chisq.test(obs_table)
# Display the result
print(result)In this example, the chisq.test function is used to perform a chi-squared test of independence on a 2x2 contingency table (obs_table). The result of the test, including the test statistic, degrees of freedom, and p-value, is then printed to the console.