# Chi-square test of homogeneity: Cocaine addiction treatments # there are two treatments: desiprimine and lithium # outcome is relapse: yes or no # there are two groups of n=24 each desip <- c(14,10) lithium <- c(6,18) outcome_table <- cbind(desip,lithium) rownames(outcome_table) <- cbind("no","yes") outcome_table cocaine_test <- chisq.test(outcome_table, correct=0) attach(cocaine_test) cocaine_test chisqdif <- observed - expected chisqterms <- residuals^2 observed expected chisqdif chisqterms detach(cocaine_test) # Chi-square test of homogeneity: student attitudes # there are three groups: rural(n=149), suburban(n=151), urban(n=178) # there are three outcome choices: athletic, grades, popular rural <- c(42,57,50) suburb <- c(22,87,42) urban <- c(26,103,49) outcome_table <- cbind(rural,suburb,urban) rownames(outcome_table) <- cbind("athletic","grades","popular") outcome_table attitude_test <- chisq.test(outcome_table, correct=0) attach(attitude_test) attitude_test chisqdif <- observed - expected chisqterms <- residuals^2 observed expected chisqdif chisqterms detach(attitude_test) # Chi-square test of independence: blood type and ethnicity # there are 4 blood types: A AB B O # there are 4 ethnic groups: H HC HW W # there are 145057 individuals type_A <- c(2490,2368,4671,50008) type_AB <- c(99,243,236,5001) type_B <- c(178,568,606,16252) type_O <- c(1903,2206,4469,53759) outcome_table <- cbind(type_A,type_AB,type_B,type_O) rownames(outcome_table) <- cbind("H","HC","HW","W") outcome_table bloodXethnic_test <- chisq.test(outcome_table, correct=0) attach(bloodXethnic_test) bloodXethnic_test chisqdif <- observed - expected chisqterms <- residuals^2 observed expected chisqdif chisqterms detach(bloodXethnic_test) -------- results --------------------------------------- > # Chi-square test of homogeneity: Cocaine addiction treatments > # there are two treatments: desiprimine and lithium > # outcome is relapse: yes or no > # there are two groups of n=24 each > desip <- c(14,10) > lithium <- c(6,18) > outcome_table <- cbind(desip,lithium) > rownames(outcome_table) <- cbind("no","yes") > outcome_table desip lithium no 14 6 yes 10 18 > cocaine_test <- chisq.test(outcome_table, correct=0) > attach(cocaine_test) > cocaine_test Pearson's Chi-squared test data: outcome_table X-squared = 5.4857, df = 1, p-value = 0.01917 > chisqdif <- observed - expected > chisqterms <- residuals^2 > observed desip lithium no 14 6 yes 10 18 > expected desip lithium no 10 10 yes 14 14 > chisqdif desip lithium no 4 -4 yes -4 4 > chisqterms desip lithium no 1.600000 1.600000 yes 1.142857 1.142857 > detach(cocaine_test) > # Chi-square test of homogeneity: student attitudes > # there are three groups: rural(n=149), suburban(n=151), urban(n=178) > # there are three outcome choices: athletic, grades, popular > rural <- c(42,57,50) > suburb <- c(22,87,42) > urban <- c(26,103,49) > outcome_table <- cbind(rural,suburb,urban) > rownames(outcome_table) <- cbind("athletic","grades","popular") > outcome_table rural suburb urban athletic 42 22 26 grades 57 87 103 popular 50 42 49 > attitude_test <- chisq.test(outcome_table, correct=0) > attach(attitude_test) > attitude_test Pearson's Chi-squared test data: outcome_table X-squared = 18.8276, df = 4, p-value = 0.0008497 > chisqdif <- observed - expected > chisqterms <- residuals^2 > observed rural suburb urban athletic 42 22 26 grades 57 87 103 popular 50 42 49 > expected rural suburb urban athletic 28.05439 28.43096 33.51464 grades 76.99372 78.02720 91.97908 popular 43.95188 44.54184 52.50628 > chisqdif rural suburb urban athletic 13.945607 -6.430962 -7.514644 grades -19.993724 8.972803 11.020921 popular 6.048117 -2.541841 -3.506276 > chisqterms rural suburb urban athletic 6.9322457 1.4546562 1.6849315 grades 5.1919686 1.0318351 1.3205252 popular 0.8322674 0.1450536 0.2341429 > detach(attitude_test) > # Chi-square test of independence: blood type and ethnicity > # there are 4 blood types: A AB B O > # there are 4 ethnic groups: H HC HW W > # there are 145057 individuals > type_A <- c(2490,2368,4671,50008) > type_AB <- c(99,243,236,5001) > type_B <- c(178,568,606,16252) > type_O <- c(1903,2206,4469,53759) > outcome_table <- cbind(type_A,type_AB,type_B,type_O) > rownames(outcome_table) <- cbind("H","HC","HW","W") > outcome_table type_A type_AB type_B type_O H 2490 99 178 1903 HC 2368 243 568 2206 HW 4671 236 606 4469 W 50008 5001 16252 53759 > bloodXethnic_test <- chisq.test(outcome_table, correct=0) > attach(bloodXethnic_test) > bloodXethnic_test Pearson's Chi-squared test data: outcome_table X-squared = 1078.604, df = 9, p-value < 2.2e-16 > chisqdif <- observed - expected > chisqterms <- residuals^2 > observed type_A type_AB type_B type_O H 2490 99 178 1903 HC 2368 243 568 2206 HW 4671 236 606 4469 W 50008 5001 16252 53759 > expected type_A type_AB type_B type_O H 1916.749 179.6117 566.7474 2006.892 HC 2210.212 207.1111 653.5192 2314.158 HW 4096.999 383.9151 1211.4074 4289.679 W 51313.041 4808.3621 15172.3259 53726.271 > chisqdif type_A type_AB type_B type_O H 573.2515 -80.61167 -388.74742 -103.89239 HC 157.7878 35.88890 -85.51924 -108.15750 HW 574.0013 -147.91514 -605.40743 179.32123 W -1305.0407 192.63791 1079.67409 32.72867 > chisqterms type_A type_AB type_B type_O H 171.44516 36.179394 266.65239 5.37828016 HC 11.26453 6.218947 11.19101 5.05499110 HW 80.41925 56.988864 302.55565 7.49615614 W 33.19100 7.717672 76.83042 0.01993747 > detach(bloodXethnic_test) > >