Questions tagged [contingency]

A contingency table is a non-negative integer matrix with specified row and column sums.

A contingency table is a non-negative integer matrix with specified row and column sums, so named by Karl Pearson in developing statistical tests of significance. Observations are counted in a table with appropriate row and column labels, whereby statistical tests may be done on the entries to determine how likely the results would arise if the row and column outcomes were independent events.

Given specified row and column sums, counting the number of possible contingency tables can be a hard problem. Indeed even the case of $2$ rows and $n$ columns is known to be #P-complete.

However existence of solutions, unless otherwise constrained, is easy: it is necessary and sufficient that the row sums and column sums give equal totals for the entries of the entire matrix (balance condition).

An example of a further constraint would be requiring 0/1 entries, called binary contingency tables. Necessary and sufficient criteria for these restricted solutions were given by Gale and Ryser (independently) in 1957.

242 questions
3
votes
3 answers

Contingency table when you have a column with count values

I have a dataframe that looks something like this: Year Var Count 2019 A 10 2020 B 23 2019 B 36 2020 A 42 How can I make a Year x Var contingency table using the "Count" column as frequencies?
3
votes
1 answer

How do table and ftable differ?

The documentation for ftable tells us that "ftable creates ‘flat’ contingency tables". However, the meaning of this isn't getting through to me. I've placed two examples below, but they look so similar that I feel like I'm completely missing the…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
3
votes
1 answer

Create a single table providing the mean values of column A across columns B and C in R

I want to create a single contingency table given 3 columns of a data frame using xtabs() function(in R). The code below works fine for 2 columns: xtabs(~B + C, data = theData) #contingency table for two columns but when I add one more attribute, I…
Helen Grey
  • 439
  • 6
  • 16
3
votes
1 answer

Two-Way Contingency Table with frequencies and percentages

I got the following Two-way contingency table with cell percentages along with frequencies (in parentheses). gender blue blue-gray brown dark hazel yellow female 33.33% (3) 0.00% (0) 55.56% (5) 0.00% (0) 11.11% (1) 0.00%…
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
3
votes
0 answers

Change fill colors in contingency plot in R

I try to to change the colors of a contingency plot in R that is created with the survey package. Unfortunately only the colors of the rows from the contingency table are changed. Is there a way to give each of the four fields below an individual…
joaoal
  • 1,892
  • 4
  • 19
  • 29
3
votes
3 answers

Getting each element in Pandas crosstab

I have a dataset given below: a,b,c 1,1,1 1,1,1 1,1,2 2,1,2 2,1,1 2,2,1 I created crosstab with pandas: cross_tab = pd.crosstab(index=a, columns=[b, c], rownames=['a'], colnames=['b', 'c']) my crosstab is given as an output: b 1 2 c …
user3104352
  • 1,100
  • 1
  • 16
  • 34
3
votes
1 answer

Two way frequency table in Rmarkdown

In what way can I construct a two-way frequency table in Rmarkdown? Something like: I tried to use both the kable function from the knitr package and the datable function from the DT package, but none gave me the desired…
Michael
  • 1,281
  • 1
  • 17
  • 32
3
votes
2 answers

crosstable() export to csv

Hello so i needed to make a crosstable. I found out there were multiple ways, but there was this function that made the table just like a pivot table from Excel. It works perfectly however i cannot export it to csv, or excel,given the fact that is…
Dennis Aguilar
  • 113
  • 1
  • 7
3
votes
3 answers

How to combine logical gate NOT in lst.count((x, not y)) in Python

I am trying to construct contingency table from a list of tuples. The list looks like this: lst = [('a', 'bag'), ('a', 'bag'), ('a', 'bag'), ('a', 'cat'), ('a', 'pen'), ('that', 'house'), ('my', 'car'), ('that', 'bag'), ('this', 'bag')] Given a…
Mohammed
  • 1,364
  • 5
  • 16
  • 32
3
votes
2 answers

Find frequencies over 3rd quartile in table

I have a big data frame (+239k observations on 57 variables) with some sickness descriptions and medicines administered to those sicknesses for people in different age ranges. I'd like to find those medicines in the top quartile of frequency use for…
PavoDive
  • 6,322
  • 2
  • 29
  • 55
3
votes
0 answers

Applying survey weights to data before compiling contingency tables in R

The sample for a survey I am analysing was not selected randomly and so I need to apply a vector of weights to make the findings representative of the population. I have used wtd.table() (from gmodels) successfully to create frequency tables but now…
spatel
  • 33
  • 3
3
votes
2 answers

How to merge two input values of contingency matrix?

I have this kind of xtabs object: structure(c(1, 4, 7, 2, 5, 8, 3, 6, 9), .Dim = c(3L, 3L), .Dimnames = structure(list(Var1 = c("A", "B", "C"), Var2 = c("A", "B", "C")), .Names = c("Var1", "Var2")), class = c("xtabs", "table")) which gives: …
DJack
  • 4,850
  • 3
  • 21
  • 45
3
votes
3 answers

select most frequent element in dataframe while using table

I have a list of data frames on which I want to use table. The list looks like this: pronouns <- data.frame(pronounciation = c("juː","juː","juː","ju","ju","jə","jə","hɪm","hɪm","hɪm", "həm","ðɛm"), words =…
Annemarie
  • 689
  • 6
  • 14
  • 28
2
votes
1 answer

How do I read dimension names from a file with a contingency table in R

I have an input data file (say, pets.csv)as a contingency table As for example, so Animal Dog Cat Color Black 15 20 White 30 60 So, the dimension names are Animal and Color. Is there a way to read this into R with…
Zina Taran
  • 21
  • 2
2
votes
1 answer

Generalizable for loop for all possible 2-way tables?

I'm sure the answer is staring me in the face here so bear with me but how do I build a for loop that iterates through a dataframe, creating 2-way contingency tables for all possible iterations? Sample dataframe: amazon <- c(0,1,0,1,1) age <-…
1
2
3
16 17