Tuesday, June 30, 2020

xtabs

The xtabs command lets you build contingency tables (two-way table, cross tablulation, or crosstab). It is similar to table. Both are depictions of spread, which display multivariate frequency distribution of two variables, though from the R documentation, it looks like you can use xtabs for more than two variables. In the STAT 3473 videos, Kern asserts that xtab is the easiest way to make a table.

See the table topic for an example of a crosstab.

For two variables, the xtabs command provides an initial output nearly identical to the output of table. It has 10 parameters. Of these, you'll always need the formula and data parameters.

The xtabs command is one of the first ones I encountered with the wacky formula. You have to introduce columns from a dataset with a tilde (~), and separate columns with the plus sign (+). That's the formula parameter. Then the name of the data.frame that contains the columns. Here's the form:

xtabs(~[column1] + [column2] + … [columnN], data = [data.frame])

R Example

This example uses the standard infert dataset. The infert dataset shows infertility data after abortion across a number of characteristics, like education.

xtabs(~education + induced + spontaneous, data = infert)
, , spontaneous = 0

induced
education 0 1 2
0-5yrs 2 1 6
6-11yrs 46 15 10
12+ yrs 19 29 13

, , spontaneous = 1

induced
education 0 1 2
0-5yrs 1 0 0
6-11yrs 19 9 5
12+ yrs 27 7 3

, , spontaneous = 2

induced
education 0 1 2
0-5yrs 1 1 0
6-11yrs 13 3 0
12+ yrs 15 3 0


Separate tables are produced for each value of spontaneous. You can put these together into one table with ftable (flatten table), which I don't go into further in this document.

> tt = xtabs(~education + induced + spontaneous, data = infert)
> ftable(tt)

                 spontaneous 0 1 2
education induced
0-5yrs    0                  2 1 1
          1                  1 0 1
          2                  6 0 0
6-11yrs   0                 46 19 13
          1                 15 9 3
          2                 10 5 0
12+ yrs   0                 19 27 15
          1                 29 7 3
          2                 13 3 0


You would think that xtabs would do the flattening part (and indeed it may). This is one of those commands I'm going to have to look into further.

No comments:

Post a Comment

Please help to combat malicious use of the Internet.