Friday, March 18, 2011

Chi squared test of independence in R

1. Create or import table into R
EX: I used the same data from this post, but I had edited the Excel text file to reflect the column and row titles and renamed it "Table2forRhead.txt".

> getwd()
[1] "C:/Documents and Settings/me/My Documents"
>
> setwd("E:")
>
> mydata = read.table("Table2forRhead.txt")
> mydata
Educ Nav
Male 336 355
Female 955 947

2. Run chi square test:
> chisq.test(mydata)

Pearson's Chi-squared test with Yates' continuity correction

data: mydata
X-squared = 0.448, df = 1, p-value = 0.5033

3. Report p-value to test hypothesis of independence
EX: In this problem, p-value = 0.5033, which is > 0.05 (the significance level), therefore we do not reject the null hypothesis that sex is independent of the intervention group of the subjects

BUT... what if we want to test null that invention group is independent of sex (reverse the chi squared test)?

1. Create new txt in Excel, reversing the columns and rows
EX: "Table2forRheadReverse.txt"
2. Repeat above
> getwd()
[1] "E:/"
> mydata2 = read.table("Table2forRheadReverse.txt")
> mydata2
Male Female
Educ 336 955
Nav 355 947
> chisq.test(mydata2)

Pearson's Chi-squared test with Yates' continuity correction

data: mydata2
X-squared = 0.448, df = 1, p-value = 0.5033

THEY ARE THE SAME! So p-value can be interpreted as failing to reject the null hypothesis that sex is independent of the intervention group AND failing to reject the null that intervention group is independent of sex.

No comments:

Post a Comment