# R-Code from March 29th. #We "examined" the Titanic data with the 5 Ws deducible from information here: #http://www.encyclopedia-titanica.org/manifest.php?q=1%22%22 #We have (imagined!) breaking the community up into three categories via the three variables Gender, Survival, Class/Crew. #Legend #F=Female passengers #M=Male passengers #A=Alive, i.e. survivors #D=Dead, i.e.victims #1=First class passengers #2=Second class passengers #3=Third class passengers #c=Crew ################### # Our first contingency table! # Rows F,M # Columns A, D #The table: A=rbind( c(266,500), c(444,991)) # Type A to see it! # we can compute the margins as follows... # The Gender margin: M1=margin.table(A,1) # The survival margin: M2=margin.table(A,2) ################# #Here is the example in the book. # Rows A, D # Columns 1, 2, 3, C A=rbind( c(202,118,178,212), c(123,167,528,673)) M1=margin.table(A,1) M2=margin.table(A,2) # We can use this contingency table and it's margins to produce the possible Relative Frequency tables with the distributions conditioned on: # 1. Survival F1=diag(1/M1)%*%A #2. Class/Crew. F2=A%*%diag(1/M2) #Exercise: Figure out what diag(1/M2) did to M2. Why was this useful to us? ################## # We can plot the percent of each Class/Crew who Survived. barplot(F2[1,],names.arg=c("First","Second","Third","Crew"),main="Percent Survival by Class",col=rep('blue',4)) # Exercise: Use R to decide what F2[1,] does to F2. # quartz() tells the computer to make a new graph. quartz() # We can compare the Survivors by Class pie(F1[1,],labels=c("First","Second","Third","Crew"),main="Class Breakdown for Survivors") # With the Victims by Class. quartz() pie(F1[2,],labels=c("First","Second","Third","Crew"),main="Class breakdown for Victims") #They are clearly not independent variables hence are associated. # Exercise: Change the tiles and category labels.