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
2
votes
1 answer

How do I create contingency tables in R?

I have a large data frame with many variables. Many are likert scale answers and schools which observations belong to they are logic variables (and can include overlap). Example: Q1 <- c(1,2,2,4,3,5) Q2 <- c(3,4,3,5,4,5) A <-…
Sarah R
  • 23
  • 4
2
votes
2 answers

How to create frequency tables with xtabs

> data(infert, package = "datasets") > tt = xtabs(~education + induced + spontaneous, data = infert) > ftable(tt) spontaneous 0 1 2 education induced 0-5yrs 0 2 1 1 1 …
Adrian
  • 9,229
  • 24
  • 74
  • 132
2
votes
2 answers

Contingency table once against multiple table

I have a table like the following: V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 fve fve fve fve fve fve fve fve mdm mdm mdm fve fve fve fve fve fve fve fve fve fve fve fve fve fve fve fve fve fve fve mdm fve fve fve fve fve fve fve fve fve fve fve fve fve…
2
votes
0 answers

R: Print omitted 0's in table() - contingency tables

I use large sets of contingency tables by looping table(). Simple problem: columns containing only 0's are omitted in the output. Can this be adjusted using table()? Example: data 1 0 0 0 1 0 0 0 0 0 1 0 1 0 1 0 1 0 The output I get: table(data) …
lardensis
  • 21
  • 1
2
votes
1 answer

Is R able to compute contingency tables on big file without putting the whole file in RAM?

Let me explain the question: I know the functions table or xtabs compute contingency tables, but they expect a data.frame, which is always stored in RAM. It's really painful when trying to do this on a big file (say 20 GB, the maximum I have to…
user1220978
2
votes
1 answer

Creating a contingency table with fixed margins

I am trying to create a table with random entries from a central hypergeometric distribution where the column and row totals are fixed. However I can get the column sums to be fixed and equal but not the row sums. I have read other answers but none…
user4561672
2
votes
2 answers

statistics for 2x4 contingency table with both large and small counts

I apologize if this is a very naive question... I have 7000 2x4 contingency tables with count data. They represent a particular position in a genome and the number of times each dna nucleotide is observed at that position in 2 different…
Ron
  • 25
  • 5
2
votes
2 answers

How to enter data directly to make a data.frame representing a contingency table?

I'm trying to directly enter the following data into R (which represents a contingency table) Area School Coffeshop Hospitals Parks Totatl Washington 142 120 20 20 302 Seattle 120 …
Pirate
  • 311
  • 1
  • 5
  • 12
1
vote
1 answer

Julia function for conditional proportions given margins of multi-dimensional array

I am new to Julia and looking for a function to compute the proportions of multidimensional array give some dimensions as margin. Basically, it is dividing each element of the array by the sum of element in the desired dimensions. In R,…
Mohammad
  • 97
  • 5
1
vote
1 answer

Perform Action to all possible combinations of columns

I´m new to R so forgive me if the question is quite mundane. I have a table with categorical variables as columns. I need to calculate the contingency coefficient between every possible combination of two columns. To calculate the contingency…
Kevin
  • 47
  • 6
1
vote
1 answer

How to write for loop for sjt.xtab in R, a df of factors?

I'm trying to write a for loop to create tables using sjt.xtab() so it iterates through every variation in a dataframe. Ideally this would be generalizable to all other dataframes too so a function would probably be better. I have a dataframe called…
1
vote
1 answer

Create a contingency table with multiple column names and row values

I have a data_frame like this Id A B C 1 0 0 1 2 1 0 0 3 1 0 1 4 0 0 1 I would like to create a contingency matrix like below to calculate correlations Label T/F count A 0 2 A 1 2 B 0 4 B 1 0 C 0 1 C 1 3 I was…
Jessie
  • 313
  • 1
  • 4
  • 16
1
vote
1 answer

Pd.crosstab missing data?

I am using pd.crosstab to count presence/absence data. In the first column, I have several presence counts (represented by 1's), in the second column I have just one 'presence'. Howwever, when I run crosstab on this data that single presence in the…
1
vote
2 answers

Contingency matrix to 1D format in Python

2x2 contingency matrix: Cj 2 1 Ci 1 0 Translates to: [[ 0 0 0 1 ] [ 0 0 1 0 ]] The contingency matrix represents the outcome of two clustering algorithms, each with two clusters. The first row indicates that Ci has three data…
1
vote
3 answers

expand a 2 by 2 contingency table

I have loaded data in the following format: Gender Yes No Male 2 1 Female 1 2 I would like to expand it to: Gender Result Male Yes Male Yes Male No Female Yes Female No Female No I've tried using the expand.table…