# Doing a chi squared test in R [closed]

So let's say I've tallied up each letter in a text. I now have a vector whose indices represent the letters, and the values in the vector represent the number of occurences of a letter. Here it is:

v =  [545  54 107 129   0  63  57  35 504  12   3 229  74 397 342 108  46 263 341 353 355  52   1  16  19   4].


I have another vector which is made of the theoretical frequencies of each letter, based on the dictionary. Here it is:

freq = [0.1780 0.0870 0.0800 0.0750 0.0750 0.0640 0.0610 0.0600 0.0580 0.0520 0.0390 0.0330 0.0320 0.0260 0.0140 0.0130 0.0120 0.0110 0.0100 0.0070
0.0050 0.0030 0.0030 0.0010 0.0007 0.0003]


Now, my null hypothesis is that the distribution I found conforms to the theoretical frequencies. I want to do a chi-squared to test this out.

Here is what I did; I'm new to this, and have a feeling it's wrong:

So I first created the expected data by multiplying the number of letters in total to the frequencies:

th_dist = sum(v)*freq


Then, I created a matrix made of two rows:

chi_mat = matrix(c(v, th_dst), nrow = 2, byrow = True)


Which creates a vector where the first row is v and the second row is th_dist.

Then, I simply used chisq.test(chi_mat) to do the test.

It returns the following:

Pearson's Chi-squared test
data:  v
X-squared = 942.32, df = 25, p-value < 2.2e-16


Here, I conclude that the null has to be rejected, as my p-value is significantly below my 0.05.

So, is my logic wrong? From what I understand, I'm supposed to be doing a test from independence, right? The issue is, I'm so not used to this that I don't know what sort of values I should be expecting. I haven't built an intuition for these things yet.

Does this successfully do a chi-squared test, and are my conclusions correct? Thank you.

• The question title asks about R, but you have a mixture of Matlab and R code. Please revise so the code works correctly. – Gregg H Apr 6 '18 at 3:28

There are a number of issues. First, this would be a goodness-of-fit test, not a test of independence. The former compares a multinomial distribution to a hypothesized distribution. The latter compares the observed frequencies in a contingency table to expected frequencies with the same marginal distributions (ie, the columns and rows add to the same values).

The problem here is that you are running test-of-independence using a table that was constructed using the observed frequencies and the expected frequencies. Thus, the answer here is incorrect.

Using R, the correct code would be

chisq.test(v,p=freq)


When I attempted to run this test, there was a problem:

    Chi-squared test for given probabilities

data:  v
X-squared = 16939, df = 25, p-value < 2.2e-16

Warning message:
In chisq.test(v, p = freq) : Chi-squared approximation may be incorrect


This is a consequence of too many cells have expected frequencies less than 5.

• Thank you for your response. This makes more sense now, – iaskdumbstuff Apr 6 '18 at 3:50