* ======================================================================= * File: 2x2.SPS . * Date: 10-Feb-2005 . * Author: Bruce Weaver, bweaver@lakeheadu.ca . * Notes: Calculate various statistics for 2x2 tables . * ======================================================================= . * This file calculates the following statistics for 2x2 tables: - Pearson chi-square - Pearson chi-square with Yates' correction - likelihood ratio chi-square (sometimes called G-squared) - relative risk (user sets value of delta) - odds ratio (user sets value of delta). * The user can replace the data list below with their own data. * Read in an ID# and the 4 cell counts (a, b, c, d) . DATA LIST LIST /id (f5.0) a (f8.0) b (f8.0) c (f8.0) d (f8.0). BEGIN DATA. 1 15 44 22 52 2 3 27 10 20 3 6 34 5 40 4 8 22 9 23 5 12 88 13 87 6 10 73 21 59 END DATA. var lab id 'Trial'. compute d.rr = 0.5. /* ---------- delta for RR computations [0 or 0.5] . compute d.or = 0.5. /* ---------- delta for OR computations [0 or 0.5] . exe. var lab d.rr 'Delta (RR)' d.or 'Delta (OR)'. * ----------------------------------------------------------------------- . * Compute marginal totals . compute row1.sum = a+b. compute row2.sum = c+d. compute col1.sum = a+c. compute col2.sum = b+d. compute n = a+b+c+d. exe. format row1.sum to n (f8.0). * ----------------------------------------------------------------------- . * Calculate expected frequencies . compute exp.a = row1.sum*col1.sum/n. compute exp.b = row1.sum*col2.sum/n. compute exp.c = row2.sum*col1.sum/n. compute exp.d = row2.sum*col2.sum/n. compute min.exp = min(exp.a,exp.b,exp.c,exp.d). exe. * ----------------------------------------------------------------------- . * Calculate Pearson chi-square (without Yates' correction) . * Use temporary (or "scratch") variables (starting with #) to compute the (O-E)**2/E term for each cell, then sum them. compute #a = (a-exp.a)**2/exp.a. compute #b = (b-exp.b)**2/exp.b. compute #c = (c-exp.c)**2/exp.c. compute #d = (d-exp.d)**2/exp.d. compute chisqr = #a + #b + #c + #d. compute df = 1. compute p.chisqr = 1 - CDF.CHISQ(chisqr,df). exe. format chisqr (f8.3) / p.chisqr (f8.4)/ df(f2.0). var lab min.exp 'Minimum expected frequency' chisqr 'Pearson chi-square' p.chisqr 'p (Pearson)'. * ----------------------------------------------------------------------- . * Calculate chi-square WITH Yates' correction. compute #a = (abs(a-exp.a)-.5)**2/exp.a. compute #b = (abs(b-exp.b)-.5)**2/exp.b. compute #c = (abs(c-exp.c)-.5)**2/exp.c. compute #d = (abs(d-exp.d)-.5)**2/exp.d. compute yates = #a + #b + #c + #d. compute p.yates = 1 - CDF.CHISQ(yates,df). exe. format yates (f8.3) / p.yates (f8.4) . var lab yates 'Chi-sqare (Yates)' p.yates 'p (Yates)'. * ----------------------------------------------------------------------- . * Calculate likelihood ratio chi-square . compute #a = a*ln(a/exp.a). compute #b = b*ln(b/exp.b). compute #c = c*ln(c/exp.c). compute #d = d*ln(d/exp.d). compute lrc = 2*(#a + #b + #c + #d). compute p.lrc = 1 - CDF.CHISQ(lrc,df). exe. format lrc (f8.3) / p.lrc (f8.4) . var lab lrc 'Likelihood ratio chi-square' p.lrc 'p (LR chi-sqr)'. * ----------------------------------------------------------------------- . * Calculate relative risk (Row 1 relative to Row 2) . * Let Y1 = ln(rr). compute risk1 = (a+d.rr)/(row1.sum+d.rr*2). compute risk2 = (c+d.rr)/(row2.sum+d.rr*2). compute rr = risk1/risk2. compute y1 = ln(rr). compute var.y1 = 1/(a+d.rr) - 1/(row1.sum+d.rr*2) + 1/(c+d.rr) - 1/(row2.sum+d.rr*2). compute se.y1 = sqrt(var.y1). compute ll.y1 = y1 - 1.96*se.y1. compute ul.y1 = y1 + 1.96*se.y1. compute ll.rr = exp(ll.y1). compute ul.rr = exp(ul.y1). compute z.rr = y1/se.y1. compute p.rr = 2*(1-(CDF.NORMAL(abs(z.rr),0,1))) . exe. format risk1 to z.rr (f8.3) / p.rr (f8.4). var lab risk1 'Row 1 risk' risk2 'Row 2 risk' rr 'RR' y1 'Y1=ln(RR)' var.y1 'Var(Y1)' se.y1 'SE(Y1)' ll.y1 '95% CI lower (Y1)' ul.y1 '95% CI upper (Y1)' ll.rr '95% CI lower (RR)' ul.rr '95% CI upper (RR)' z.rr 'Z-test (RR)' p.rr 'p-value (RR)'. * ----------------------------------------------------------------------- . * Calculate odds ratio (row 1 relative to row 2). * Let Y2 = ln(OR). compute odds1 = (a+d.or)/(b+d.or). compute odds2 = (c+d.or)/(d+d.or). compute oratio = odds1/odds2. compute y2 = ln(oratio). compute var.y2 = 1/(a+d.or) + 1/(b+d.or) + 1/(c+d.or) + 1/(d+d.or). compute se.y2 = sqrt(var.y2). compute ll.y2 = y2 - 1.96*se.y2. compute ul.y2 = y2 + 1.96*se.y2. compute ll.or = exp(ll.y2). compute ul.or = exp(ul.y2). compute z.or = y2/se.y2. compute p.or = 2*(1-(CDF.NORMAL(abs(z.or),0,1))) . exe. format odds1 to z.or (f8.3) / p.or (f8.4). var lab odds1 'Row 1 odds' odds2 'Row 2 odds' oratio 'OR' y2 'Y2=ln(OR)' var.y2 'Var(Y2)' se.y2 'SE(Y2)' ll.y2 '95% CI lower (Y2)' ul.y2 '95% CI upper (Y2)' ll.or '95% CI lower (OR)' ul.or '95% CI upper (OR)' z.or 'Z-test (OR)' p.or 'p-value (OR)'. * ----------------------------------------------------------------------- . summarize table = id a b c d row1.sum row2.sum col1.sum col2.sum n exp.a exp.b exp.c exp.d min.exp /title = 'Summary of the 2x2 tables' /cells = none /format = list nocasenum /stat = none. * NOTE: If minimum expected frequency < 5, Fisher Exact test is recommended . summarize table = id chisqr p.chisqr yates p.yates lrc p.lrc /title = 'Chi-square tests' /cells = none /format = list nocasenum /stat = none. summarize table = id d.rr risk1 risk2 rr ll.rr ul.rr z.rr p.rr /title = 'Relative Risks' /cells = none /format = list nocasenum /stat = none. summarize table = id d.or odds1 odds2 oratio ll.or ul.or z.or p.or /title = 'Odds Ratios' /cells = none /format = list nocasenum /stat = none. * ======================================================================= .