# 3-way table: Hypothetical DIF # Edps 589 # Fall 2018 # C.J.Anderson # library(vcd) library(vcdExtra) library(MASS) library(DescTools) var.values <- expand.grid(response=c("yes","no"), group=c("A","B"), ability=c("low","middle","high")) counts <- c(5, 15, 15, 5, 10, 10, 10, 10, 15, 5, 5, 15) dif <- cbind(var.values,counts) dif # 2-way marginal table response.by.group <- xtabs(counts ~ response + group,data=dif) addmargins(response.by.group) # 2-way marginal analysis: assocstats(response.by.group) # Compute G2 and X2 ( OR <- oddsratio(response.by.group) ) # Compute odds ratio and view summary(OR) # signficant test for odds ratio -- don't really # need because have X2 and G2 from assocstats() exp(0) # odds ratio exp(confint(OR) ) # Confidnece interval of odds ratio # 3-way Table of data # -- partial or conditional tables dif.tab <- xtabs(counts ~ response + group + ability,data=dif) addmargins(dif.tab) # 3-way contitional/partial table analysis assocstats(dif.tab) # Compute X2 and G2 ( OR <- oddsratio(dif.tab) ) # Compute odds ratio & view logodds <- summary(OR) # Signficant test for odds ratios exp(logodds[1:3] ) # Odds ratios exp( confint(OR) ) # Confidnece interval of odds ratios # Breslow-Day test of homogeneity BreslowDayTest(dif.tab, OR = NA, correct = FALSE) # First: Test for homogenity (shouldn't be significant...p should = 1 WoolfTest(dif.tab) # Shouldn't do this because WoolfTest significant...but if it wasn't.... # Test for conditional independence CMHtest(dif.tab) # If data are conditionally independent, estimate their Common odds ratio mantelhaen.test(dif.tab,alternative = c("two.sided"), correct = TRUE, exact = FALSE, conf.level = 0.95)