3 Inspired by https://canvas.harvard.edu/courses/5749/assignments/syllabus (STAT E-100, Fall 2015)
5 > options(warn=-1) # warnings from `library` are annoying
6 > options(device="png") # instead of Rplots.pdf
7 > out <- capture.output(suppressMessages(library("mosaic")))
8 > # conduct analyses of Titanic data
9 > # frequency table of counts with marginal totals
10 > tally(~Survived, format = "count", data = Titanic, margins = TRUE)
14 > # bar graph of survival
15 > bargraph(~Survived, data = Titanic)
17 <img src="titanic/Rplot001.png">
19 > # frequency table of propotions with marginal totals
20 > tally(~Survived, format = "proportion", data = Titanic, margins = TRUE)
24 > # frequency table of percentages with marginal totals
25 > tally(~Survived, format = "percent", data = Titanic, margins = TRUE)
29 > # pie chart of survival
30 > pie(tally(~Survived, format = "count", data = Titanic))
32 <img src="titanic/Rplot002.png">
34 > # contingency table of counts
35 > tally(~Class + Survived, format = "count", margins=TRUE, data = Titanic)
42 > # contingency table of proportions without conditioning
43 > tally(~Class + Survived, format = "proportion", margins=TRUE, data = Titanic)
46 Lower 0.353 0.125 0.478
47 Middle 0.140 0.110 0.250
48 Upper 0.099 0.173 0.272
49 Total 0.591 0.409 1.000
50 > # contingency table of percentages without conditioning,
51 > tally(~Class + Survived, format = "percent", margins=TRUE, data = Titanic)
58 > # conditioning on survival
59 > # Of those who survived, what percentage were in the lower class? (31%)
60 > tally(~Class | Survived, format = "percent", data = Titanic, margins=TRUE)
67 > # conditioning on passenger class
68 > # Of those in the lower class, what percent survived? (26%)
69 > tally(~Survived | Class, format = "percent", data = Titanic, margins=TRUE)
71 Survived Lower Middle Upper
75 > # Dodged Bar Graph: Survival with Class Subgroups
76 > bargraph(~Survived, groups=Class, auto.key=TRUE, data=Titanic)
78 <img src="titanic/Rplot003.png">
80 > # Dodged Bar Graph: Class with Survival Subgroups
81 > bargraph(~Class, groups=Survived, auto.key=TRUE, data=Titanic)
83 <img src="titanic/Rplot004.png">
85 > # Stacked Bar Graph: Survival with Class Subgroups
86 > bargraph(~Survived, groups=Class, auto.key=TRUE, stack=TRUE, data=Titanic)
88 <img src="titanic/Rplot005.png">
90 > # Stacked Bar Graph: Class with Survival Subgroups
91 > bargraph(~Class, groups=Survived, auto.key=TRUE, stack=TRUE, data=Titanic)
93 <img src="titanic/Rplot006.png">
95 > # Unconditional Distribution of Survival and Conditional Distributions of Class
96 > mosaicplot(~Survived + Class, color=TRUE, data=Titanic)
98 <img src="titanic/Rplot007.png">
100 > # Distributions of class conditional on survival
101 > tally(~Class | Survived, format = "percent", data = Titanic, margins=TRUE)
108 > # Unconditional Distribution of Class and Conditional Distributions of Survival
109 > mosaicplot(~Class + Survived, color=TRUE, data=Titanic)
111 <img src="titanic/Rplot008.png">
113 > # Distributions of survival conditional on class
114 > tally(~Survived | Class, format = "percent", data = Titanic, margins=TRUE)
116 Survived Lower Middle Upper