# R chi square test with degrees of freedom

Does anybody know of a chi-square test function in R for which you can specify the degrees of freedom? Example: I want to test if the Hardy-Weinberg distribution holds true for a sample of the MN blood group.

M <- 3156
MN <- 4997
N <- 1847
FM <- 2*M + MN
FN <- MN + 2*N

indiv.num <- M + MN + N
alleles.num <- (M + MN + N) * 2
alleles.num
p <- FM / alleles.num
q <- FN / alleles.num

M.exp <- p^2 * indiv.num
M.exp
MN.exp <- 2*p*q * indiv.num
MN.exp
N.exp <- q^2 * indiv.num
N.exp

chisq.test(c(M, MN, N), c(M.exp, MN.exp, N.exp))


chisq.test automatically calculates df=4:

    Pearson's Chi-squared test

data:  c(M, MN, N) and c(M.exp, MN.exp, N.exp)
X-squared = 6, df = 4, p-value = 0.1991


But this is not applicable for this case: df must be one, since from the frequency of one allele the frequency of the other allele is known.

• Not so sure but, maybe this one. – maydin Aug 19 '19 at 6:01
• I think you need to tell us more about your data and why you think you need 1 df. You may be trying to solve this using the wrong test / model. – user2974951 Aug 19 '19 at 10:08

It looks like you are trying to perform a chi-square goodness-of-fit test. That's about all I can suss out, so I can't say the following is correct for your purposes. In this case, the last line of your code could be replaced with the following. My advice is to perform the desired test by hand, or to compare to a published example similar to yours, and then compare the R output to be sure the code is doing what you want.

Theoretical = c(M.exp  / sum(M.exp, MN.exp, N.exp),
MN.exp / sum(M.exp, MN.exp, N.exp),
N.exp  / sum(M.exp, MN.exp, N.exp))

Theoretical

chisq.test(c(M, MN, N), p= Theoretical)

• Although it remains unclear why the OP is asking for df being 1. – machine Aug 19 '19 at 11:42

If you want to know the p value for some chi-square value and a certain df you can find it out like that

# example
library(MASS)
tbl = table(survey$$Smoke, survey$$Exer)
chisq.test(tbl)
# here you see that for df= 6 p is 0.4828

chi_val <- chisq.test(tbl)$statistic pchisq(chi_val,df= 6,lower.tail=FALSE) # gives you the same p value  In your case with a chi-square value of 6 and df= 1 you can use pchisq(x= 6,df= 1,lower.tail=FALSE), which is 0.01430588. Here, I set Chi-square to $$6$$ with x= 6 and the degrees of freedom to $$1$$ by using df= 1 to match your example. Please note that I am not sure what your code above is doing. Your variables are saying ".exp", but we do not need to provide expected values for the chisq.test function unless you await certain probabilities (See answer from Sal Mangiafico). This means if your question is whether chi-square for the frequencies c(M, MN, N) is significant, you can use chisq.test(c(M, MN, N)). But here df is 2 since the vector has length 3. • Can you elaborate how you can set df in that particular function? How does that work? – user2974951 Aug 19 '19 at 10:10 • @user2974951 in pchisq(6,df= 1,lower.tail=FALSE) I set the degrees of freedom to$1$by using df= 1. You can choose the df of interest for the df argument inside of pchisq. In this example,$6\$ the chi-square value, which you can set, too. I add the exmplanation to the answer – machine Aug 19 '19 at 10:12