Quarto Refresher and Publishing
Titanic
INTRODUCTION
The content above in lines 1 through 17 is called the YAML, and it is useful to set the document title and specify the output when you render the file. Rendering options include html, Word, and .pdf. Here, we will render the document into an html file with folded code that wraps. Note, too that we include a table of contents (toc) and we can apply the global options to suppress warnings and messages as well.
This text in the white sections are areas where you can insert narratives and other useful information. The backslash at the end is a carriage return – which means start a new line, otherwise this text will run together.
CODE CHUNKS AND INLINE CODE REFRESHER
The gray section below is called a code chunk. This is where you will place R script.
All code chunks start and end with three tick marks — use the key just below the ESC key on a Windows keyboard.
The first line needs the {r} to indicate that this code chunk is using R script. (Quarto will run with other code types such as Python and you can include different code chunks in the same document. However, for this course, we will only use R throughout.)
You can run all of the commands just in this code chunk by clicking on the green play arrow in the upper right corner of the chunk. Go ahead and run the code below. Note, you may need to install the titanic package first using the Packages tab in the lower right quadrant of RStudio.
DATA UNDERSTANDING
For this exercise, we will use the dataset that is stored in the titanic package. Note that the data are already split into a training and test datasets. We want to use all records, so the first thing we will do is the concatenate the datasets (bind_rows) into one.
View the Data
Let’s understand the structure of the dataset as well as view a few of the first and last observations.
'data.frame': 1309 obs. of 12 variables:
$ PassengerId: int 1 2 3 4 5 6 7 8 9 10 ...
$ Survived : int 0 1 1 1 0 0 0 0 1 1 ...
$ Pclass : int 3 1 3 1 3 3 1 3 3 2 ...
$ Name : chr "Braund, Mr. Owen Harris" "Cumings, Mrs. John Bradley (Florence Briggs Thayer)" "Heikkinen, Miss. Laina" "Futrelle, Mrs. Jacques Heath (Lily May Peel)" ...
$ Sex : chr "male" "female" "female" "female" ...
$ Age : num 22 38 26 35 35 NA 54 2 27 14 ...
$ SibSp : int 1 1 0 1 0 0 0 3 0 1 ...
$ Parch : int 0 0 0 0 0 0 0 1 2 0 ...
$ Ticket : chr "A/5 21171" "PC 17599" "STON/O2. 3101282" "113803" ...
$ Fare : num 7.25 71.28 7.92 53.1 8.05 ...
$ Cabin : chr "" "C85" "" "C123" ...
$ Embarked : chr "S" "C" "S" "S" ...
PassengerId Survived Pclass
1 1 0 3
2 2 1 1
3 3 1 3
Name Sex Age SibSp Parch
1 Braund, Mr. Owen Harris male 22 1 0
2 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0
3 Heikkinen, Miss. Laina female 26 0 0
Ticket Fare Cabin Embarked
1 A/5 21171 7.2500 S
2 PC 17599 71.2833 C85 C
3 STON/O2. 3101282 7.9250 S
PassengerId Survived Pclass Name Sex Age SibSp
1307 1307 NA 3 Saether, Mr. Simon Sivertsen male 38.5 0
1308 1308 NA 3 Ware, Mr. Frederick male NA 0
1309 1309 NA 3 Peter, Master. Michael J male NA 1
Parch Ticket Fare Cabin Embarked
1307 0 SOTON/O.Q. 3101262 7.2500 S
1308 0 359309 8.0500 S
1309 1 2668 22.3583 C
Descriptive Statistics
Note that important variables such as \(Survived\) and \(Age\) have missing values.
PassengerId Survived Pclass Name
Min. : 1 Min. :0.0000 Min. :1.000 Length:1309
1st Qu.: 328 1st Qu.:0.0000 1st Qu.:2.000 Class :character
Median : 655 Median :0.0000 Median :3.000 Mode :character
Mean : 655 Mean :0.3838 Mean :2.295
3rd Qu.: 982 3rd Qu.:1.0000 3rd Qu.:3.000
Max. :1309 Max. :1.0000 Max. :3.000
NA's :418
Sex Age SibSp Parch
Length:1309 Min. : 0.17 Min. :0.0000 Min. :0.000
Class :character 1st Qu.:21.00 1st Qu.:0.0000 1st Qu.:0.000
Mode :character Median :28.00 Median :0.0000 Median :0.000
Mean :29.88 Mean :0.4989 Mean :0.385
3rd Qu.:39.00 3rd Qu.:1.0000 3rd Qu.:0.000
Max. :80.00 Max. :8.0000 Max. :9.000
NA's :263
Ticket Fare Cabin Embarked
Length:1309 Min. : 0.000 Length:1309 Length:1309
Class :character 1st Qu.: 7.896 Class :character Class :character
Mode :character Median : 14.454 Mode :character Mode :character
Mean : 33.295
3rd Qu.: 31.275
Max. :512.329
NA's :1
Passenger Lookup
John Jacob Astor, one of the wealthiest people in the world, was aboard the Titanic. Let’s see if he survived.
Name Survived
701 Astor, Mrs. John Jacob (Madeleine Talmadge Force) 1
1094 Astor, Col. John Jacob NA
Col. Astor was not known to have survived nor was he confirmed as a non-survivor. The NA means he was not recovered. His wife, however, did survive.
Average Age
[1] 29.88
Rather than hardcode the value for the age, we will use inline code to print the value in the narrative. This is useful if the variable you create is dynamic, such as when you frequently update a dataset with new data. To do this, we use inline code, like this:
- The average age of all passengers on board the Titanic was 29.88.
- You will see the answer when you render the code.
PRACTICE CODE CHUNKS AND INLINE CODE
Let’s run just a few more bits of analysis. Specifically, let’s see if we can answer the following questions:
1. How many passengers are male and how many female?
2. How many passengers survived and how many died?
3. How many females survived? How many died?
4. What percent of females survived?
5. What percent of survivors were male?
6. What percent of passengers were females who perished?
7. What is the distribution of passengers by fare class and embark location?
1. Number of Passengers by Gender
The following table shows the number of female and male passengers aboard the Titanic.
female male
466 843
However, it may be nicer to show the answer as inline text vs code output:
- There were 466 females and 843 males aboard the Titanic.
2. Number of Survivors and Non-Survivors
The following table shows the number of known survivors and non-survivors from the Titanic.
Note that many of the passengers were unaccounted (Unsure).
Var1 Freq
1 Did Not Survive 549
2 Survived 342
3 Unsure 418
3. Number of Female Survivors and Non-Survivors
The following table shows the number of survivors and non-survivors by gender.
Did Not Survive Survived Unsure
female 81 233 152
male 468 109 266
- Records show that among the females on board the Titanic, 233 survived and 81 did not survive.
4. Percentage of Female Survivors and Non-Survivors
While the prior table showed the number, this table depicts the percentage of survivors by gender.
Cross-Tabulation, Row Proportions
Sex * Survived.f
Data Frame: titanic
-------- ------------ ----------------- ------------- ------------- ---------------
Survived.f Did Not Survive Survived Unsure Total
Sex
female 81 (17.4%) 233 (50.0%) 152 (32.6%) 466 (100.0%)
male 468 (55.5%) 109 (12.9%) 266 (31.6%) 843 (100.0%)
Total 549 (41.9%) 342 (26.1%) 418 (31.9%) 1309 (100.0%)
-------- ------------ ----------------- ------------- ------------- ---------------
- Among Females on board the Titanic, 50% survived and 17.4% did not survive.
5. Percentage of Survivors that are Male
The following table shows the distribution of survivors by gender.
Cross-Tabulation, Column Proportions
Sex * Survived.f
Data Frame: titanic
-------- ------------ ----------------- -------------- -------------- ---------------
Survived.f Did Not Survive Survived Unsure Total
Sex
female 81 ( 14.8%) 233 ( 68.1%) 152 ( 36.4%) 466 ( 35.6%)
male 468 ( 85.2%) 109 ( 31.9%) 266 ( 63.6%) 843 ( 64.4%)
Total 549 (100.0%) 342 (100.0%) 418 (100.0%) 1309 (100.0%)
-------- ------------ ----------------- -------------- -------------- ---------------
- Among Females on board the Titanic, 31.9% survived.
6. Percent of all Titanic passengers were females who perished
Cross-Tabulation, Column Proportions
Sex * Survived.f
Data Frame: titanic
-------- ------------ ----------------- -------------- -------------- ---------------
Survived.f Did Not Survive Survived Unsure Total
Sex
female 81 ( 14.8%) 233 ( 68.1%) 152 ( 36.4%) 466 ( 35.6%)
male 468 ( 85.2%) 109 ( 31.9%) 266 ( 63.6%) 843 ( 64.4%)
Total 549 (100.0%) 342 (100.0%) 418 (100.0%) 1309 (100.0%)
-------- ------------ ----------------- -------------- -------------- ---------------
- Among the females on board
female_nonsurvivedT% perished
7. Distribution of passengers by fare class and embark location?
Note that passengers embarked at one of three locations (in order).
S: Southampton, England
C: Cherbourg, France
Q: Queenstown, Ireland
Cross-Tabulation, Row Proportions
as.factor(titanic$Pclass) * Embarked.f
--------------------------- ------------ ------------- ------------- ------------- ---------- ---------------
Embarked.f C Q S Unknown Total
as.factor(titanic$Pclass)
1 141 (43.7%) 3 ( 0.9%) 177 (54.8%) 2 (0.6%) 323 (100.0%)
2 28 (10.1%) 7 ( 2.5%) 242 (87.4%) 0 (0.0%) 277 (100.0%)
3 101 (14.2%) 113 (15.9%) 495 (69.8%) 0 (0.0%) 709 (100.0%)
Total 270 (20.6%) 123 ( 9.4%) 914 (69.8%) 2 (0.2%) 1309 (100.0%)
--------------------------- ------------ ------------- ------------- ------------- ---------- ---------------
CREATING PLOTS
Let’s create three plots using ggplot2. We will learn more about the capabilities of ggplot2 over this course. We will practice a few here.
The first two charts are similar – one shows the levels, and the other shows the values in percent.
Total Number of Passengers by Survival Status and Gender
Percentage of Total Passengers by Survival and Gender
This is a similar chart as above, but showing the data labels as a percent of Total Passengers rather than raw values.
Note that we first calculate the percentage using dplyr then we use that data to create the chart. We also add data labels.
# A tibble: 6 × 5
Sex Survived.f Count Total Percentage
<chr> <fct> <int> <int> <dbl>
1 female Did Not Survive 81 1309 6.19
2 female Survived 233 1309 17.8
3 female Unsure 152 1309 11.6
4 male Did Not Survive 468 1309 35.8
5 male Survived 109 1309 8.33
6 male Unsure 266 1309 20.3
Average Age by Fare Class
Create a bar chart that shows the average age of passengers by fare class – 1st Class, 2nd Class, or 3rd Class.
Here we created the plot that shows the average age by fare class. First class Titanic passengers were, on average, older than those in second and third class.
YOUR TURN!
Create and interpret two charts that address embark location and fare class.
Include both the intent, the chart, and the interpretation.
Be prepared to share your charts with the class.
Total Number of Passengers by Embark Location and Fare Class
Here we created a dodged bar chart that shows the relationship between fare class and embarked location of the titanic passengers. The largest amount of passengers in all three classes embarked from Southampton England.
Percentage of Total Passengers by Embark Location and Fare Class
# A tibble: 10 × 5
Pclass Embarked.f Count Total Percentage
<int> <fct> <int> <int> <dbl>
1 1 C 141 1309 10.8
2 1 Q 3 1309 0.229
3 1 S 177 1309 13.5
4 1 Unknown 2 1309 0.153
5 2 C 28 1309 2.14
6 2 Q 7 1309 0.535
7 2 S 242 1309 18.5
8 3 C 101 1309 7.72
9 3 Q 113 1309 8.63
10 3 S 495 1309 37.8
We created a dodged bar chart that shows the percentage of titanic passengers from all class levels and their embarked locations. 70% of total passengers embarked from Southampton England with 38 of the 70% being third class passengers.
###Last sectione intent, the chart, and the interpretation.
Be prepared to share your charts with the class.