titanic <- read.table("data/titanic.csv",
header = TRUE, sep = ",", stringsAsFactors = TRUE
)Descriptive analysis of the titanic data
Programming and Data Analysis with R
Homepage
Topics covered
- Qualitative data
- Contingency tables
- Conditional distributions
Description of the problem
After the Titanic disaster, a commission of inquiry of the British Board of Trade compiled a list of all the 1316 passengers, including the following information:
- the outcome (saved, not saved)
- the class (I, II, III) in which they travelled
- sex, age, and so on.
In this unit we limit ourselves to considering the information on the survived outcome and the class.
Importing the titanic data
As done previously, first of all you need to download the file titanic.csv and save it on your computer. Link to the file
Alternatively, we can simply obtain them using the link:
path <- "https://tommasorigon.github.io/EnvEcoStat/data/titanic.csv"
titanic <- read.table(path, header = TRUE, sep = ",", stringsAsFactors = TRUE)str(titanic)'data.frame': 1316 obs. of 2 variables:
$ survived: Factor w/ 2 levels "No","Yes": 2 2 2 2 2 2 2 2 2 2 ...
$ class : Factor w/ 3 levels "I","II","III": 1 1 1 1 1 1 1 1 1 1 ...
Absolute and relative (marginal) frequencies
We can obtain the (marginal) absolute frequencies of the two variables using the command summary:
summary(titanic) survived class
No :817 I :325
Yes:499 II :285
III:706
Of course, we can obtain the absolute and relative frequencies also using the command table. For example for the variable class, we can use:
freq_abs_class <- table(titanic$class)
freq_rel_class <- freq_abs_class / sum(freq_abs_class)
tab_summary <- cbind(freq_abs_class, freq_rel_class)
tab_summary freq_abs_class freq_rel_class
I 325 0.2469605
II 285 0.2165653
III 706 0.5364742
Joint frequencies
A summary we can produce consists in building a table, called a contingency table or two-way table.
In R one uses, also in this case, the command table, with two arguments:
tab <- table(titanic$survived, titanic$class)
tab
I II III
No 122 167 528
Yes 203 118 178
In this table the joint frequencies are reported: for example, the value 203 represents the number of passengers who travelled in first class and who survived.
Contingency table
Let x and y be two variables having categories c_1,\dots,c_h and d_1,\dots,d_k, respectively.
A contingency table (for two variables) for the pairs of data (x_1,y_1),\dots,(x_n,y_n) takes the following form:
| Variable y | ||||||
|---|---|---|---|---|---|---|
| Variable x | d_1 | \dots | d_j | \dots | d_k | Total |
| c_1 | n_{11} | \dots | n_{1j} | \dots | n_{1k} | n_{1+} |
| \vdots | \vdots | \vdots | \vdots | \vdots | ||
| c_i | n_{i1} | \dots | n_{ij} | \dots | n_{ik} | n_{i+} |
| \vdots | \vdots | \vdots | \vdots | \vdots | ||
| c_h | n_{h1} | \dots | n_{hj} | \dots | n_{hk} | n_{h+} |
| Total | n_{+1} | \dots | n_{+j} | \dots | n_{+k} | n |
The frequency n_{ij} is the number of statistical units that simultaneously present the categories c_i and d_j.
Contingency table, relative frequencies
Dividing each term of the previous table by n, one moreover obtains:
| Variable y | ||||||
|---|---|---|---|---|---|---|
| Variable x | d_1 | \dots | d_j | \dots | d_k | Total |
| c_1 | f_{11} | \dots | f_{1j} | \dots | f_{1k} | f_{1+} |
| \vdots | \vdots | \vdots | \vdots | \vdots | ||
| c_i | f_{i1} | \dots | f_{ij} | \dots | f_{ik} | f_{i+} |
| \vdots | \vdots | \vdots | \vdots | \vdots | ||
| c_h | f_{h1} | \dots | f_{hj} | \dots | f_{hk} | f_{h+} |
| Total | f_{+1} | \dots | f_{+j} | \dots | f_{+k} | 1 |
The relative frequency f_{ij} = n_{ij} / n is therefore the fraction of observations that simultaneously present the categories c_i and d_j.
Joint & marginal frequencies
The tables described in the previous sections are obtained in R as follows:
addmargins(tab) # Adds the (absolute) marginal distributions
I II III Sum
No 122 167 528 817
Yes 203 118 178 499
Sum 325 285 706 1316
tab_rel <- prop.table(tab) # Alternative command: tab / sum(tab)
tab_rel
I II III
No 0.09270517 0.12689970 0.40121581
Yes 0.15425532 0.08966565 0.13525836
addmargins(tab_rel) # Adds the relative marginal distributions
I II III Sum
No 0.09270517 0.12689970 0.40121581 0.62082067
Yes 0.15425532 0.08966565 0.13525836 0.37917933
Sum 0.24696049 0.21656535 0.53647416 1.00000000
Conditional distributions I
Conditional distribution (x \mid y = d_j)
The j-th column shows the distribution of x conditional on y = d_j or, equivalently, the distribution of x given y = d_j.
| Distribution x \mid y = d_j | c_1 | \dots | c_i | \dots | c_h | Total |
|---|---|---|---|---|---|---|
| Absolute frequencies | n_{1j} | \dots | n_{ij} | \dots | n_{hj} | n_{+j} |
| Relative frequencies | n_{1j} / n_{+j} | \dots | n_{ij} / n_{+j} | \dots | n_{hj} / n_{+j} | 1 |
Conditional distribution (y \mid x = c_i)
The i-th row shows the distribution of y conditional on x = c_i or, equivalently, the distribution of y given x = c_i.
| Distribution y \mid x = c_i | d_1 | \dots | d_j | \dots | d_k | Total |
|---|---|---|---|---|---|---|
| Absolute frequencies | n_{i1} | \dots | n_{ij} | \dots | n_{ik} | n_{i+} |
| Relative frequencies | n_{i1} / n_{i+} | \dots | n_{ij} / n_{i+} | \dots | n_{ik} / n_{i+} | 1 |
Conditional distributions II
The command prop.table also makes it possible to compute the relative conditional frequencies.
The distribution of the class, conditional on each outcome, is:
prop.table(tab, 1)
I II III
No 0.1493268 0.2044064 0.6462668
Yes 0.4068136 0.2364729 0.3567134
The distribution of the outcome, conditional on each class, is:
prop.table(tab, 2)
I II III
No 0.3753846 0.5859649 0.7478754
Yes 0.6246154 0.4140351 0.2521246
Graphical representations
It is possible to represent the data of a contingency table graphically through a bar chart, which in R is implemented through the command barplot.
A graphical representation of the joint frequencies is therefore the following:
barplot(tab, beside = TRUE, legend.text = TRUE) # beside = TRUE places the bars side by side
If we were interested in showing the conditional frequencies, we can instead use:
barplot(prop.table(tab, 2),
beside = FALSE,
xlab = "Class",
legend.text = TRUE
) # beside = FALSE stacks the bars.