I want to write a function that will take two categorical variables and create and display mosaic plot, table of counts, table of row percentages and then finally display a chi-square test. Suppose I have data regarding a persons marital status(married/unmarried) and if they smoke (yes/no) or not. I want it to create a mosaic plot of those two variables, then show me the counts/row percentages of those two variables and finally carry out a chi-square test.
I have attempted the following:
Fnct <- function(x, y) {
# Will create mosaic plot, but the labels show up incorrectly
plot <- mosaicplot(~x + y, color=TRUE, main = "Mosaic Plot", xlab = x, ylab = y)
#creates a 2by2 table and stores it in my table
mytable <- table(x, y)
mytable2 <- prop.table(mytable, 1)
chi <- chisq.test(mytable)
return(c(plot, mytable2, chi))
}
Fnct(Data$Marital, Data$Smoker)
When I output the data, it does output the mosaic plot, but the labels are incorrect. They repeatedly repeat the levels of the treatment, but not just the column name. It does not output the counts or chi-square tests properly either. What am I doing incorrectly?