# Independence test for values between 0 and 1

I want to compare a set of frequencies and determine the p-value of their dependence. One of them is the null distribution of frequencies and the other is the distribution that I want to test. The data look like this:

nucleotide   background    selected
1   0.1489113   0.06074766
2   0.1428619   0.04205607
3   0.1189465   0.63084112
4   0.1209048   0.05140187
5   0.1218093   0.07476636
6   0.1282073   0.04205607
7   0.2183589   0.09813084


I cannot use the $\chi^2$ test because the numbers are less than 1. I've tried to take the $\log_2$ of the ratio, but I end up with some negative numbers so the $\chi^2$ is a no go again. How can I conduct a test of independence with these values and compute a p-value?

basically i'm working on DNA sequences. i have a background pool of sequences that act like a null distribution and from this background i have selected a number of sequences for a particular biological function. now, i wanted to compare the distribution of mutations in a small sequence of 7 nucleotides in the pool of selected sequences versus the background.

To achieve this, i've taken all the instances where this sequence is present with one mutation in both pools and determined the frequency of mutation for each nucleotide (e.g. number of mutations at a given nucleotide / total number of mutations over all nucleotides), that is how i end up with those frequencies.

now my aim was to show that the pattern of mutation is indeed much different in the selected pool with respect to the background (63% of the mutations in the selected pool happen in the 3rd nucleotide, and the other nucleotides undergo visibly less mutations than in the background)

to provide further information, the selected column describes the frequency of 214 mutations over the 7 nucleotides while the background column describes the frequency of 120508 mutations over 7 nucleotides.

• Do your observations represent the number of events over the same number of trials? – Seth Nov 12 '12 at 19:05
• I can't understand why you would want to use the chi-squared test in the first place. What would it mean for these two sets to be independent? Are you wondering if these vectors are uncorrelated? Are you wondering if the means or SDs of these vectors differ from each other? – gung - Reinstate Monica Nov 12 '12 at 19:23
• To six significant figures, one can rewrite the rightmost column as (13,9,135,11,16,9,21)/214, suggesting the sample size is a multiple of 214. If so, and if these frequencies represent independent observations, without a doubt--regardless of sample size--they are not explained by the null distribution (middle column). – whuber Nov 12 '12 at 20:35
• @whuber i don't know how the hell you managed to determine that the observations were 214 but you are correct. i've edited the question and explained how i obtained the data. I too think that the rightmost column is NOT explained by the middle column; but i wanted some sort of statistical indicator that could tell me how much. – Tito Candelli Nov 13 '12 at 9:00
• I've updated the solution – Adam Ryczkowski Nov 13 '12 at 12:17

I'd suggest you forget about the frequencies and work with the raw counts (i.e. don't divide by 214 and 120508).

First put these data into R. You can do it eighter directly or by use of R for Excel (which works only on 32-bit MS Office for Windows). I'll assume you put the 7 counts of background in variable B, and 7 counts of selected in variable S. Remember, that R is case-sensitive!

Run this code:

S<-c(23,123,54,235,23,32,123) #sample data.
B<-c(65,345,65,35,786,43,234) #sample data.
library(dgof) #Required to use a more accurate version of the ks.test.
ks.test(rep(1:7,times=S),rep(1:7,times=B))


The subexpression rep(1:7,times=S) converts counts into the actual observations by simple replication.

• How do you account for the number of observations? (Answer: you cannot, because they are unknown.) Until you can, no test--not K-S, not $\chi^2$--will be applicable. – whuber Nov 12 '12 at 20:52
• I see now what you intended, Adam. The last paragraph is not very clear. I think you mean to say that if, in this example, you have the raw counts, then you can apply K-S to them (not to the data as presented in the question). The spirit of the idea is right--$\chi^2$ would work well--but K-S (as far as I know) does not apply to discrete distributions. It also is not at all apparent how it would be applied to count data. – whuber Nov 12 '12 at 21:04
• The KS test statistic works just fine for any distribution, but (if I recall correctly) calculation of the p-value assumes the distribution is continuous. Discrete distributions will screw it up. See the first page of journal.r-project.org/archive/2011-2/…, for instance. – whuber Nov 12 '12 at 21:20
• Thank you. Indeed, I didn't know about that difficulties. The bottom line of this paper is that the end user should use ks.test from the dgof package instead of the standard one, and proceed with testing the discrete distributions, just like the problem never existed. Assuming one uses R, of course. – Adam Ryczkowski Nov 12 '12 at 22:15
• Although it does not matter for these data, because the difference is so obvious, dgof::ks.test is not applicable to this situation. Its documentation appears to state that when comparing discrete distributions, one of them must be a known reference and not estimated from data. Furthermore, the p-value computed for sets with more than $30$ data values is only approximate and "conservative." In such a case, one is likely better off with a permutation test or a chi-square test (available in base R). – whuber Nov 13 '12 at 16:39

Good comment by Tito, pointing out that you may want to make sure that your frequencies are coming from a number of trials that are similar (ideally identical).

I recommend multiplying each proportion that you have by X, and setting the number of trials to X also.

If you have an identical number of trials for each variable, then X = the number of trials. You can then run your chi-squared. If there are differing numbers of trials, then I'm not sure.

Hope this helps.