**460**wrote:

I was searching for a function in R to perform a chi-squared test for hardy-weinberg equilibrium.

Strait-forward would be to compute a chi-squared-value by my own and then take a look at a p-value with a given degree of freedom:

nAA=18

nAa=104

naa=109

n=nAA+nAa+naa

pA=(2*nAA+nAa)/(2*n)

pa=1-pA

obs= c(nAA,nAa,naa)

exp=c(n*pA*pA,2*n*pA*pa,n*pa^2)

sum(((obs-exp)^2)/exp)

[1] 1.001318

1-pchisq(1.001318,df=1)

[1] 0.3169918

My degrees of freedom are 1, as number of genotypes = 3 and of alleles = 2 (df=3-2=1). So, p-value is 0.31699, I am happy, everything is in HWE. Then I decided to have a look if there is a function in R for that within standard packages and have found the following:

chisq.test(obs, p = exp/sum(exp))

Chi-squared test for given probabilities data: obs X-squared = 1.0013, df = 2, p-value = 0.6061

It seems that chisq.test is not suitable for HWE as it computes the df as 2. Are there any functions that can compute the chisq.test for HWE problems?

Thank you.

**2.8k**• written 4.4 years ago by tonja.r •

**460**