-
Notifications
You must be signed in to change notification settings - Fork 0
/
rChapter3-5.Rmd
299 lines (233 loc) · 8.11 KB
/
rChapter3-5.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
---
title: "Correlation for dissimilarity matrices"
description: |
Chapter 3.5 Comparing dissimilarity matrices
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 3
load(here("data", "3-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 3
([`r fontawesome::fa("far fa-file-zipper")` Chapter03.zip](source/Chapter03.zip)).
- Read `readme.html` and run `3-0_ChapterSetup.R`. This will create `3-0_ChapterSetup.RData` in the sub folder `data/R`. This file contains the data required to produce the plots shown below.
- You also have to add the function `legend_large_box` to your environment in order to render the tweaked version of the legend described below. You find this file in the `source` folder of the unzipped Chapter 3 archive.
- We also recommend to load the libraries listed in Chapter 3'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", "load_libraries.R"))
# load environment generated in "3-0_ChapterSetup.R"
load(here("data", "R", "3-0_ChapterSetup.RData"))
```
</details>
\
In chapter 3.5, we consider how to compute a correlation matrix of sequences' pairwise dissimilarity matrices obtained using different strategies. The data come from a sub-sample of the German Family Panel - pairfam. For further information on the study and on how to access the full scientific use file see [here](https://www.pairfam.de/en/){target="_blank"}.
## Preparatory work: computing the dissimilarity matrices to be compared
Two OM-based with different substitution costs, first equal to 2 and then equal to 1:
```{r, eval=TRUE, echo=TRUE}
costs.sm2 <- matrix(
c(0,2,2,2,2,2,2,2,2,
2,0,2,2,2,2,2,2,2,
2,2,0,2,2,2,2,2,2,
2,2,2,0,2,2,2,2,2,
2,2,2,2,0,2,2,2,2,
2,2,2,2,2,0,2,2,2,
2,2,2,2,2,2,0,2,2,
2,2,2,2,2,2,2,0,2,
2,2,2,2,2,2,2,2,0
),
nrow = 9,
ncol = 9,
byrow = TRUE)
```
```{r, eval=TRUE, echo=TRUE}
costs.sm1 <- matrix(
c(0,1,1,1,1,1,1,1,1,
1,0,1,1,1,1,1,1,1,
1,1,0,1,1,1,1,1,1,
1,1,1,0,1,1,1,1,1,
1,1,1,1,0,1,1,1,1,
1,1,1,1,1,0,1,1,1,
1,1,1,1,1,1,0,1,1,
1,1,1,1,1,1,1,0,1,
1,1,1,1,1,1,1,1,0
),
nrow = 9,
ncol = 9,
byrow = TRUE)
```
OM with substitution costs based on state properties (or features):
```{r, eval=TRUE, echo=TRUE}
partner <- c(0, 0, 1, 1, 1, 1, 1,1,1)
child <- c(0,1,0,1,0,1,0,1,2)
alphabetprop <- data.frame(partner = partner,
child = child)
rownames(alphabetprop) <- alphabet(partner.child.year.seq)
prop <- seqcost(partner.child.year.seq,
method="FEATURES",
state.features = alphabetprop)
```
OM with theory-based substitution costs:
```{r, eval=TRUE, echo=TRUE}
theo <- matrix(
c(0,1,2,2,2,2,2,2,2,
1,0,2,2,2,2,2,2,2,
2,2,0,1,2,2,2,2,2,
2,2,1,0,2,2,2,2,2,
2,2,2,2,0,1,2,2,2,
2,2,2,2,1,0,2,2,2,
2,2,2,2,2,2,0,1,1,
2,2,2,2,2,2,1,0,1,
2,2,2,2,2,2,1,1,0),
nrow = 9,
ncol = 9,
byrow = TRUE,
dimnames = list(shortlab.partner.child,
shortlab.partner.child))
```
## Correlation between the different dissimilarity matrices
We now compute the dissimilarity matrices with different options to be compared:
```{r, eval=TRUE, echo=TRUE}
om.s2.i1<-seqdist(partner.child.year.seq,
method = "OM",
indel = 1,
sm = costs.sm2)
om.s1.i4<-o<-seqdist(partner.child.year.seq,
method = "OM",
indel = 2,
sm = costs.sm1)
om.prop<-seqdist(partner.child.year.seq,
method = "OM",
indel = 1,
sm = prop$sm)
om.theo<-seqdist(partner.child.year.seq,
method = "OM",
indel = 1,
sm = theo)
trate<-seqdist(partner.child.year.seq,
method = "OM",
indel=1,
sm= "TRATE")
lcs<-seqdist(partner.child.year.seq,
method = "LCS")
ham<-seqdist(partner.child.year.seq,
method = "HAM")
dhd<-seqdist(partner.child.year.seq,
method = "DHD")
```
Further, we have to create a `data.frame` that bring together the various dissimilarity matrices:
```{r, eval=TRUE, echo=TRUE}
diss.partner.child <- data.frame(
OMi1s2 = vech(om.s2.i1),
OMi2s1 = vech(om.s1.i4),
prop = vech(om.prop),
theo = vech(om.theo),
trate = vech(trate),
lcs = vech(lcs),
ham = vech(ham),
dhd = vech(dhd)
)
```
We can now calculate the correlation between the various dissimilarity matrices:
```{r, eval=TRUE, echo=TRUE}
corr.partner.child <- cor(diss.partner.child)
```
...and display the resulting correlation matrix
```{r, eval=TRUE, echo=TRUE}
corr.partner.child
```
## Correlation between the different normalized dissimilarity matrices
It is wise to compute the dissimilarity matrices with different options to be compared by setting the normalization method (see `?seqdist` to learn more about this):
```{r, eval=TRUE, echo=TRUE}
om.s2.i1.n<-seqdist(partner.child.year.seq,
method = "OM",
indel = 1,
sm = costs.sm2,
norm="auto")
om.s1.i4.n<-o<-seqdist(partner.child.year.seq,
method = "OM",
indel = 2,
sm = costs.sm1,
norm="auto")
om.prop.n<-seqdist(partner.child.year.seq,
method = "OM",
indel = 1,
sm = prop$sm,
norm="auto")
om.theo.n<-seqdist(partner.child.year.seq,
method = "OM",
indel = 1,
sm = theo,
norm="auto")
trate.n<-seqdist(partner.child.year.seq,
method = "OM",
indel=1,
sm= "TRATE",
norm="auto")
lcs.n<-seqdist(partner.child.year.seq,
method = "LCS",
norm="auto")
ham.n<-seqdist(partner.child.year.seq,
method = "HAM",
norm="auto")
dhd.n<-seqdist(partner.child.year.seq,
method = "DHD",
norm="auto")
```
Also in this case, we create a `data.frame` of the various normalized dissimilarity matrices:
```{r, eval=TRUE, echo=TRUE}
diss.partner.child.n <- data.frame(
OMi1s2 = vech(om.s2.i1.n),
OMi2s1 = vech(om.s1.i4.n),
PROP = vech(om.prop.n),
THEO = vech(om.theo.n),
TRATE = vech(trate.n),
LCS = vech(lcs.n),
HAM = vech(ham.n),
DHD = vech(dhd.n)
)
```
...and then calculate the correlation between the various dissimilarity matrices:
```{r, eval=TRUE, echo=TRUE}
corr.partner.child.n <- cor(diss.partner.child.n)
```
....and display the resulting correlation matrix
```{r, eval=TRUE, echo=TRUE}
corr.partner.child.n
```
Several options for the visualization of the correlation matrix are available, here we suggest a pie from the `?coorplot` package:
```{r fig.width=6, fig.height=4,echo=FALSE, echo=FALSE}
include_graphics("images/Chapter3/3-5_Fig3-2_corrDissMatrices_gray.png")
```
If you want to explore the code to produce this graph, here it is:
```{r, eval=FALSE, echo=TRUE}
corrplot(corr.partner.child.n,
method =("pie"),
type = "upper",
tl.col = "black",
tl.srt = 40,
tl.cex = 2,
cl.cex = 2,
col=brewer.pal(n = 8, name = "Greys"),
is.corr = FALSE)
dev.off()
```