1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Not so sure but, maybe this one. $\endgroup$ – maydin Aug 19 '19 at 6:01
  • 2
    $\begingroup$ 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. $\endgroup$ – user2974951 Aug 19 '19 at 10:08
0
$\begingroup$

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) 
$\endgroup$
  • 2
    $\begingroup$ Although it remains unclear why the OP is asking for df being 1. $\endgroup$ – machine Aug 19 '19 at 11:42
0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Can you elaborate how you can set df in that particular function? How does that work? $\endgroup$ – user2974951 Aug 19 '19 at 10:10
  • $\begingroup$ @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 $\endgroup$ – machine Aug 19 '19 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy