---
title: "Lab 2: Tables"
subtitle: 'BIOS:4120 - Introduction to Biostatistics'
output:
  html_document:
    toc: true
    toc_float: true
date: "January 23 - 24, 2024"
---

## Objectives 
In today's lab we will:

1. Learn to make tables and proportion tables in R
2. Learn bracketing techniques for indexing tables
3. Work on calculating weighted averages of data, adjusting for confounders


## Constructing Tables
First, let's read in the 'titanic' dataset and compute some summary statistics. 
```{r}
titanic <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/titanic/titanic.txt')
summary(titanic)
```

By default, when the summary() function encounters categorical data, it produces a table for that column, as evidenced above, when it created 4 separate tables.  A better way to look at the variables in the dataset would be to use the table() function. Recall from lab 1 that we can extract vectors from the dataset using the '$' symbol:

```{r}
table(titanic$Class)
```

But the table function is more versatile than that.  For example, we can create tables using 2 variables:
```{r}
table(titanic$Class,titanic$Survived)
```

If we give the function more than two variables, it creates multiple tables (one for each level):  
```{r}
table(titanic$Class, titanic$Survived, titanic$Sex)
```
It is recommended to keep the number of variables down to 2 or 3, as more than that begins to get a bit cluttered and confusing.

If we save a table, we can use square brackets to access individual numbers [row,column]. When the row or column entry is left blank using this type of indexing, R will take all the rows or columns (whichever one was left blank):
```{r}
class_surv <- table(titanic$Class,titanic$Survived)

class_surv

class_surv[3,2] 
# The 3 indicates the third row and the 2 indicates the second column, 
# so this is the number of 3rd class passengers who survived.

class_surv[4, 1]

class_surv[,2] 
# Note that the column position is left blank. This extracts all rows and 2nd column.
# In the context of the data, this shows us the number of people that survived in each class.

class_surv[3,]
# Note that the row position is left blank. This extracts the 3rd row (3rd class) and all columns.
# In the context of the data, this shows us the number of people who died and survived from the 3rd class.
```

Additionally, we can use the "==" to access data with certain specifications. For instance, if we wanted to access only the data in which people survived, we could use the following code:
```{r}
justSurv <- titanic[titanic$Survived == "Survived",]
# Note that the name of the characteristic you want should be in quotations.
# ie, == "Survived",] is different from == Survived,]. The latter will produce an error code.

# Or if we only want 2nd class travelers.

justSecond <- titanic[titanic$Class == "2nd",]
```


We can also use prop.table() to get the proportions for each cell of a table:
```{r}
prop.table(class_surv, 1) # Gives proportions for each class (by row)

prop.table(class_surv, 2) # Gives proportions for the survival groups (by column)
```


## Weighted Averages (Manual)
Let's investigate Titanic survival rates based on class.  
(For the sake of practice, we will do this manually and then with an R function.)  

Recall the steps for computing a weighted average:

  1) Identify the outcome, the group you are interested in, and the confounder that you are adjusting for
  
  2) Calculate $w_1, w_2, ..., w_n$, the overall proportion of observations that belong to each level of the confounder
  
  3) Calculate $\bar{x}_1,\bar{x}_2, ..., \bar{x}_n$, the average (or percentage) for that group at each level of the confounder
  
  4) Calculate the weight average $\bar{x} = \Sigma_iw_i\bar{x}_i$
```{r,echo=FALSE,comment=''}

temptanic<-titanic
temptanic$Survived[temptanic$Survived=="Died"]<-"Total"

tablerone<-with(temptanic, table(Class,Survived,Sex))
tablerone[,2,]<-tablerone[,1,]+tablerone[,2,]
print(tablerone)
```

##### Part A: 
From "class_surv" above, calculate the overall percentages of survival for each class.

##### Part B: 
Now, create a table listing the percentage of passengers in each class who survived, broken down by sex.  

##### Part C:
Create a table listing the total proportion of male and female passengers on the ship.

##### Part D:  
Finally, construct a weighted average of the percentage of passengers in each class who survived, controlling for the effect of sex (i.e., report one number for each class).

*Do any of these results surprise you? What changed in Part a when compared to Part d? What conclusions can we draw from this?* 

\newpage

## Answers (Manual)

<details><summary>Part A</summary>

```{r,echo=FALSE}
class_surv_prop <- prop.table(class_surv, 1)
class_surv_prop[,2]
```

</details>

<details><summary>Part B</summary>

```{r,echo=FALSE}
justsurv_sex <- prop.table(table(titanic$Class, titanic$Sex, titanic$Survived), 1:2)[,,2]
justsurv_sex #x
```

</details>

<details><summary>Part C</summary>

```{r,echo=FALSE}
prop.table(table(titanic$Sex))
```

</details>

<details><summary>Part D</summary>
```{r,echo=FALSE}
overall <- prop.table(table(titanic$Sex)) #proportion of overall gender

cat("1st : ",signif(sum(justsurv_sex[1,]*overall),3), "\n")
cat("2nd : ",signif(sum(justsurv_sex[2,]*overall),3), "\n")
cat("3rd : ",signif(sum(justsurv_sex[3,]*overall),3), "\n")
cat("Crew: ",signif(sum(justsurv_sex[4,]*overall),3), "\n")
```

</details>

## Weighted Averages (R)
We can also use R to solve these problems. There is no simple function that allows you to calculate the weighted average. Below are a few of ways to do this. Note that the first method could introduce mistakes since you are inputting values individually and also could be a lengthy process depending on the structure of the dataset. Typically, we would prefer to use the second method which is more efficient and has less opportunity for error.  

```{r}
overallSex <- prop.table(table(titanic$Sex))

#First Method
firstclass <- c(141, 62) / c(145, 180) # From table provided

(first_method1 <- weighted.mean(firstclass, overallSex))


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

(second_method1 <- weighted.mean(classes[,1], overallSex))
```

\newpage 

## Practice

*Now let's say that we want to investigate the difference in survival by sex for the Titanic data set. Construct a weighted average of the percentage of passengers for each sex who survived, controlling for the effect of class. For the sake of learning the process do not use the weighted.mean() function except to check your answer*

## Practice Answers

<details><summary>Manual Solution</summary>

```{r, echo=FALSE}
# Table for table purposes
temptanic<-titanic
temptanic$Survived[temptanic$Survived=="Died"] <- "Total"

tablerone <- with(temptanic, table(Sex,Survived,Class))
tablerone[,2,] <- tablerone[,1,] + tablerone[,2,]
print(tablerone)
```

First we find the proportion of people who survived by sex:

```{r,echo=FALSE}
sex_surv <- table(titanic$Sex,titanic$Survived)
sex_surv_prop <- prop.table(sex_surv, 1)

sex_surv_prop[,2]
```

Then we find the percentage of passengers of each sex who survived broken down by class:

```{r,echo=FALSE}
justsurv_class <- prop.table(table(titanic$Sex, titanic$Class, titanic$Survived), 1:2)[,,2]
justsurv_class
```

Next we create a table listing the total proportion of passengers on the ship in each class:

```{r,echo=FALSE}
prop.table(table(titanic$Class))

```

Finally we construct a weighted average of the percentage of passengers of each sex who survived, controlling for the effect of class:

```{r,echo=FALSE}
overall_practice <- prop.table(table(titanic$Class)) #proportion of overall gender

cat("F : ",signif(sum(justsurv_class[1,]*overall_practice),3), "\n")
cat("M : ",signif(sum(justsurv_class[2,]*overall_practice),3), "\n")

```

</details>

<details><summary>R Solution</summary>

Then we can check our answer using the weighted.mean() function:

```{r}
overallclass <- prop.table(table(titanic$Class))

#Second Method
practice_table <- table(titanic$Class,titanic$Sex,titanic$Survived)
sexes <- prop.table(practice_table, 1:2)[,,2]

(weighted.mean(sexes[,1], overallclass))
(weighted.mean(sexes[,2], overallclass))

```
</details>




