1 Inspired by https://canvas.harvard.edu/courses/5749 (STAT E-100, Fall 2015)
3 > options(warn=-1) # warnings from `library` are annoying
4 > options(device="png") # instead of Rplots.pdf
5 > out <- capture.output(suppressMessages(library("mosaic")))
6 > # conduct analyses of Titanic data
7 > # frequency table of counts with marginal totals
8 > tally(~Survived, format = "count", data = Titanic, margins = TRUE)
12 > # bar graph of survival
13 > bargraph(~Survived, data = Titanic)
15 <img src="titanic/Rplot001.png">
17 > # frequency table of propotions with marginal totals
18 > tally(~Survived, format = "proportion", data = Titanic, margins = TRUE)
22 > # frequency table of percentages with marginal totals
23 > tally(~Survived, format = "percent", data = Titanic, margins = TRUE)
27 > # pie chart of survival
28 > pie(tally(~Survived, format = "count", data = Titanic))
30 <img src="titanic/Rplot002.png">
32 > # contingency table of counts
33 > tally(~Class + Survived, format = "count", margins=TRUE, data = Titanic)
40 > # contingency table of proportions without conditioning
41 > tally(~Class + Survived, format = "proportion", margins=TRUE, data = Titanic)
44 Lower 0.353 0.125 0.478
45 Middle 0.140 0.110 0.250
46 Upper 0.099 0.173 0.272
47 Total 0.591 0.409 1.000
48 > # contingency table of percentages without conditioning,
49 > tally(~Class + Survived, format = "percent", margins=TRUE, data = Titanic)
56 > # conditioning on survival
57 > # Of those who survived, what percentage were in the lower class? (31%)
58 > tally(~Class | Survived, format = "percent", data = Titanic, margins=TRUE)
65 > # conditioning on passenger class
66 > # Of those in the lower class, what percent survived? (26%)
67 > tally(~Survived | Class, format = "percent", data = Titanic, margins=TRUE)
69 Survived Lower Middle Upper
73 > # Dodged Bar Graph: Survival with Class Subgroups
74 > bargraph(~Survived, groups=Class, auto.key=TRUE, data=Titanic)
76 <img src="titanic/Rplot003.png">
78 > # Dodged Bar Graph: Class with Survival Subgroups
79 > bargraph(~Class, groups=Survived, auto.key=TRUE, data=Titanic)
81 <img src="titanic/Rplot004.png">
83 > # Stacked Bar Graph: Survival with Class Subgroups
84 > bargraph(~Survived, groups=Class, auto.key=TRUE, stack=TRUE, data=Titanic)
86 <img src="titanic/Rplot005.png">
88 > # Stacked Bar Graph: Class with Survival Subgroups
89 > bargraph(~Class, groups=Survived, auto.key=TRUE, stack=TRUE, data=Titanic)
91 <img src="titanic/Rplot006.png">
93 > # Unconditional Distribution of Survival and Conditional Distributions of Class
94 > mosaicplot(~Survived + Class, color=TRUE, data=Titanic)
96 <img src="titanic/Rplot007.png">
98 > # Distributions of class conditional on survival
99 > tally(~Class | Survived, format = "percent", data = Titanic, margins=TRUE)
106 > # Unconditional Distribution of Class and Conditional Distributions of Survival
107 > mosaicplot(~Class + Survived, color=TRUE, data=Titanic)
109 <img src="titanic/Rplot008.png">
111 > # Distributions of survival conditional on class
112 > tally(~Survived | Class, format = "percent", data = Titanic, margins=TRUE)
114 Survived Lower Middle Upper