/
01-cleanKLD.R
753 lines (584 loc) · 21.9 KB
/
01-cleanKLD.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#
#
# name: 01-cleanKLD.R
# by: rjc
# date: 2021-04-22
# what: this cleans and reshapes the KLD data
#
#
# ____________________________________________________________________________
# introductories ####
# clean up
rm(list = ls())
# packages
library(tidyverse)
library(foreach)
# ____________________________________________________________________________
# load in and clean data ####
# read in data
kld <-
read_csv(file = "KLD_stats_all_through_2018.csv",
na = c("", "R", "NA"),
# the following variables are wonky and not of use:
col_types = cols('CUSIP' = col_skip(),
'legacy_companyID' = col_skip(),
'domicile' = col_skip(),
'issuerid' = col_skip())) %>%
# remaining parsing problems are all when HUM_str_num = 2, so:
mutate(HUM_str_num = if_else(condition = HUM_str_num >= 1,
true = 1,
false = 0)) %>%
# get rid of cumulative totals:
select(-contains("num")) %>%
# put things in order:
select(CompanyName, Ticker, year, everything()) %>%
# rename to save some annoyances:
rename('name' = CompanyName,
'ticker' = Ticker)
# fix the logicals
fixLogical <- function (x)
{
y <- rep(NA, length(x))
y[x == FALSE] <- 0
y[x == TRUE] <- 1
as.integer(y)
}
cleanTwo <- function (x)
{
y <- x
y[x >= 2] <- 1
as.integer(y)
}
kld <-
kld %>%
mutate(across(.cols = where(is_logical),
.fns = fixLogical)) %>%
mutate(across(.cols = where(is_double),
.fns = as.integer))
kld <-
kld %>%
mutate(across(.cols = where(is_double) & !c(year),
.fns = cleanTwo))
# rather than parse through all the different domiciles, let's just
# get the tickers of firms from 1991-2012 data (ensures this is similar in
# kind with original D-SOCIAL-KLD)
universe <-
kld %>%
filter(year <= 2012) %>%
distinct(ticker) %>%
pull(ticker)
kld <-
filter(kld, ticker %in% universe)
rm(universe)
## ............................................................................
## some problematic tickers/names ####
# having USX as "X" throws off some statistics later on, so:
kld <-
kld %>%
mutate(ticker = ifelse(ticker == "X", "USX", ticker))
# LLL and MKL both have bad duplicates
kld$ticker[kld$name == "lululemon athletica inc."] <- "LLLM"
kld$ticker[kld$name == "Alterra Capital Holdings Ltd"] <- "MKLA"
# mistake with Heritage Financial Corporation versus group
kld$ticker[kld$name == "Heritage Financial Corporation"] <- "HFWA"
# several missing tickers:
# Ryder System is R, which created a problem from the read-in:
kld$ticker[kld$name %in% c("Ryder System, Inc.",
"RYDER SYSTEM, INC.")] <- "R"
# one missing ticker in AuthenTec, Inc.
kld$ticker[kld$name == "AuthenTec, Inc."] <- "AUTH"
# one missing ticker in Benihana Inc
kld$ticker[kld$name == "Benihana Inc"] <- "BNHNA"
# one missing ticker in MEDTOX Scientific, Inc.
kld$ticker[kld$name == "MEDTOX Scientific, Inc."] <- "MTOX"
# national bank of canada has ticker NA, which created a problem
kld$ticker[kld$name %in% c("National Bank of Canada",
"NATIONAL BANK OF CANADA",
"BANQUE NATIONALE DU CANADA")] <- "NA"
# no tickers for Romarco Minerals Inc.
kld$ticker[kld$name == "Romarco Minerals Inc."] <- "RTRAF"
# no tickers for AKER SOLUTIONS ASA
kld$ticker[kld$name == "AKER SOLUTIONS ASA"] <- "AKSO"
# capital property fund is a problem: CPL is doubled, and some NAs.
kld$ticker[kld$name %in% c("CAPITAL PROPERTY FUND",
"Capital Property Fund Ltd")] <- "CPL:SJ"
# no tickers for Intra-Cellular Therapies Inc
kld$ticker[kld$name == "Intra-Cellular Therapies Inc"] <- "ITCI"
# no tickers for MGM GROWTH PROPERTIES LLC
kld$ticker[kld$name == "MGM GROWTH PROPERTIES LLC"] <- "MGP"
# problems with Allergan
kld$ticker[kld$name %in% c("Allergan, Inc.", "Allergan plc")] <- "AGN"
kld$ticker[kld$name == "AEGON N.V."] <- "AEG~~uixl"
# problems with choice point
kld$ticker[kld$name %in% c("Cyfrowy Polsat S.A.",
"CYFROWY POLSAT SPOLKA AKCYJNA")] <- "CPS~~xuqn"
kld$ticker[kld$name == "COOPER-STANDARD HOLDINGS INC."] <- "CPS~~iuvh"
# problems with Ladenburg Thalmann
kld$ticker[kld$name == "GRUPA LOTOS SPOLKA AKCYJNA"] <- "LTS~~ssil"
# ____________________________________________________________________________
# learn which metrics and firms are in which year ####
## ............................................................................
## which metrics are in which year? ####
has_data <- function (x) any(!is.na(x))
all_distinct <- function (x, negate = FALSE)
{
foo <- identical(sort(x), sort(unique(x)))
if (negate == FALSE) foo
if (negate == TRUE) !foo
}
mets_by_year <-
foreach (i = min(kld$year):max(kld$year)) %do%
{
filter(kld, year == i) %>%
select(-name, -ticker, -year) %>%
select_if(has_data) %>%
names(.)
}
rm(i)
# check to see if all metrics are distinct within year
lapply(mets_by_year, all_distinct) %>%
unlist() %>%
all()
# they are indeed
## ............................................................................
## which tickers are in which year? ####
firms_in_year <-
foreach (i = min(kld$year):max(kld$year)) %do%
{
filter(kld, year == i) %>%
pull(ticker) %>%
sort(.)
}
rm(i)
# check to see if all tickers are distinct within year
lapply(firms_in_year, all_distinct, negate = TRUE) %>%
unlist() %>%
which(.)
# they are not :-(.
# 10 is a bad year
filter(kld, year == 1990 + 10) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num))
# for some reason, we have four copies of the same row for RIG. we can just
# unique our way out of that one.
firms_in_year[[10]] <- sort(unique(firms_in_year[[10]]))
# 15 is a bad year
filter(kld, year == 1990 + 15) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num))
# same with BWNG in year 15 we can unique our way out of it.
firms_in_year[[15]] <- sort(unique(firms_in_year[[15]]))
# 16 is a bad year
filter(kld, year == 1990 + 16) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num))
# same with H in year 16.
firms_in_year[[16]] <- sort(unique(firms_in_year[[16]]))
# 17 is a bad year
filter(kld, year == 1990 + 17) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num))
# same with IVZ_LN in year 17.
firms_in_year[[17]] <- sort(unique(firms_in_year[[17]]))
# 20 is a bad year
filter(kld, year == 1990 + 20) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num))
# same with BNHNA, CENT, HEI, and UBA in year 20 (checked all---duplicates)
firms_in_year[[20]] <- sort(unique(firms_in_year[[20]]))
# 22 is a bad year
filter(kld, year == 1990 + 22) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num))
# same with KCAP and UAM in year 22
firms_in_year[[22]] <- sort(unique(firms_in_year[[22]]))
# 23 is a bad year
filter(kld, year == 1990 + 23) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num)) -> bad23
# MANY bad tickers. 190.
filter(kld, year == 1990 + 23 & ticker %in% bad23$ticker) %>%
group_by(ticker) %>%
summarise(numNames = length(unique(name)))
# and each of these has multiple company names. it's fine to just index
# these now (assuming we use a rule that will work for future years). that
# said, we also need to ensure that previous tickers are linked to this one.
for (i in 1:nrow(bad23))
{
# see if the ticker has been represented earlier.
oldI <- filter(kld, year < 1990 + 23 & ticker == bad23$ticker[i]) %>%
select(name, ticker, year)
# figure out which of the duplicated tickers are new
if (nrow(oldI) > 0)
{
newNamesI <-
filter(kld, year == 1990 + 23 & ticker == bad23$ticker[i]) %>%
distinct(name) %>%
pull(name)
notInOld <- setdiff(newNamesI, oldI$name)
} else {
notInOld <-
filter(kld, year == 1990 + 23 & ticker == bad23$ticker[i]) %>%
distinct(name) %>%
pull(name)
}
# create new ticker names for the duplicated new firms
set.seed(61802 + 23 + i)
newTicks <-
foreach (j = 1:length(notInOld), .combine = c) %do%
{
paste(sample(letters, size = 4, replace = TRUE), collapse = "")
}
rm(j)
newTicks <- paste(bad23$ticker[i], newTicks, sep = "~~")
# take note of the new tickers for future use
newTicksLogI <-
tibble(
name = notInOld,
oldTick = bad23$ticker[i],
newTick = newTicks,
yearCreated = 1990 + 23,
yearLastUsed = 1990 + 23
)
if (i == 1) newTicksLog <- NULL
newTicksLog <- bind_rows(newTicksLog, newTicksLogI)
# execute the replacement
for (j in 1:nrow(newTicksLogI))
{
kld$ticker[kld$year == (1990 + 23) & kld$name == newTicksLogI$name[j]] <-
newTicksLogI$newTick[j]
}
rm(j)
}
rm(bad23, newTicksLogI, oldI, i, newNamesI, newTicks, notInOld)
firms_in_year[[23]] <-
filter(kld, year == 1990 + 23) %>%
pull(ticker) %>%
sort(.)
# fixed
# 24 is a bad year
filter(kld, year == 1990 + 24) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num)) -> bad24
# same idea
# roll new tickers through
for (i in 1:nrow(newTicksLog))
{
check <- filter(kld, year == 1990 + 24 & name == newTicksLog$name[i])
if (nrow(check) > 0)
{
kld$ticker[kld$year == 1990 + 24 & kld$name == newTicksLog$name[i]] <-
newTicksLog$newTick[i]
newTicksLog$yearLastUsed <- 1990 + 24
}
rm(check)
}
rm(i)
# check again
filter(kld, year == 1990 + 24) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num)) -> bad24
# better, but not good yet
for (i in 1:nrow(bad24))
{
# see if the ticker has been represented earlier.
oldI <- filter(kld, year < 1990 + 24 & ticker == bad24$ticker[i]) %>%
select(name, ticker, year)
# figure out which of the duplicated tickers are new
if (nrow(oldI) > 0)
{
newNamesI <-
filter(kld, year == 1990 + 24 & ticker == bad24$ticker[i]) %>%
distinct(name) %>%
pull(name)
notInOld <- setdiff(newNamesI, oldI$name)
} else {
notInOld <-
filter(kld, year == 1990 + 24 & ticker == bad24$ticker[i]) %>%
distinct(name) %>%
pull(name)
}
# create new ticker names for the duplicated new firms
set.seed(61802 + 24 + i)
newTicks <-
foreach (j = 1:length(notInOld), .combine = c) %do%
{
paste(sample(letters, size = 4, replace = TRUE), collapse = "")
}
rm(j)
newTicks <- paste(bad24$ticker[i], newTicks, sep = "~~")
# take note of the new tickers for future use
newTicksLogI <-
tibble(
name = notInOld,
oldTick = bad24$ticker[i],
newTick = newTicks,
yearCreated = 1990 + 24,
yearLastUsed = 1990 + 24
)
newTicksLog <- bind_rows(newTicksLog, newTicksLogI)
# execute the replacement
for (j in 1:nrow(newTicksLogI))
{
kld$ticker[kld$year == (1990 + 24) & kld$name == newTicksLogI$name[j]] <-
newTicksLogI$newTick[j]
}
rm(j)
}
rm(bad24, newTicksLogI, oldI, i, newNamesI, newTicks, notInOld)
# all that's left is a double-counted one like in the earlier bad years.
firms_in_year[[24]] <-
filter(kld, year == 1990 + 24) %>%
pull(ticker) %>%
sort(.)
firms_in_year[[24]] <- sort(unique(firms_in_year[[24]]))
# 25 is a bad year
filter(kld, year == 1990 + 25) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num))
# for some reason Homeaway is counted as Expedia.
kld$ticker[kld$name == "HOMEAWAY, INC."] <- "AWAY"
firms_in_year[[25]] <- sort(unique(firms_in_year[[25]]))
# 27 is a bad year
filter(kld, year == 1990 + 27) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num))
# another extensive one.
# roll new tickers throguh
for (i in 1:nrow(newTicksLog))
{
check <- filter(kld, year == 1990 + 27 & name == newTicksLog$name[i])
if (nrow(check) > 0)
{
kld$ticker[kld$year == 1990 + 27 & kld$name == newTicksLog$name[i]] <-
newTicksLog$newTick[i]
newTicksLog$yearLastUsed <- 1990 + 27
}
rm(check)
}
rm(i)
# check again
filter(kld, year == 1990 + 27) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num)) -> bad27
# fix what's left
for (i in 1:nrow(bad27))
{
# see if the ticker has been represented earlier.
oldI <- filter(kld, year < 1990 + 27 & ticker == bad27$ticker[i]) %>%
select(name, ticker, year)
# figure out which of the duplicated tickers are new
if (nrow(oldI) > 0)
{
newNamesI <-
filter(kld, year == 1990 + 27 & ticker == bad27$ticker[i]) %>%
distinct(name) %>%
pull(name)
notInOld <- setdiff(newNamesI, oldI$name)
} else {
notInOld <-
filter(kld, year == 1990 + 27 & ticker == bad27$ticker[i]) %>%
distinct(name) %>%
pull(name)
}
# create new ticker names for the duplicated new firms
set.seed(61802 + 27 + i)
newTicks <-
foreach (j = 1:length(notInOld), .combine = c) %do%
{
paste(sample(letters, size = 4, replace = TRUE), collapse = "")
}
rm(j)
newTicks <- paste(bad27$ticker[i], newTicks, sep = "~~")
# take note of the new tickers for future use
newTicksLogI <-
tibble(
name = notInOld,
oldTick = bad27$ticker[i],
newTick = newTicks,
yearCreated = 1990 + 27,
yearLastUsed = 1990 + 27
)
newTicksLog <- bind_rows(newTicksLog, newTicksLogI)
# execute the replacement
for (j in 1:nrow(newTicksLogI))
{
kld$ticker[kld$year == (1990 + 27) & kld$name == newTicksLogI$name[j]] <-
newTicksLogI$newTick[j]
}
rm(j)
}
rm(bad27, newTicksLogI, oldI, i, newNamesI, newTicks, notInOld)
# fixed
firms_in_year[[27]] <-
filter(kld, year == 1990 + 27) %>%
pull(ticker) %>%
sort(.)
# 28 is a bad year
filter(kld, year == 1990 + 28) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num))
# another extensive one.
# roll new tickers throguh
for (i in 1:nrow(newTicksLog))
{
check <- filter(kld, year == 1990 + 28 & name == newTicksLog$name[i])
if (nrow(check) > 0)
{
kld$ticker[kld$year == 1990 + 28 & kld$name == newTicksLog$name[i]] <-
newTicksLog$newTick[i]
newTicksLog$yearLastUsed <- 1990 + 28
}
rm(check)
}
rm(i)
filter(kld, year == 1990 + 28) %>%
group_by(ticker) %>%
summarise(num = n()) %>%
filter(num > 1) %>%
arrange(desc(num)) -> bad28
# fix what's left
for (i in 1:nrow(bad28))
{
# see if the ticker has been represented earlier.
oldI <- filter(kld, year < 1990 + 28 & ticker == bad28$ticker[i]) %>%
select(name, ticker, year)
# figure out which of the duplicated tickers are new
if (nrow(oldI) > 0)
{
newNamesI <-
filter(kld, year == 1990 + 28 & ticker == bad28$ticker[i]) %>%
distinct(name) %>%
pull(name)
notInOld <- setdiff(newNamesI, oldI$name)
} else {
notInOld <-
filter(kld, year == 1990 + 28 & ticker == bad28$ticker[i]) %>%
distinct(name) %>%
pull(name)
}
# create new ticker names for the duplicated new firms
set.seed(61802 + 28 + i)
newTicks <-
foreach (j = 1:length(notInOld), .combine = c) %do%
{
paste(sample(letters, size = 4, replace = TRUE), collapse = "")
}
rm(j)
newTicks <- paste(bad28$ticker[i], newTicks, sep = "~~")
# take note of the new tickers for future use
newTicksLogI <-
tibble(
name = notInOld,
oldTick = bad28$ticker[i],
newTick = newTicks,
yearCreated = 1990 + 28,
yearLastUsed = 1990 + 28
)
newTicksLog <- bind_rows(newTicksLog, newTicksLogI)
# execute the replacement
for (j in 1:nrow(newTicksLogI))
{
kld$ticker[kld$year == (1990 + 28) & kld$name == newTicksLogI$name[j]] <-
newTicksLogI$newTick[j]
}
rm(j)
}
rm(bad28, newTicksLogI, oldI, i, newNamesI, newTicks, notInOld)
firms_in_year[[28]] <-
filter(kld, year == 1990 + 28) %>%
pull(ticker) %>%
sort(.)
# check to see if all tickers are distinct within year
lapply(firms_in_year, all_distinct) %>%
unlist() %>%
all(.) # we're good
# get rid of duplicated rows
kld <-
kld %>%
unite(id, ticker, year, remove = FALSE) %>%
distinct(id, .keep_all = TRUE) %>%
select(-id)
# useful to have this data for merging
write_csv(x = kld,
file = "firm-year-level.csv")
# ____________________________________________________________________________
# execute the reshape ####
allTicks <- sort(unique(kld$ticker))
for (i in 1:length(unique(kld$year)))
{
# get the data we have.
dati_in <-
kld %>%
filter(year == 1990 + i & ticker %in% firms_in_year[[i]]) %>%
select(ticker, mets_by_year[[i]]) %>%
rename_with(.fn = ~ paste(., 1990 + i, sep = "_"),
.cols = -ticker) %>%
arrange(ticker)
dati_out <-
tibble(
ticker = unique(sort(kld$ticker[!(kld$ticker %in%
firms_in_year[[i]])]))
)
dati <- bind_rows(dati_in, dati_out) %>%
arrange(ticker) %>%
select(-ticker)
yrsi <- rep(i, ncol(dati))
rm(dati_in, dati_out)
if (i == 1) dat <- dati
if (i > 1) dat <- bind_cols(dat, dati)
if (i == 1) yrs <- yrsi
if (i > 1) yrs <- c(yrs, yrsi)
rm(yrsi, dati)
}
# ____________________________________________________________________________
# triple check it's 0/1/NA ####
trigger <- function (x)
{
any(!(is.na(x) | x %in% c(0,1)))
}
dat <-
dat %>%
mutate(across(.cols = everything(),
.fns = as.integer))
bad <- summarise(.data = dat,
across(.cols = everything(),
.fns = trigger)) %>%
pivot_longer(cols = everything(),
names_to = "metric",
values_to = "trigger") %>%
filter(trigger) %>%
pull(metric)
dat$PRO_con_A_1995[dat$PRO_con_A_1995 > 1] <- 1
dat$NUC_con_A_1999[dat$NUC_con_A_1999 > 1] <- 1
# ____________________________________________________________________________
# save for later ####
rm(firms_in_year, kld, mets_by_year, newTicksLog, i, bad)
save.image(file = "data-reshaped.RData")
# end ## (not run)