-
Notifications
You must be signed in to change notification settings - Fork 0
/
rChapter6-1.Rmd
119 lines (86 loc) · 6.63 KB
/
rChapter6-1.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
---
title: "Exploring group-specific discrepancies"
description: |
Chapter 6.1 Comparing Within-Group Discrepancies
output: distill::distill_article
---
```{r setup, include=FALSE}
# Load required packages
library(here)
source(here("source", "load_libraries.R"))
# Output options
knitr::opts_chunk$set(eval=TRUE, echo=TRUE)
options("kableExtra.html.bsTable" = T)
# load data for Chapter 2
load(here("data", "6-0_ChapterSetup.RData"))
```
```{r, xaringanExtra-clipboard, echo=FALSE}
htmltools::tagList(
xaringanExtra::use_clipboard(
button_text = "<i class=\"fa fa-clone fa-2x\" style=\"color: #301e64\"></i>",
success_text = "<i class=\"fa fa-check fa-2x\" style=\"color: #90BE6D\"></i>",
error_text = "<i class=\"fa fa-times fa-2x\" style=\"color: #F94144\"></i>"
),
rmarkdown::html_dependency_font_awesome()
)
```
<details><summary>**Click here to get instructions...**</summary>
- Please download and unzip the replication files for Chapter 6
([`r fontawesome::fa("far fa-file-zipper")` Chapter06.zip](source/Chapter06.zip)).
- Read `readme.html` and run `6-0_ChapterSetup.R`. This will create `6-0_ChapterSetup.RData` in the sub folder `data/R`. This file contains the data required to produce the table shown at the bottom of this page.
- We also recommend to load the libraries listed in the Chapter 6's `LoadInstallPackages.R`
```{r, eval=FALSE}
# assuming you are working within .Rproj environment
library(here)
# install (if necessary) and load other required packages
source(here("source", "LoadInstallPackages.R"))
# load environment generated in "6-0_ChapterSetup.R"
load(here("data", "R", "6-0_ChapterSetup.RData"))
```
</details>
\
**Table 6.1** in Chapter 6.1 presents dicrepancies and complexity scores for different subgroups in our example datasets on family and labor market trajectories (yearly granularity) between age 18 to 40 (monthly data). The sequences are stored in the objects `partner.child.year.seq` and `activity.year.seq`. The data frames `family` and `activity` include information on the grouping variables `east` (living in East vs West Germany), `sex` (male vs. female), `highschool` (at least highschool degree: yes vs. no).
The computation of the discrepancies requires a dissimilarity matrix as an input. The two dissimilarity matrices `partner.child.year.om` and `activity.year.om` come from an OM analysis using constant substitution costs of 2 and indel costs of 1. All objects required for the analysis are stored in `6-0_ChapterSetup.RData`(see instructions above). Discrepancies are obtained by [`{TraMineR}`](http://traminer.unige.ch){target="_blank"}'s `dissassoc` function.
The following code snippet illustrates the function comparing the discrepancies in women's and men's labor market biographies.
```{r echo=TRUE, eval=TRUE}
discr_sex <- dissassoc(activity.year.om,activity$sex)
discr_sex
```
The output indicates a statistically significant difference (Levene Test) in the discrepancies of women ($=$ `r round(discr_sex$groups[2,2],1)`) and men ($=$ `r round(discr_sex$groups[1,2],1)`). The pairwise dissimilarities in the sequence of women are greater than those of men.
Next to discrepancies **Table 6.1** also displays group-specific complexity values which are obtained by `seqici`. With `t.test`we test for the equality of the average complexities of men and women.
```{r echo=TRUE, eval=TRUE}
complex_men <- seqici(activity.year.seq[activity$sex == 0,])
complex_women <- seqici(activity.year.seq[activity$sex == 1,])
complex_sex <- t.test(complex_men, complex_women)
```
The test indicates that the mean complexity of men (`r complex_sex$estimate[1] %>% round(2)`) is different from the corresponding complexity of women (`r complex_sex$estimate[2] %>% round(2)`). By adding the argument `alternative = "less"` to `t.test` we even could finally prove that (the life courses of) men are less complex than (those of) women... Hence, the within and between sequence variation of women is larger than the corresponding values of men.
**Table 6.1** in the book presents several group differences at once. Instead of re-iterating the code for all group comparisons and two different data sets, we wrote a function named `DiscrCompTest` (defined in `6-1_Table_6-1_Step1_Source.R` in [`r fontawesome::fa("far fa-file-zipper")` Chapter06.zip](source/Chapter06.zip)) that can be reused to compute the same group comparisons (group indicators: `east`, `sex`, `highschool`) using two different sets of input arguments (set 1: `family`, `partner.child.year.seq`, `partner.child.year.om`; set 2: `activity`, `activity.year.seq`, `activity.year.om`). The function's output is a nicely formatted table using `{knitr}`'s `kable` and the `{kableExtra}` package. The function is sourced and called in `6-1_Table_6-1_Step2_Print.R`.
On this page we do not further elaborate on the code defining the function because this website's focus is on introducing the functions required to re-produce the results shown in the book. Re-using and adjusting the two simple code chunks shown above would be sufficient to achieve this goal. Admittedly, such a verbose coding strategy wouldn't be very efficient and is also quite error prone, but done correctly would produce all the bits and pieces required to "manually" re-build **Table 6.1**.
```{r echo=TRUE, eval=FALSE}
# Discrepancy & Complexity I - Labor Market
dissassoc(activity.year.om,activity$sex)
complex_men <- seqici(activity.year.seq[activity$sex == 0,])
complex_women <- seqici(activity.year.seq[activity$sex == 1,])
t.test(complex_men, complex_women)
dissassoc(activity.year.om,activity$east)
complex_west <- seqici(activity.year.seq[activity$east == 0,])
complex_east <- seqici(activity.year.seq[activity$east == 1,])
t.test(complex_west, complex_east)
dissassoc(activity.year.om,activity$highschool)
complex_no_highschool <- seqici(activity.year.seq[activity$highschool == 0,])
complex_highschool <- seqici(activity.year.seq[activity$highschool == 1,])
t.test(complex_no_highschool, complex_highschool)
# Discrepancy & Complexity II - Family
dissassoc(partner.child.year.om,family$sex)
complex_men <- seqici(partner.child.year.seq[family$sex == 0,])
complex_women <- seqici(partner.child.year.seq[family$sex == 1,])
t.test(complex_men, complex_women)
dissassoc(partner.child.year.om,family$east)
complex_west <- seqici(partner.child.year.seq[family$east == 0,])
complex_east <- seqici(partner.child.year.seq[family$east == 1,])
t.test(complex_west, complex_east)
dissassoc(partner.child.year.om,family$highschool)
complex_no_highschool <- seqici(partner.child.year.seq[family$highschool == 0,])
complex_highschool <- seqici(partner.child.year.seq[family$highschool == 1,])
t.test(complex_no_highschool, complex_highschool)
```