# 3-way table: age x smoking x breathing test result # Edps 589 # Fall 2018 # C.J.Anderson # library(vcd) library(vcdExtra) library(MASS) library(DescTools) var.values <- expand.grid(test=c("normal","not normal"), smoke=c("never","current"), age=c("<40","40-59")) counts <- c(577, 34, 682, 57, 164, 4, 245, 74) ast <- cbind(var.values,counts) ast # 2-way marginal table test.by.smoke<- xtabs(counts ~ test+smoke,data=ast) addmargins(test.by.smoke) # 2-way marginal analysis: assocstats(test.by.smoke) # Compute G2 and X2 ( OR <- oddsratio(test.by.smoke) ) # Compute odds ratio and view summary(OR) # signficant test for odds ratio -- don't really # need because have X2 and G2 from assocstats() exp(1.013658) # odds ratio exp(confint(OR) ) # Confidnece interval of odds ratio # 3-way Table of data # -- partial or conditional tables ast.tab <- xtabs(counts ~ test+smoke+age,data=ast) addmargins(ast.tab) # 3-way contitional/partial table analysis assocstats(ast.tab) # Compute X2 and G2 ( OR <- oddsratio(ast.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 # First: Test for homogenity (shouldn't be significant...p should = small WoolfTest(ast.tab)