Perform a \(\chi^{2}\) goodness-of-fit test or a \(\chi^{2}\) test on independence on variables of a data set. The output is printed as a LaTeX table that mimics the look of SPSS output.
chisq_test(data, variables, p = NULL)# S3 method for chisq_test_SPSS
to_SPSS(
object,
statistics = c("test", "frequencies"),
version = r2spss_options$get("version"),
digits = c(1, 3),
...
)
# S3 method for chisq_test_SPSS
print(
x,
statistics = c("frequencies", "test"),
version = r2spss_options$get("version"),
digits = c(1, 3),
...
)
chisqTest(data, variables, p = NULL)
a data frame containing the 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).
a vector of probabilities for the categories in the goodness-of-fit test.
an object of class "chisq_test_SPSS"
as returned by
function chisq_test
.
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).
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.
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
.
An object of class "chisq_test_SPSS"
with the following components:
chisq
a list containing the results of the \(\chi^{2}\) 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^{2}\) 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.
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
.
# NOT RUN {
# 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"))
# }
Run the code above in your browser using DataLab