add code.org to intro and fix typo
[wiki.git] / r / titanic.R
1 load("~/data/Titanic.RData")
2 #View(Titanic) # used in RStudio
3 options(digits=2) # enough precision
4 options(echo=TRUE) # see commands run by Rscript
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)
11 # bar graph of survival
12 bargraph(~Survived, data = Titanic)
13 # frequency table of propotions with marginal totals
14 tally(~Survived, format = "proportion", data = Titanic, margins = TRUE)
15 # frequency table of percentages with marginal totals
16 tally(~Survived, format = "percent", data = Titanic, margins = TRUE)
17 # pie chart of survival
18 pie(tally(~Survived, format = "count", data = Titanic))
19 # contingency table of counts
20 tally(~Class + Survived, format = "count", margins=TRUE, data = Titanic)
21 # contingency table of proportions without conditioning
22 tally(~Class + Survived, format = "proportion", margins=TRUE, data = Titanic)
23 # contingency table of percentages without conditioning,
24 tally(~Class + Survived, format = "percent", margins=TRUE, data = Titanic)
25 # conditioning on survival
26 # Of those who survived, what percentage were in the lower class? (31%)
27 tally(~Class | Survived, format = "percent", data = Titanic, margins=TRUE)
28 # conditioning on passenger class
29 # Of those in the lower class, what percent survived? (26%)
30 tally(~Survived | Class, format = "percent", data = Titanic, margins=TRUE)
31 # Dodged Bar Graph: Survival with Class Subgroups
32 bargraph(~Survived, groups=Class, auto.key=TRUE, data=Titanic)
33 # Dodged Bar Graph: Class with Survival Subgroups
34 bargraph(~Class, groups=Survived, auto.key=TRUE, data=Titanic)
35 # Stacked Bar Graph: Survival with Class Subgroups
36 bargraph(~Survived, groups=Class, auto.key=TRUE, stack=TRUE, data=Titanic)
37 # Stacked Bar Graph: Class with Survival Subgroups
38 bargraph(~Class, groups=Survived, auto.key=TRUE, stack=TRUE, data=Titanic)
39 # Unconditional Distribution of Survival and Conditional Distributions of Class
40 mosaicplot(~Survived + Class, color=TRUE, data=Titanic)
41 # Distributions of class conditional on survival
42 tally(~Class | Survived, format = "percent", data = Titanic, margins=TRUE)
43 # Unconditional Distribution of Class and Conditional Distributions of Survival
44 mosaicplot(~Class + Survived, color=TRUE, data=Titanic)
45 # Distributions of survival conditional on class
46 tally(~Survived | Class, format = "percent", data = Titanic, margins=TRUE)