chisq_test: Chi-squared Tests

View source: R/chisq_test.R

chisq_testR Documentation

Chi-squared Tests

Description

Perform a chi-squared goodness-of-fit test or a chi-squared test on independence on variables of a data set. The output is printed as a LaTeX table that mimics the look of SPSS output.

Usage

chisq_test(data, variables, p = NULL)

## S3 method for class 'chisq_test_SPSS'
to_SPSS(
  object,
  statistics = c("test", "frequencies"),
  version = r2spss_options$get("version"),
  digits = c(1, 3),
  ...
)

## S3 method for class 'chisq_test_SPSS'
print(
  x,
  statistics = c("frequencies", "test"),
  version = r2spss_options$get("version"),
  digits = c(1, 3),
  ...
)

chisqTest(data, variables, p = NULL)

Arguments

data

a data frame containing the variables.

variables

a character vector specifying the categorical variable(s) of interest. If only one variable is specified, a goodness-of-fit test is performed. If two variables are specified, a test on independence is performed (with the first variable used for the rows and the second variable for the columns of the crosstabulation).

p

a vector of probabilities for the categories in the goodness-of-fit test.

object, x

an object of class "chisq_test_SPSS" as returned by function chisq_test.

statistics

a character string or vector specifying which SPSS tables to produce. Available options are "frequencies" for a table of the observed and expected frequencies, and "test" for test results. For the to_SPSS method, only one option is allowed (the default is the table of test results), but the print method allows several options (the default is to print all tables).

version

a character string specifying whether the table should mimic the content and look of recent SPSS versions ("modern") or older versions (<24; "legacy"). The main difference in terms of content is that small p-values are displayed differently.

digits

an integer vector giving the number of digits after the comma to be printed in the SPSS tables. The first element corresponds to the number of digits for the expected frequencies, and the second element corresponds to the number of digits in the table for the test.

...

additional arguments to be passed down to format_SPSS.

Details

The print method first calls the to_SPSS method followed by to_latex. Further customization can be done by calling those two functions separately, and modifying the object returned by to_SPSS.

Value

An object of class "chisq_test_SPSS" with the following components:

chisq

a list containing the results of the chi-squared test.

lr

a list containing the results of a likelihood ratio test (only test on independence).

MH

a list containing the results of a Mantel-Haenszel test of linear association (only test on independence).

observed

a table containing the observed frequencies.

expected

a vector or matrix containing the expected frequencies.

n

an integer giving the number of observations.

k

an integer giving the number of groups (only goodness-of-fit test).

r

an integer giving the number of groups in the first variable corresponding to the rows (only test on independence).

c

an integer giving the number of groups in the second variable corresponding to the columns (only test on independence).

variables

a character vector containing the name(s) of the categorical variable(s) of interest.

type

a character string giving the type of chi-squared test performed ("goodness-of-fit" or "independence").

The to_SPSS method returns an object of class "SPSS_table" which contains all relevant information in the required format to produce the LaTeX table. See to_latex for possible components and how to further customize the LaTeX table based on the returned object.

The print method produces a LaTeX table that mimics the look of SPSS output.

Note

The test on independence also reports the results of a likelihood ratio test.

LaTeX tables that mimic recent versions of SPSS (version = "modern") may require several LaTeX compilations to be displayed correctly.

Author(s)

Andreas Alfons

Examples

# load data
data("Eredivisie")

# test whether the traditional Dutch 4-3-3 (total football)
# is still reflected in player composition
chisq_test(Eredivisie, "Position", p = c(1, 4, 3, 3)/11)

# test whether playing position and dummy variable for
# foreign players are independent
chisq_test(Eredivisie, c("Position", "Foreign"))


r2spss documentation built on May 25, 2022, 5:05 p.m.