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
1
vote
1 answer

How can I apply fisher.test in R to a large matrix, and extract p-values to a new matrix?

I have a large matrix (12 rows, 53 columns) with counts of how many times genes in my clusters "A", "B", "C", etc. overlap with clusters created by someone else "0", "1", "2", etc. I am only providing a preview of the data to not overcrowd the post,…
J22
  • 21
  • 4
1
vote
0 answers

contingency table=fisher test

Hello everyone, I am very new at R programming and I am having a particular issue creating a contingency table in the way that I want. I need to run fisher.test and I cannot get the table to work. I have two categorical variables tmt (a,d) and…
1
vote
1 answer

R: how to divide each cell value (dataframe) by a quantity that includes the correspoding row AND column total

I have a cross-tab (dataframe format), from which I have calculated the chi-sq standardized residuals. Below I provide the two reproducible datasets. Cross-tab: df <- structure(c(310, 36, 0, 0, 212, 158, 9, 0, 21, 35, 17, 4, 25, 102, 49, 18, 7, 35,…
NewAtGis
  • 139
  • 7
1
vote
3 answers

Create contingency table that displays the frequency distribution of pairs of variables

I want to create a contingency table that displays the frequency distribution of pairs of variables. Here is an example dataset: mm <- matrix(0, 5, 6) df <- data.frame(apply(mm, c(1,2), function(x) sample(c(0,1),1))) colnames(df) <- c("Horror",…
cliu
  • 933
  • 6
  • 13
1
vote
1 answer

Why are there redundant rows and columns in my contingency table?

I am a novice to R, and I am currently learning contingency table. I want to create a contingency table using the data from "loans_full_schema"(from openintro) with the "application_type" and "homeownership" datas. Below is my…
Willkczy
  • 13
  • 3
1
vote
0 answers

Crosstab to identify the number of id with multiple modalities

I cant resolve this since this morning, I have a dataset with 2 columns : id_campaign and id_client I need to know the number of client in a couple of campaign, he an exemple : I want a dataset like this : But with this command I have 0's or I…
Robin
  • 51
  • 5
1
vote
4 answers

Count co-occurences across groups

I would like to count co-occurences of a variable across groups. I'm sure there's an easy {dplyr}/{tidyr} solution with count or pivot_ but I'm stuck. Thank you for your help! Below is a reprex of my input and potential output (I don't care about…
maia-sh
  • 537
  • 4
  • 14
1
vote
2 answers

Can I use group_map or group_walk to iteratively export results?

I want to iteratively process a master list of comparisons using group_walk() or group_map() as an alternative method to import batches of .csv files. I would like to input a dataset that looks like this: Test…
MBell
  • 57
  • 1
  • 8
1
vote
1 answer

how is obtained the observed cell counts in svychisq in R?

I'm using the function survey::svychisq() to test for independence in a two-way contingency table for complex samples. With svytable() I get the observed counts considering the weights defined in design, and I would assume that the observed values…
MDSF
  • 123
  • 6
1
vote
1 answer

How to make a contingency table from categorical data using Python?

I have a sample dataset as below Phillippines Indonesia Malta India 0 Error Free Error Free Defective Error Free 1 Error Free Error Free Error Free Defective 2 Error Free Defective Defective Error Free 3 Error Free …
1
vote
1 answer

Adding conditional probabilities to data.table in R

I have the titanic dataset in which I want to find the probability of survival based on 3 conditions. The following table gives the probabilities. library(PASWR2) tab = with(TITANIC3, ftable(fare = fare > 200, pclass, sex, survived)) %>%…
Saurabh
  • 1,566
  • 10
  • 23
1
vote
2 answers

Construct 3-way contingency table in R

The 4 variables I have are: Breathless(B)= {y,n}, wheeze(W)= {y,n}, age={g1,g2,...,g5}, and count= {20 numeric counts}. I would like to construct a table as shown in the image, however the ftable function is unable to arrange the output properly…
siegfried
  • 461
  • 1
  • 5
  • 20
1
vote
2 answers

Contingency table from a 2x3 dataframe in r

I have this df Genero Frustrado No frustrado Hombre 138 9 Mujer 145 12 And I need to turn it into a contingency table like this: Frustrado Genero Si …
Ale Rey
  • 75
  • 8
1
vote
2 answers

Create 2x2 Conditional Table from Counting Instances in Dataframe

I have a dataframe that is broken down by the gender of an author, their role in a project, and an identifier (PMID) (see below). What I need is to create a 2x2 contingency table so I can calculate an odds ratio for the association between being a…
1
vote
1 answer

contingency table doesn't return the right result in r shiny with pickerInput

I'm trying to create a contingency table on r shiny using the function table() and a pickerInput. While I don't have any message error, the result I obtain on r shiny isn't right. The code and the data I tried are…