## -----------------------------------------------------------------------------
#| echo: false
titanic <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/titanic/titanic.txt')
table(titanic$Class, titanic$Survived) |> addmargins(2)


## -----------------------------------------------------------------------------
#| echo: false
table(titanic$Sex, titanic$Survived) |> addmargins(2)


## -----------------------------------------------------------------------------
#| echo: false
temptanic<-titanic # assign temporary copy of data set
temptanic$Survived[temptanic$Survived=="Died"]<-"Total" # rename level "Died" to "Total"

tablerone<-with(temptanic, table(Class,Survived,Sex))
tablerone[,2,]<-tablerone[,1,]+tablerone[,2,] # sum counts of "Survived" and "Died" to define "Total"
print(tablerone)


## -----------------------------------------------------------------------------
# read in our dataset
# What information does each column contain?
titanic <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/titanic/titanic.txt')


## -----------------------------------------------------------------------------
# create table of counts
tclass <- table(titanic$Class, titanic$Survived) |>
  addmargins() # creates automatic 'sum' column
tclass[,2] / tclass[,3] # divide count of 'survived' by the total


## -----------------------------------------------------------------------------
classtable <- table(titanic$Sex,titanic$Class,titanic$Survived)
classes <- prop.table(classtable, 1:2)[,,2]
t(classes) 


## -----------------------------------------------------------------------------
weights <- with(titanic, table(Sex)) |> 
  prop.table()
weights


## -----------------------------------------------------------------------------
# first we create vectors that contain only survival proportions for females and males, respectively
fem_props <- t(classes)[1:4, 1]
mal_props <- t(classes)[1:4, 2]

# we can now weight those proportions by the percentage of passengers of each sex
(fem_props * weights[1]) + (mal_props * weights[2])


## -----------------------------------------------------------------------------
# Create a table of Sex × Survived counts
sextab <- table(titanic$Sex, titanic$Survived)

# Compute proportion survived for each sex
prop_survived_sex <- sextab[, "Survived"] / rowSums(sextab)

# Print the result
prop_survived_sex


## -----------------------------------------------------------------------------
# Create a 3-way table: Class × Sex × Survived
classtab <- table(titanic$Class, titanic$Sex, titanic$Survived)

# Compute survival proportion for each Class × Sex
prop_survived <- classtab[,, "Survived"] / 
                 (classtab[,, "Survived"] + classtab[,, "Died"])

# Print only the final table
prop_survived


## -----------------------------------------------------------------------------
# Create a table of counts for each class (all sexes combined)
class_counts <- table(titanic$Class)

# Compute proportion of total passengers in each class
class_proportions <- class_counts / sum(class_counts)

# Print the result
class_proportions


## -----------------------------------------------------------------------------
# survival proportions by sex and class
fem_props <- t(classes)[1:4, 1]  # female survival rates per class
mal_props <- t(classes)[1:4, 2]  # male survival rates per class

# proportion of passengers in each class (class weights)
class_counts <- table(titanic$Class)
total_passengers <- sum(class_counts)
class_weights <- class_counts / total_passengers

# weighted average survival for each sex, controlling for class
weighted_survival_female <- sum(fem_props * class_weights)
weighted_survival_male   <- sum(mal_props * class_weights)

# combine into a vector
weighted_survival_by_sex <- c(F = weighted_survival_female,
                              M = weighted_survival_male)

# print
weighted_survival_by_sex

