Testing against a custom probability distribution

What I want is some tool that will test an ordinal, discrete variable against a specific probability distribution, say the variable might take 4 values, 1, 2, 3, 4, and my expected probability distribution is that 10% will be 1, 50% will be 2, 30% will be 3 and 10% will be 4.

What program, or application, need I use to check if an empirical dataset comes from the expected distribution, or if it deviates significantly from it? How do I do it?

You can start with Pearson's chi-squared test. It is implemented in R, as a function chisq.test. Here is the example with fictitious data:

 set.seed(1)
#Generate some discrete variable
y<-rpois(30,1)
#Tabulate the values
table(y)
y
0  1  2  3  4
10 12  5  2  1
##Calculate the theoretical probabilities of the values
p<-dpois(0:3,1)
p<-c(p,1-sum(p))
> p
[1] 0.36787944 0.36787944 0.18393972 0.06131324 0.01898816

##Do actual test. You need to supply the table and the corresponding probabilities
chisq.test(table(y),p=p)

Chi-squared test for given probabilities

data:  table(y)
X-squared = 0.5693, df = 4, p-value = 0.9664

Message d'avis :
In chisq.test(table(y), p = p) :
l'approximation du Chi-2 est peut-être incorrecte


See the p-value. If it is bigger than 0.05, your data conforms to the expected probability distribution.

This is just an example, but it will get you started.

• Looks like I really should get around to learning R. most awesome, my thanks – Tomas Boncompte Dec 20 '11 at 17:42
• I know that I state the obvious, but not being able to reject the null hypothesis does not mean that it can be adopted – Boris Gorelik Dec 26 '11 at 19:20
• Note the last sentence. I intentionally left out all the statistical nitpicking, since it was clear that it would not help OP. Is it really necessary to add caveat a la en.wikipedia.org/wiki/P_value to every post where hypothesis testing is mentioned? Finally note also, that I said the data conforms to the expected probability distribution, not that null hypothesis must be adopted. – mpiktas Dec 27 '11 at 7:00

Not a formal test, but to help you get a feel for how well your data matches a distribution you may want to look at a hanging rootogram, there are implementations of this in the vcd package and latticeextra pakages for R.

Also note that any formal test (the chi-squared is appropriate here as @mpiktas posted) can only rule out distributions, it can never prove that your data came from a given distribution, just show that there is not enough data to rule it out.