-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities_report.R
401 lines (310 loc) · 15 KB
/
utilities_report.R
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Style output tables #########################################################
# These four functions are used to change the style of the output tables.
# Depending on the form of the table different styels are chiosen.
# Style 0
style_table0 <- function(tab) {
# Capitalize first letter of column and make header boldface
names(tab) <- pandoc.strong.return(names(tab))
return(tab)
}
# Style 1
style_table1 <- function(tab) {
# Capitalize first letter of column, make header, last column and second
# last row in boldface and make last row italic
names(tab) <- pandoc.strong.return(names(tab))
emphasize.strong.cols(ncol(tab))
emphasize.strong.rows(nrow(tab))
return(tab)
}
# Style 2
style_table2 <- function(tab) {
# Capitalize first letter of column, make header, last column and
# last row in boldface
names(tab) <- pandoc.strong.return(names(tab))
emphasize.strong.cols(ncol(tab))
emphasize.strong.rows(nrow(tab))
return(tab)
}
# Style 3
style_table3 <- function(tab) {
# Capitalize first letter of column, make header and first column boldface
names(tab) <- pandoc.strong.return(names(tab))
emphasize.strong.cols(1)
return(tab)
}
# Here the maturity bias are plotted for all readers combined.
plot_general_freq_matur <- function(ad_long, strata=NULL){
# There are conflicts between plyr and ddply, and, it seems that it is necesary unload plyr and load it again for the function ddply and other working properly
unloadNamespace("plyr")
library("plyr")
freq_tab <-
ad_long %>%
ddply(.(modal_maturity, Maturity), summarize, count=length(Maturity)) ##
total_tab <-
ad_long %>%
ddply(.(modal_maturity), summarize, count=length(Maturity)) ##
freq_tab=merge(freq_tab, total_tab, by.x="modal_maturity", by.y="modal_maturity")
freq_tab$count=round(freq_tab$count.x/freq_tab$count.y, digits=3)
freq_tab=freq_tab[,-c(3,4)]
diffmodal=setdiff(sort(unique(ad_long$Maturity)), sort(unique(freq_tab$modal_maturity)))
mat=sort(unique(ad_long$Maturity))
completedat=as.data.frame(expand.grid(diffmodal,mat))
completedat$count=rep(0,dim(completedat)[1])
colnames(completedat)=c("Maturity", "modal_maturity", "count")
freq_tab=rbind(freq_tab, completedat)
freq_tab=as.data.frame(ddply(freq_tab,.(modal_maturity, Maturity), summarize, count=if(sum(count)==0){NA} else {sum(count, na.rm=TRUE)}))
ggplot(freq_tab, aes(x = modal_maturity, y = Maturity, size = count, label = count)) +
geom_point(shape = 21, fill = "white") +
scale_size(range = c(2, 15), guide = "legend") +
geom_text(aes(label=as.character(count),hjust=0.5,vjust=-1), size=3) +
geom_abline(mapping = NULL, data = freq_tab, slope=1, 0, na.rm = FALSE, show.legend = NA) +
ggtitle("Relative frequency plot") +
xlab("Modal maturity") + ylab("Maturity stage") +
theme(
plot.title = element_text(color="black", size=20, face="bold", hjust=0.5),
axis.title.x = element_text(color="black", size=15, face="bold"),
axis.title.y = element_text(color="black", size=15, face="bold"),
axis.text.x = element_text(size = 15),
axis.text.y = element_text(size = 15)
) +
theme(legend.position = "none") +
theme(panel.background = element_rect(fill = "#BFD5E3", colour = "#6D9EC1",size = 2, linetype = "solid"))
}
# Here the sex categorization bias are plotted for all readers combined.
plot_general_freq_sex <- function(ad_long, strata=NULL){
# There are conflicts between plyr and ddply, and, it seems that it is necesary unload plyr and load it again for the function ddply and other working properly
unloadNamespace("plyr")
library("plyr")
freq_tab <-
ad_long %>%
ddply(.(modal_sex, Sex), summarize, count=length(Sex)) ##
total_tab <-
ad_long %>%
ddply(.(modal_sex), summarize, count=length(Sex)) ##
freq_tab=merge(freq_tab, total_tab, by.x="modal_sex", by.y="modal_sex")
freq_tab$count=round(freq_tab$count.x/freq_tab$count.y, digits=3)
freq_tab=freq_tab[,-c(3,4)]
diffmodal=setdiff(sort(unique(ad_long$Sex)), sort(unique(freq_tab$modal_sex)))
sex=sort(unique(ad_long$Sex))
completedat=as.data.frame(expand.grid(diffmodal,sex))
completedat$count=rep(0,dim(completedat)[1])
colnames(completedat)=c("modal_sex", "Sex", "count")
freq_tab=rbind(freq_tab, completedat)
freq_tab=as.data.frame(ddply(freq_tab,.(modal_sex, Sex), summarize, count=if(sum(count)==0){NA} else {sum(count, na.rm=TRUE)}))
ggplot(freq_tab, aes(x = modal_sex, y = Sex, size = count, label = count)) +
geom_point(shape = 21, fill = "white") +
scale_size(range = c(2, 25), guide = "legend") +
geom_text(aes(label=as.character(count),hjust=0.5,vjust=-1), size=3) +
geom_abline(mapping = NULL, data = freq_tab, slope=1, 0, na.rm = FALSE, show.legend = NA) +
ggtitle("Relative frequency plot") +
xlab("Modal sex") + ylab("Sex category") +
theme(
plot.title = element_text(color="black", size=20, face="bold", hjust=0.5),
axis.title.x = element_text(color="black", size=15, face="bold"),
axis.title.y = element_text(color="black", size=15, face="bold"),
axis.text.x = element_text(size = 15),
axis.text.y = element_text(size = 15)
) +
theme(legend.position = "none") +
theme(panel.background = element_rect(fill = "#BFD5E3", colour = "#6D9EC1",size = 2, linetype = "solid"))
}
# Sex and maturity bias plots #############################################################
# The maturity bias plots are made per reader.
# For each reader is the mean age and 2 times standard deviation
# (as error bars) plotted against the modal age.
plot_bias_matur <- function(ad_long) {
# There are conflicts between plyr and ddply, and, it seems that it is necesary unload plyr and load it again for the function ddply and other working properly
unloadNamespace("plyr")
library("plyr")
ad_long$reader=as.character(ad_long$reader)
# return list of plots
lapply(sort(unique(ad_long$reader)), function(ireader) {
# Plot data and make settings/design
freq_tab <-
ad_long %>%
filter(reader == ireader) %>%
ddply(.(modal_maturity, Maturity, reader), summarize, count=length(Maturity)) ##
diffmodal=setdiff(sort(unique(ad_long$Maturity)), sort(unique(freq_tab$modal_maturity)))
mat=sort(unique(ad_long$Maturity))
completedat=as.data.frame(expand.grid(diffmodal, mat))
completedat$reader=rep(unique(freq_tab$reader),dim(completedat)[1])
completedat$count=rep(NA,dim(completedat)[1])
colnames(completedat)=c( "modal_maturity", "Maturity", "reader", "count")
freq_tab=rbind(freq_tab, completedat)
freq_tab=as.data.frame(ddply(freq_tab,.(modal_maturity, Maturity, reader), summarize, count=sum(count)))
total_tab <-
ad_long %>%
filter(reader == ireader) %>%
ddply(.(modal_maturity), summarize, count=length(Maturity))
freq_tab=merge(freq_tab, total_tab, by.x="modal_maturity", by.y="modal_maturity", all.x=TRUE)
freq_tab$count=round(freq_tab$count.x/freq_tab$count.y, digits=3)
freq_tab=freq_tab[,-c(4,5)]
ggplot(freq_tab, aes(x = modal_maturity, y = Maturity, size = count, label = count, group = reader)) +
geom_point(shape = 21, fill = "white") +
geom_text(aes(label=as.character(count),hjust=0.5,vjust=-1), size=3) +
scale_size(range = c(2, 15), guide = "legend") +
geom_abline(mapping = NULL, data = freq_tab, slope=1, 0, na.rm = FALSE, show.legend = NA) +
facet_wrap(~ reader, ncol = 2) +
theme(strip.text.x=element_text(size=25, hjust=0.5, vjust=0.5)) +
xlab("Modal maturity") + ylab("Maturity stage") +
theme(
plot.title = element_text(color="black", size=20, face="bold", hjust=0.5),
axis.title.x = element_text(color="black", size=15, face="bold"),
axis.title.y = element_text(color="black", size=15, face="bold"),
axis.text.x = element_text(size = 15),
axis.text.y = element_text(size = 15)
) +
theme(legend.position = "none") +
theme(panel.background = element_rect(fill = "#BFD5E3", colour = "#6D9EC1",size = 2, linetype = "solid"))
})
}
plot_bias_sex <- function(ad_long) {
# There are conflicts between plyr and ddply, and, it seems that it is necesary unload plyr and load it again for the function ddply and other working properly
unloadNamespace("plyr")
library("plyr")
ad_long$reader=as.character(ad_long$reader)
# return list of plots
lapply(sort(unique(ad_long$reader)), function(ireader) {
# Plot data and make settings/design
freq_tab <-
ad_long %>%
filter(reader == ireader) %>%
ddply(.(modal_sex, Sex, reader), summarize, count=length(Sex))
diffmodal=setdiff(sort(unique(ad_long$Sex)), sort(unique(freq_tab$modal_sex)))
mat=sort(unique(ad_long$Sex))
completedat=as.data.frame(expand.grid(diffmodal, mat))
completedat$reader=rep(unique(freq_tab$reader),dim(completedat)[1])
completedat$count=rep(NA,dim(completedat)[1])
colnames(completedat)=c( "modal_sex", "Sex", "reader", "count")
freq_tab=rbind(freq_tab, completedat)
freq_tab=as.data.frame(ddply(freq_tab,.(modal_sex, Sex, reader), summarize, count=sum(count)))
total_tab <-
ad_long %>%
filter(reader == ireader) %>%
ddply(.(modal_sex), summarize, count=length(Sex))
freq_tab=merge(freq_tab, total_tab, by.x="modal_sex", by.y="modal_sex", all.x=TRUE)
freq_tab$count=round(freq_tab$count.x/freq_tab$count.y, digits=3)
freq_tab=freq_tab[,-c(4,5)]
ggplot(freq_tab, aes(x = modal_sex, y = Sex, size = count, label = count, group = reader)) +
geom_point(shape = 21, fill = "white") +
geom_text(aes(label=as.character(count),hjust=0.5,vjust=-1), size=3) +
scale_size(range = c(2, 15), guide = "legend") +
geom_abline(mapping = NULL, data = freq_tab, slope=1, 0, na.rm = FALSE, show.legend = NA) +
facet_wrap(~ reader, ncol = 2) +
theme(strip.text.x=element_text(size=25, hjust=0.5, vjust=0.5)) +
xlab("Modal sex") + ylab("Sex stage") +
theme(
plot.title = element_text(color="black", size=20, face="bold", hjust=0.5),
axis.title.x = element_text(color="black", size=15, face="bold"),
axis.title.y = element_text(color="black", size=15, face="bold"),
axis.text.x = element_text(size = 15),
axis.text.y = element_text(size = 15)
) +
theme(legend.position = "none") +
theme(panel.background = element_rect(fill = "#BFD5E3", colour = "#6D9EC1",size = 2, linetype = "solid"))
})
}
# Plot std, ca and pa #########################################################
# Plot overall std, CA and PA per modal maturity stage in same plot
plot_stat_matur <- function(ad_long) {
# Again, beacuse of conflicts between plyr and dplyr it is necesary to unload the library plyr
unloadNamespace("plyr")
# Combine the three data sets
dat_in <-
ad_long %>%
group_by(modal_maturity) %>%
summarise(
cu = cu_II(Maturity),
pa = mean(Maturity == modal_maturity, na.rm = TRUE) * 100)
# Limit to use for axis
cu_lim <- ceiling(max(dat_in$cu, na.rm = T))
p <-
dat_in %>%
filter(!is.na(cu)) %>%
ggplot() +
theme_bw() +
# Coefficient of unalikeability
geom_line(aes(x = modal_maturity, y = cu, colour = "cu")) +
geom_point(aes(x = modal_maturity, y = cu, colour = "cu",
shape = "cu"), size = 3) +
# PA
geom_line(aes(x = modal_maturity, y = pa*cu_lim/100, colour = "pa")) +
geom_point(aes(x = modal_maturity, y = pa*cu_lim/100, colour = "pa",
shape = "pa"), size = 3) +
# Make left side y-axis
scale_y_continuous(name = expression("Coefficient of unalikability CU (years)"),
limits = c(0, cu_lim)) +
# Make right side y-axis
scale_y_continuous(name = expression("Coefficient of unalikability (years)"),
sec.axis = sec_axis(~ . * 100/cu_lim,
name = "Percentage of Agreement PA (%)"),
limits = c(0, cu_lim))
print(p)
# # Colors and labels
# p +
# theme(axis.text.y = element_text(color = "#80B1D3"),
# axis.text.y.right = element_text(color = "#FB8072")
# #text = element_text(family="Calibri")
# ) +
# scale_colour_manual(name = "Measure",
# values = c("#80B1D3", "#FB8072"),
# labels = c("CU", "PA")) +
# scale_shape_manual(name = "Measure", values = c(16, 8, 17),
# labels = c("CU", "PA")) +
# labs(x = "Modal maturity", colour = "")
#
# print(p)
# Now, I load again plyr and dplyr in this order:
library(plyr); library(dplyr)
}
plot_stat_sex <- function(ad_long) {
# Again, beacuse of conflicts between plyr and dplyr it is necesary to unload the library plyr
unloadNamespace("plyr")
# Combine the three data sets
dat_in <-
ad_long %>%
group_by(modal_sex) %>%
summarise(
cu = cu_II(Sex),
pa = mean(Sex == modal_sex, na.rm = TRUE) * 100)
# Limit to use for axis
cu_lim <- ceiling(max(dat_in$cu, na.rm = T))
p <-
dat_in %>%
filter(!is.na(cu)) %>%
ggplot() +
theme_bw() +
# Coefficient of unalikeability
geom_line(aes(x = modal_sex, y = cu, colour = "cu")) +
geom_point(aes(x = modal_sex, y = cu, colour = "cu",
shape = "cu"), size = 3) +
# PA
geom_line(aes(x = modal_sex, y = pa*cu_lim/100, colour = "pa")) +
geom_point(aes(x = modal_sex, y = pa*cu_lim/100, colour = "pa",
shape = "pa"), size = 3) +
# Make left side y-axis
scale_y_continuous(name = expression("Coefficient of unalikability CU (years)"),
limits = c(0, cu_lim)) +
# Make right side y-axis
scale_y_continuous(name = expression("Coefficient of unalikability (years)"),
sec.axis = sec_axis(~ . * 100/cu_lim,
name = "Percentage of Agreement PA (%)"),
limits = c(0, cu_lim))
print(p)
# # Colors and labels
# p +
# theme(axis.text.y = element_text(color = "#80B1D3"),
# axis.text.y.right = element_text(color = "#FB8072")
# #text = element_text(family="Calibri")
# ) +
# scale_colour_manual(name = "Measure",
# values = c("#80B1D3", "#FB8072"),
# labels = c("CU", "PA")) +
# scale_shape_manual(name = "Measure", values = c(16, 8, 17),
# labels = c("CU", "PA")) +
# labs(x = "Modal sex", colour = "")
#
# print(p)
# Now, I load again plyr and dplyr in this order:
library(plyr); library(dplyr)
}