# R code for chapter 51 mosaic and extended association plots.r # Book details: # Health Science Statistics using R and R Commander # by Robin Beaumont # Paperback - ISBN 9781907904318 # http://www.amazon.co.uk/Health-Science-Statistics-using-Commander/dp/190790431X # or direct from publisher at: # http://www.scionpublishing.com/ # book website: http://www.robin-beaumont.co.uk/rbook ########################################################### ### # using the Amoxicillin/Erythromycin data described in the chapter # Comparing several independent categories, section Equivalent analysis directly in R. # thedata <- matrix( c(144, 96, 160, 80), nrow= 2, ncol=2, dimnames = list(outcome = c("improved at 5 days", "NO improvement at 5 days"), antibiotic =c("Amoxicillin", "Erythromycin"))) thedata # get a printout of the data # now draw the moasic plot using the vcd package: install.packages("vcd", dependencies=TRUE) library(vcd) mosaic(thedata, shade=TRUE, legend=TRUE) ######## more data showing significant proportions: thedata <- matrix( c (110, 120,160,80), ncol=2, byrow=FALSE) mycol_names <- c("Amoxicillin", "Erythromycin") myrow_names <- c("improved", "no_improve") dimnames(thedata) <- list(status = myrow_names, antibiotic = mycol_names) thedata myresult <- chisq.test(thedata) myresult # 50.2 Mosaic plot options - shading and residual types # default; using components of Pearson’s chi-squared mosaic(thedata, residuals_type = "pearson", gp = shading_Friendly ) # using components of the likelihood ratio chi-squared mosaic(thedata, residuals_type = "deviance", gp = shading_Friendly ) # using Freeman-Tukey residuals mosaic(thedata, residuals_type = "FT", gp = shading_Friendly ) ############# tips # Remember if you have raw data you need to wrap the data in the table() function: # mosaic(thecountdata, residuals_type = "pearson", gp = shading_Friendly ) # or # mosaic(table(therawdata), residuals_type = "pearson", gp = shading_Friendly ) ########## end ####### load(url("http://www.robin-beaumont.co.uk/virtualclassroom/book2data/healthwagedata.rda")) # or # http://www.robin-beaumont.co.uk/virtualclassroom/book2data/healthwagedata.sav