<p>You can get standardised residuals from <code>chisq.test</code> and convert these to p-values, but it's <a href="https://stats.stackexchange.com/questions/129194/converting-chi-square-adjusted-residuals-to-p-values">not recommended</a>. The frequencies of each genotype depend on the counts of all other genotypes, so the p-values aren't independent and they aren't corrected for multiple testing, but if you must:  </p>

<pre><code>pnorm(-abs(chisq.test(tmp[,c('A','B')])$stdres)))
</code></pre>

CC BY-SA 4.0
2018-07-23T16:07:23.637Z
hm... What other options are possible? whether the differences between the two groups were statistically significant for all pairs (rs5+rs6 and all pairs - 4a/4a G/G, 4b/4a G/G and etc)?
n.osennij
CC BY-SA 4.0
2018-07-23T16:18:53.470Z
If I were you, I would just focus on the overall chisq p-value (corrected for the number of SNP pairs tested). The frequency of allele 2 depends on the frequency of allele 1 and the frequency of the genotypes depends on the allele frequencies, so I don't think it makes much sense to test each genotype independently
heathobrien
CC BY-SA 4.0
2018-07-23T16:41:40.800Z
Note that all you're doing is looking at differences in linkage disequilibrium between groups. There are a lot of tools out there to that efficiently
heathobrien
CC BY-SA 4.0
2018-07-23T20:11:07.353Z
chisq.test - not very good for values less then 5. I'v try to use fisher.test `pnorm(-abs(fisher.test(tmp[,c('A','B')], simulate.p.value=TRUE)$stdres))`, but I'v got the error - Error in abs(fisher.test(tmp[, c("A", "B")], simulate.p.value = TRUE)$stdres) :   non-numeric argument to mathematical function
n.osennij
CC BY-SA 4.0
2018-07-23T20:13:23.440Z
o the p-values aren't independent and they aren't corrected for multiple testing, but if you must - I'm not must. I just think it is good solution to use chisq.test. I just need to know - is there any differencies in two grops
n.osennij
CC BY-SA 4.0
2018-07-23T20:36:16.027Z
Fishers exact test doesn't give you residuals. Having values less than 5 is probably the least of your worries if you're trying to obtain p-values for each combination of genotypes.
heathobrien
CC BY-SA 4.0
2018-07-23T20:42:11.827Z
In that case, I'm completely confused and do not understand what I can do with my data and how...
n.osennij
CC BY-SA 4.0
2018-07-23T20:45:25.773Z
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/80573/discussion-between-heathobrien-and-n-osennij).
heathobrien
CC BY-SA 4.0
2018-07-23T16:02:05.047Z
2018-07-23T16:02:05.047Z
heathobrien
<p>I have next <code>SNPs</code> data.frame structure</p>

<pre><code>&gt; str(SNPs)
'data.frame':   1703 obs. of  4 variables:
 $ group: Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
 $ rs1  : Factor w/ 3 levels "D/D","I/D","I/I": 1 1 2 3 3 2 1 1 1 1 ...
 $ rs2  : Factor w/ 3 levels "a/a","a/b","b/b": 3 3 2 3 3 2 2 3 3 2 ...
 $ rs3  : Factor w/ 3 levels "G/G","G/T","T/T": 2 1 2 1 1 3 1 1 2 1 ...
 ...other rs


&gt; head(SNPs)
  group rs1 rs2 rs3 ...other rs
1     A D/D b/b G/T
2     A D/D b/b G/G
3     A I/D a/b G/T
4     A I/I b/b G/G
5     A I/I b/b G/G
6     A I/D a/b T/T
</code></pre>

<p>For example, I noticed that <code>rs5 4b/4a</code> and <code>rs6 G/G</code> in <code>group A</code> very often occur together (see below). In <code>group B</code> they very often occur together too. So I want to know - is it statistical regularity or not.</p>

<p>I can create table with all pairs in both groups</p>

<pre><code>&gt; SNPs$rs5_rs6 &lt;- paste(SNPs$rs5, SNPs$rs6)
&gt; tmp &lt;- table(SNPs$rs5_rs6, SNPs$group)
&gt; tmp

              A   B
  4a/4a G/G   1  20
  4b/4a G/G  31  83
  4b/4a G/T  14  51
  4b/4a T/T   1   0
  4b/4b G/G  37 106
  4b/4b G/T  35 119
  4b/4b T/T  11  31
</code></pre>

<p>So, now I need compare (find p-value) group A and group B: <code>4a/4a G/G</code> in group A vs <code>4a/4a G/G</code> in group B, <code>4b/4a G/G</code> in group A vs <code>4b/4a G/G</code> in group B, <code>4b/4a G/T</code> in group A vs <code>4b/4a G/T</code> in group B and etc. Only this pairs/</p>

<p>How can I do that?
I need p-values for all </p>

<pre><code>&gt; tmp

              A   B
  4a/4a G/G   1  20 - p-value?
  4b/4a G/G  31  83 - p-value?
  4b/4a G/T  14  51 - p-value?
  4b/4a T/T   1   0 - p-value?
  4b/4b G/G  37 106 - p-value?
  4b/4b G/T  35 119 - p-value?
  4b/4b T/T  11  31 - p-value?
</code></pre>

CC BY-SA 4.0
2018-07-23T14:41:02.030Z
If you can restate your question in terms of explicit null and alternative hypotheses it will be much easier for people to help you determine an appropriate test to find p-values
heathobrien
CC BY-SA 4.0
2018-07-23T15:04:30.813Z
@heathobrien chisq - it is my choice. Problem is not to choice test. Problem is to calculate it for pairs in `tmp`
n.osennij
CC BY-SA 4.0
2018-07-23T15:22:44.297Z
If your question is how to apply a chisq test to the above table, the answer is `chisq.test(tmp$A, tmp$B)`
heathobrien
CC BY-SA 4.0
2018-07-23T15:26:16.553Z
heathobrien, my question is how to apply chisq test to `tmp` and get 7 p-values:
compare`4a/4a G/G` in group A vs `4a/4a G/G` in group B and get first p-value, compare `4b/4a G/G` in group A vs `4b/4a G/G` in group B and get the second p-value and etc.
n.osennij
CC BY-SA 4.0
2018-07-23T16:01:33.923Z
OK. Now we're getting somewhere. See my answer
heathobrien
<gene><snp><r>
To find p-value of compounds (pairwise)
# To find p-value of compounds (pairwise)

(Asked by: username_0 on 2018-07-23)

I have next SNPs data.frame structure
> str(SNPs)
'data.frame':   1703 obs. of  4 variables:
 $ group: Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
 $ rs1  : Factor w/ 3 levels "D/D","I/D","I/I": 1 1 2 3 3 2 1 1 1 1 ...
 $ rs2  : Factor w/ 3 levels "a/a","a/b","b/b": 3 3 2 3 3 2 2 3 3 2 ...
 $ rs3  : Factor w/ 3 levels "G/G","G/T","T/T": 2 1 2 1 1 3 1 1 2 1 ...
 ...other rs


> head(SNPs)
  group rs1 rs2 rs3 ...other rs
1     A D/D b/b G/T
2     A D/D b/b G/G
3     A I/D a/b G/T
4     A I/I b/b G/G
5     A I/I b/b G/G
6     A I/D a/b T/T

For example, I noticed that rs5 4b/4a and rs6 G/G in group A very often occur together (see below). In group B they very often occur together too. So I want to know - is it statistical regularity or not.
I can create table with all pairs in both groups
> SNPs$rs5_rs6 <- paste(SNPs$rs5, SNPs$rs6)
> tmp <- table(SNPs$rs5_rs6, SNPs$group)
> tmp

              A   B
  4a/4a G/G   1  20
  4b/4a G/G  31  83
  4b/4a G/T  14  51
  4b/4a T/T   1   0
  4b/4b G/G  37 106
  4b/4b G/T  35 119
  4b/4b T/T  11  31

So, now I need compare (find p-value) group A and group B: 4a/4a G/G in group A vs 4a/4a G/G in group B, 4b/4a G/G in group A vs 4b/4a G/G in group B, 4b/4a G/T in group A vs 4b/4a G/T in group B and etc. Only this pairs/
How can I do that?
I need p-values for all 
> tmp

              A   B
  4a/4a G/G   1  20 - p-value?
  4b/4a G/G  31  83 - p-value?
  4b/4a G/T  14  51 - p-value?
  4b/4a T/T   1   0 - p-value?
  4b/4b G/G  37 106 - p-value?
  4b/4b G/T  35 119 - p-value?
  4b/4b T/T  11  31 - p-value?

**Tags:** gene, snp, r
> **Comments:**
> **username_1**: If you can restate your question in terms of explicit null and alternative hypotheses it will be much easier for people to help you determine an appropriate test to find p-values  
> **username_0**: @heathobrien chisq - it is my choice. Problem is not to choice test. Problem is to calculate it for pairs in `tmp`  
> **username_1**: If your question is how to apply a chisq test to the above table, the answer is `chisq.test(tmp$A, tmp$B)`  
> **username_0**: heathobrien, my question is how to apply chisq test to `tmp` and get 7 p-values: compare`4a/4a G/G` in group A vs `4a/4a G/G` in group B and get first p-value, compare `4b/4a G/G` in group A vs `4b/4a G/G` in group B and get the second p-value and etc.  
> **username_1**: OK. Now we're getting somewhere. See my answer  

---

## 1 answers

### Answer by username_1 on 2018-07-23. Score: 2

You can get standardised residuals from chisq.test and convert these to p-values, but it's not recommended. The frequencies of each genotype depend on the counts of all other genotypes, so the p-values aren't independent and they aren't corrected for multiple testing, but if you must:  
pnorm(-abs(chisq.test(tmp[,c('A','B')])$stdres)))

> **Comments:**
> **username_0**: hm... What other options are possible? whether the differences between the two groups were statistically significant for all pairs (rs5+rs6 and all pairs - 4a/4a G/G, 4b/4a G/G and etc)?  
> **username_1**: If I were you, I would just focus on the overall chisq p-value (corrected for the number of SNP pairs tested). The frequency of allele 2 depends on the frequency of allele 1 and the frequency of the genotypes depends on the allele frequencies, so I don't think it makes much sense to test each genotype independently  
> **username_1**: Note that all you're doing is looking at differences in linkage disequilibrium between groups. There are a lot of tools out there to that efficiently  
> **username_0**: chisq.test - not very good for values less then 5. I'v try to use fisher.test `pnorm(-abs(fisher.test(tmp[,c('A','B')], simulate.p.value=TRUE)$stdres))`, but I'v got the error - Error in abs(fisher.test(tmp[, c("A", "B")], simulate.p.value = TRUE)$stdres) :   non-numeric argument to mathematical function  
> **username_0**: o the p-values aren't independent and they aren't corrected for multiple testing, but if you must - I'm not must. I just think it is good solution to use chisq.test. I just need to know - is there any differencies in two grops  
> **username_1**: Fishers exact test doesn't give you residuals. Having values less than 5 is probably the least of your worries if you're trying to obtain p-values for each combination of genotypes.  
> **username_0**: In that case, I'm completely confused and do not understand what I can do with my data and how...  
> **username_1**: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/80573/discussion-between-heathobrien-and-n-osennij).  

---

