/
nonparametric_tests.R
1141 lines (1063 loc) · 51.9 KB
/
nonparametric_tests.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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Returns the result of given event study nonparametric tests.
#'
#' Performs main nonparametric tests for each date in the event window and
#' returns a data frame of their statistics and significance.
#'
#' \code{nonparametric_tests} performs given tests among \code{\link{sign_test}},
#' \code{\link{generalized_sign_test}}, \code{\link{corrado_sign_test}},
#' \code{\link{rank_test}}, \code{\link{modified_rank_test}},
#' \code{\link{wilcoxon_test}}, and merge result to a single data frame. If
#' \code{all = TRUE} (the default value), the function ignores the value of
#' \code{tests}.
#'
#' @param list_of_returns a list of objects of S3 class \code{returns}, each
#' element of which is treated as a security.
#' @param event_start an object of \code{Date} class giving the first date of
#' the event period.
#' @param event_end an object of \code{Date} class giving the last date of the
#' event period.
#' @param all a logical vector of length one indicating whether all tests should
#' be performed. The default value is \code{TRUE}.
#' @param tests a list of tests' functions among \code{\link{sign_test}},
#' \code{\link{generalized_sign_test}}, \code{\link{corrado_sign_test}},
#' \code{\link{rank_test}}, \code{\link{modified_rank_test}}, and
#' \code{\link{wilcoxon_test}}.
#' @return A data frame of the following columns:
#' \itemize{
#' \item \code{date}: a calendar date
#' \item \code{weekday}: a day of the week
#' \item \code{percentage}: a share of non-missing observations for a given
#' day
#' \item Various tests' statistics and significance
#' }
#'
#' @references \itemize{
#' \item Corrado C.J., Zivney T.L. \emph{The Specification and Power of
#' the Sign Test in Event Study Hypothesis Tests Using Daily Stock Returns}.
#' Journal of Financial and Quantitative Analysis, 27(3):465-478, 1992.
#' \item McConnell J.J., Muscarella C.J. \emph{Capital expenditure plans and
#' firm value} Journal of Financial Economics, 14:399-422, 1985.
#' \item Boehmer E., Musumeci J., Poulsen A.B. \emph{Event-study methodology
#' under conditions of event-induced variance}. Journal of Financial Economics,
#' 30(2):253-272, 1991.
#' \item Cowan A.R. \emph{Nonparametric Event Study Tests}. Review of
#' Quantitative Finance and Accounting, 2:343-358, 1992.
#' \item Corrado C.J. \emph{A Nonparametric Test for Abnormal Security-Price
#' Performance in Event Studies}. Journal of Financial Economics 23:385-395,
#' 1989.
#' \item Campbell C.J., Wasley C.E. \emph{Measuring Security Price Performance
#' Using Daily NASDAQ Returns}. Journal of Financial Economics 33:73-92, 1993.
#' \item Savickas R. \emph{Event-Induced Volatility and Tests for Abnormal
#' Performance}. The Journal of Financial Research, 26(2):156-178, 2003.
#' \item Kolari J.W., Pynnonen S. \emph{Event Study Testing with Cross-sectional
#' Correlation of Abnormal Returns}. The Review of Financial Studies,
#' 23(11):3996-4025, 2010.
#' \item Wilcoxon F. \emph{Individual Comparisons by Ranking Methods}.
#' Biometrics Bulletin 1(6):80-83, 1945.
#' \item Lehmann E.L, \emph{Nonparametrics: Statistical Methods Based on Ranks}.
#' San Francisco: Holden-Day, 1975.
#' \item Hollander M., Wolfe D.A. \emph{Nonparametric Statistical Methods}.
#' New York: John Wiley & Sons, 1973.
#' }
#'
#' @seealso \code{\link{sign_test}}, \code{\link{generalized_sign_test}},
#' \code{\link{corrado_sign_test}}, \code{\link{rank_test}},
#' \code{\link{modified_rank_test}}, and \code{\link{wilcoxon_test}}.
#'
#' @examples
#' \dontrun{
#' library("magrittr")
#' rates_indx <- get_prices_from_tickers("^GSPC",
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous")
#' tickers <- c("AMZN", "ZM", "UBER", "NFLX", "SHOP", "FB", "UPWK")
#' nparam <- get_prices_from_tickers(tickers,
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous") %>%
#' apply_market_model(regressor = rates_indx,
#' same_regressor_for_all = TRUE,
#' market_model = "sim",
#' estimation_method = "ols",
#' estimation_start = as.Date("2019-04-01"),
#' estimation_end = as.Date("2020-03-13")) %>%
#' nonparametric_tests(event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#' }
#' ## The result of the code above is equivalent to:
#' data(securities_returns)
#' nparam <- nonparametric_tests(list_of_returns = securities_returns,
#' event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#'
#' @export
nonparametric_tests <- function(list_of_returns, event_start, event_end,
all = TRUE, tests) {
if(missing(tests)) {
if(all) {
tests <- list(sign_test, generalized_sign_test, corrado_sign_test,
rank_test, modified_rank_test, wilcoxon_test)
} else {
stop("Specify at least one test.")
}
} else {
message("Argument all will be ignored.")
for(i in seq_along(tests)) {
tests[[i]] <- match.fun(tests[[i]])
}
}
result <- NULL
for(test in tests) {
if(is.null(result)) {
result <- test(list_of_returns, event_start, event_end)
} else {
result <- merge(x = result, y = test(list_of_returns, event_start,
event_end)[, c(1, 4, 5)],
by = "date", all = TRUE)
}
}
return(result)
}
#' An event study simple binomial sign test.
#'
#' A binomial sign test which determines whether the frequency of positive
#' abnormal returns in the event period is significantly different from
#' one-half.
#'
#' This test is application of the simple binomial test to the event study,
#' which indicates whether the cross-sectional frequency of positive abnormal
#' returns is significantly different from 0.5. This test is stable
#' to outliers, in other words allows for checking if the result is driven by
#' few companies with extremely large abnormal performance. For this test the
#' estimation period and the event period must not overlap, otherwise an error
#' will be thrown. The test statistic is assumed to have a normal distribution
#' in approximation under a null hypothesis, if the number of securities is
#' large. Typically the test is used together with parametric tests.
#' The test is well-specified for the case, when cross-sectional abnormal
#' returns are not symmetric. Also this procedure is less sensitive to extreme
#' returns than the rank test. The significance levels of \eqn{\alpha} are 0.1,
#' 0.05, and 0.01 (marked respectively by *, **, and ***).
#'
#' @param list_of_returns a list of objects of S3 class \code{returns}, each
#' element of which is treated as a security.
#' @param event_start an object of \code{Date} class giving the first date of
#' the event period.
#' @param event_end an object of \code{Date} class giving the last date of the
#' event period.
#' @return A data frame of the following columns:
#' \itemize{
#' \item \code{date}: a calendar date
#' \item \code{weekday}: a day of the week
#' \item \code{percentage}: a share of non-missing observations for a given
#' day
#' \item \code{sign_stat}: a sign test statistic
#' \item \code{sign_signif}: a significance of the statistic
#' }
#'
#' @references Boehmer E., Musumeci J., Poulsen A.B. \emph{Event-study
#' methodology under conditions of event-induced variance}. Journal of Financial
#' Economics, 30(2):253-272, 1991.
#'
#' @seealso \code{\link{nonparametric_tests}}, \code{\link{generalized_sign_test}},
#' \code{\link{corrado_sign_test}}, \code{\link{rank_test}},
#' \code{\link{modified_rank_test}}, and \code{\link{wilcoxon_test}}.
#'
#' @examples
#' \dontrun{
#' library("magrittr")
#' rates_indx <- get_prices_from_tickers("^GSPC",
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous")
#' tickers <- c("AMZN", "ZM", "UBER", "NFLX", "SHOP", "FB", "UPWK")
#' get_prices_from_tickers(tickers,
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous") %>%
#' apply_market_model(regressor = rates_indx,
#' same_regressor_for_all = TRUE,
#' market_model = "sim",
#' estimation_method = "ols",
#' estimation_start = as.Date("2019-04-01"),
#' estimation_end = as.Date("2020-03-13")) %>%
#' sign_test(event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#' }
#' ## The result of the code above is equivalent to:
#' data(securities_returns)
#' sign_test(list_of_returns = securities_returns,
#' event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#'
#' @export
sign_test <- function(list_of_returns, event_start, event_end) {
# check event_start and event_end for class and value validity
if(!inherits(event_start, "Date")) {
stop("event_start must be an object of class Date.")
}
if(!inherits(event_end, "Date")) {
stop("event_end must be an object of class Date.")
}
if(event_start > event_end) {
stop("event_start must be earlier than event_end.")
}
# zoo objects of abnormal returns
event_binary <- NULL
for(i in seq_along(list_of_returns)) {
# check whether each element of list_of_returns is returns
if(!inherits(list_of_returns[[i]], "returns")) {
stop("Each element of list_of_rates must have class returns.")
}
if(list_of_returns[[i]]$estimation_end >= event_start) {
stop(paste0("For ", as.character(i), "-th company estimation",
" period overlaps with event period."))
}
company_event_abnormal <- zoo::as.zoo(list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >= event_start &
zoo::index(list_of_returns[[i]]$abnormal) <= event_end])
company_event_binary <- zoo::zoo(as.numeric(company_event_abnormal > 0),
zoo::index(company_event_abnormal))
if(is.null(event_binary)) {
event_binary <- company_event_binary
} else {
event_binary <- merge(event_binary, company_event_binary,
all = TRUE)
}
}
event_number_of_companies <- rowSums(!is.na(event_binary))
event_binary_sums <- rowMeans(event_binary, na.rm = TRUE) * ncol(event_binary)
event_binary_sums[is.nan(event_binary_sums)] <- NA
result <- data.frame(date = zoo::index(event_binary),
weekday = weekdays(zoo::index(event_binary)),
percentage = event_number_of_companies /
ncol(event_binary) * 100)
event_binary <- as.matrix(event_binary)
event_number_of_companies[event_number_of_companies == 0] <- NA
statistics <- (event_binary_sums - event_number_of_companies * 0.5) /
sqrt(event_number_of_companies * 0.25)
statistics[is.nan(statistics)] <- NA
significance <- rep("", length(statistics))
significance[abs(statistics) >= const_q1] <- "*"
significance[abs(statistics) >= const_q2] <- "**"
significance[abs(statistics) >= const_q3] <- "***"
result <- cbind(result, data.frame(sign_stat = statistics,
sign_signif = significance))
rownames(result) <- NULL
return(result)
}
#' An event study binomial sign test.
#'
#' A binomial sign test which determines whether the frequency of positive
#' abnormal returns in the event period is significantly different from the
#' frequency in the estimation period.
#'
#' This test is application of the binomial test to the event study,
#' which indicates whether the cross-sectional frequency of positive abnormal
#' returns is significantly different from the expected. This test is stable
#' to outliers, in other words allows for checking if the result is driven by
#' few companies with extremely large abnormal performance. For this test the
#' estimation period and the event period must not overlap, otherwise an error
#' will be thrown. This test uses an estimate from the estimation period instead
#' of using naive value of expected frequency 0.5. The test statistic is assumed
#' to have a normal distribution. Typically the test is used together with
#' parametric tests. The test is well-specified for the case, when
#' cross-sectional abnormal returns are not symmetric. Also this procedure is
#' less sensitive to extreme returns than the rank test. The significance levels
#' of \eqn{\alpha} are 0.1, 0.05, and 0.01 (marked respectively by *, **, and
#' ***).
#'
#' @param list_of_returns a list of objects of S3 class \code{returns}, each
#' element of which is treated as a security.
#' @param event_start an object of \code{Date} class giving the first date of
#' the event period.
#' @param event_end an object of \code{Date} class giving the last date of the
#' event period.
#' @return A data frame of the following columns:
#' \itemize{
#' \item \code{date}: a calendar date
#' \item \code{weekday}: a day of the week
#' \item \code{percentage}: a share of non-missing observations for a given
#' day
#' \item \code{gsign_stat}: a generalized sign test statistic
#' \item \code{gsign_signif}: a significance of the statistic
#' }
#'
#' @references \itemize{
#' \item McConnell J.J., Muscarella C.J. \emph{Capital expenditure plans and
#' firm value} Journal of Financial Economics, 14:399-422, 1985.
#' \item Cowan A.R. \emph{Nonparametric Event Study Tests}. Review of
#' Quantitative Finance and Accounting, 2:343-358, 1992.
#' }
#'
#' @seealso \code{\link{nonparametric_tests}}, \code{\link{sign_test}},
#' \code{\link{corrado_sign_test}}, \code{\link{rank_test}},
#' \code{\link{modified_rank_test}}, and \code{\link{wilcoxon_test}}.
#'
#' @examples
#' \dontrun{
#' library("magrittr")
#' rates_indx <- get_prices_from_tickers("^GSPC",
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous")
#' tickers <- c("AMZN", "ZM", "UBER", "NFLX", "SHOP", "FB", "UPWK")
#' get_prices_from_tickers(tickers,
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous") %>%
#' apply_market_model(regressor = rates_indx,
#' same_regressor_for_all = TRUE,
#' market_model = "sim",
#' estimation_method = "ols",
#' estimation_start = as.Date("2019-04-01"),
#' estimation_end = as.Date("2020-03-13")) %>%
#' generalized_sign_test(event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#' }
#' ## The result of the code above is equivalent to:
#' data(securities_returns)
#' generalized_sign_test(list_of_returns = securities_returns,
#' event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#'
#' @export
generalized_sign_test <- function(list_of_returns, event_start, event_end) {
# check event_start and event_end for class and value validity
if(!inherits(event_start, "Date")) {
stop("event_start must be an object of class Date.")
}
if(!inherits(event_end, "Date")) {
stop("event_end must be an object of class Date.")
}
if(event_start > event_end) {
stop("event_start must be earlier than event_end.")
}
# zoo objects of abnormal returns
estimation_binary <- NULL
event_binary <- NULL
for(i in seq_along(list_of_returns)) {
# check whether each element of list_of_returns is returns
if(!inherits(list_of_returns[[i]], "returns")) {
stop("Each element of list_of_rates must have class returns.")
}
if(list_of_returns[[i]]$estimation_end >= event_start) {
stop(paste0("For ", as.character(i), "-th company estimation",
" period overlaps with event period."))
}
company_estimation_abnormal <- zoo::as.zoo(list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >=
list_of_returns[[i]]$estimation_start &
zoo::index(list_of_returns[[i]]$abnormal) <=
list_of_returns[[i]]$estimation_end])
company_event_abnormal <- zoo::as.zoo(list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >= event_start &
zoo::index(list_of_returns[[i]]$abnormal) <= event_end])
company_estimation_binary <- zoo::zoo(
as.numeric(company_estimation_abnormal > 0),
zoo::index(company_estimation_abnormal))
company_event_binary <- zoo::zoo(as.numeric(company_event_abnormal > 0),
zoo::index(company_event_abnormal))
if(is.null(estimation_binary)) {
estimation_binary <- company_estimation_binary
} else {
estimation_binary <- merge(estimation_binary,
company_estimation_binary, all = TRUE)
}
if(is.null(event_binary)) {
event_binary <- company_event_binary
} else {
event_binary <- merge(event_binary, company_event_binary,
all = TRUE)
}
}
p_hat <- mean(as.matrix(estimation_binary), na.rm = TRUE)
event_number_of_companies <- rowSums(!is.na(event_binary))
event_binary_sums <- rowMeans(event_binary, na.rm = TRUE) * ncol(event_binary)
event_binary_sums[is.nan(event_binary_sums)] <- NA
result <- data.frame(date = zoo::index(event_binary),
weekday = weekdays(zoo::index(event_binary)),
percentage = event_number_of_companies /
ncol(event_binary) * 100)
# estimation_binary <- as.matrix(estimation_binary)
event_binary <- as.matrix(event_binary)
event_number_of_companies[event_number_of_companies == 0] <- NA
statistics <- (event_binary_sums - event_number_of_companies * p_hat) /
sqrt(event_number_of_companies * p_hat * (1 - p_hat))
statistics[is.nan(statistics)] <- NA
significance <- rep("", length(statistics))
significance[abs(statistics) >= const_q1] <- "*"
significance[abs(statistics) >= const_q2] <- "**"
significance[abs(statistics) >= const_q3] <- "***"
result <- cbind(result, data.frame(gsign_stat = statistics,
gsign_signif = significance))
rownames(result) <- NULL
return(result)
}
#' Corrado's sign test (1992).
#'
#' An event study nonparametric test described in Corrado and Zivney 1992.
#'
#' Performs a nonparametric test for the event study, which is described in
#' Corrado and Zivney 1992. This test is similar to procedure, described in
#' Brown and Warner 1985 (t-ratio), but instead of using abnormal
#' returns, the test uses \eqn{G_{i,t} = sign(A_{i,t} - median(A_i))}.
#' \code{sign} and \code{median} are ones, which have the same definition as R
#' functions. For this test the estimation period and the event period must not
#' overlap, otherwise an error will be thrown. The sign test procedure avoids
#' the misspecification of tests, which assumes symmetry around zero of abnormal
#' returns (the median equals to zero). For a single day the performance of this
#' test is proven to be better than classical Brown and Warner's test (without
#' event-induced variance). This test is dominated by rank test. The
#' significance levels of \eqn{\alpha} are 0.1, 0.05, and 0.01 (marked
#' respectively by *, **, and ***).
#'
#' @param list_of_returns a list of objects of S3 class \code{returns}, each
#' element of which is treated as a security.
#' @param event_start an object of \code{Date} class giving the first date of
#' the event period.
#' @param event_end an object of \code{Date} class giving the last date of the
#' event period.
#' @return A data frame of the following columns:
#' \itemize{
#' \item \code{date}: a calendar date
#' \item \code{weekday}: a day of the week
#' \item \code{percentage}: a share of non-missing observations for a given
#' day
#' \item \code{csign_stat}: a Corrado's sign test statistic
#' \item \code{csign_signif}: a significance of the statistic
#' }
#'
#' @references Corrado C.J., Zivney T.L. \emph{The Specification and Power of
#' the Sign Test in Event Study Hypothesis Tests Using Daily Stock Returns}.
#' Journal of Financial and Quantitative Analysis, 27(3):465-478, 1992.
#'
#' @seealso \code{\link{nonparametric_tests}}, \code{\link{sign_test}},
#' \code{\link{generalized_sign_test}}, \code{\link{rank_test}},
#' \code{\link{modified_rank_test}}, and \code{\link{wilcoxon_test}}.
#'
#' @examples
#' \dontrun{
#' library("magrittr")
#' rates_indx <- get_prices_from_tickers("^GSPC",
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous")
#' tickers <- c("AMZN", "ZM", "UBER", "NFLX", "SHOP", "FB", "UPWK")
#' get_prices_from_tickers(tickers,
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous") %>%
#' apply_market_model(regressor = rates_indx,
#' same_regressor_for_all = TRUE,
#' market_model = "sim",
#' estimation_method = "ols",
#' estimation_start = as.Date("2019-04-01"),
#' estimation_end = as.Date("2020-03-13")) %>%
#' corrado_sign_test(event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#' }
#' ## The result of the code above is equivalent to:
#' data(securities_returns)
#' corrado_sign_test(list_of_returns = securities_returns,
#' event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#'
#' @export
corrado_sign_test <- function(list_of_returns, event_start, event_end) {
# check event_start and event_end for class and value validity
if(!inherits(event_start, "Date")) {
stop("event_start must be an object of class Date.")
}
if(!inherits(event_end, "Date")) {
stop("event_end must be an object of class Date.")
}
if(event_start > event_end) {
stop("event_start must be earlier than event_end.")
}
# zoo objects of signs
event_sign <- NULL
full_sign <- NULL
delta_full <- numeric(length(list_of_returns))
for(i in seq_along(list_of_returns)) {
# check whether each element of list_of_returns is returns
if(!inherits(list_of_returns[[i]], "returns")) {
stop("Each element of list_of_rates must have class returns.")
}
if(list_of_returns[[i]]$estimation_end >= event_start) {
stop(paste0("For ", as.character(i), "-th company estimation",
" period overlaps with event period."))
}
company_full_abnormal <- zoo::as.zoo(c(
list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >=
list_of_returns[[i]]$estimation_start &
zoo::index(list_of_returns[[i]]$abnormal) <=
list_of_returns[[i]]$estimation_end],
list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >= event_start &
zoo::index(list_of_returns[[i]]$abnormal) <= event_end]))
company_event_abnormal <- zoo::as.zoo(list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >= event_start &
zoo::index(list_of_returns[[i]]$abnormal) <= event_end])
company_median <- stats::median(zoo::coredata(company_full_abnormal), na.rm = TRUE)
company_full_sign <- sign(company_full_abnormal - company_median)
company_event_sign <- sign(company_event_abnormal - company_median)
if(is.null(full_sign)) {
full_sign <- company_full_sign
} else {
full_sign <- merge(full_sign, company_full_sign, all = TRUE)
}
if(is.null(event_sign)) {
event_sign <- company_event_sign
} else {
event_sign <- merge(event_sign, company_event_sign, all = TRUE)
}
delta_full[i] <-
length(company_full_abnormal[!is.na(company_full_abnormal)])
}
event_number_of_companies <- rowSums(!is.na(event_sign))
result <- data.frame(date = zoo::index(event_sign),
weekday = weekdays(zoo::index(event_sign)),
percentage = event_number_of_companies /
ncol(event_sign) * 100)
full_sign <- as.matrix(full_sign)
event_sign <- as.matrix(event_sign)
number_of_companies <- rowSums(!is.na(full_sign))
number_of_companies[number_of_companies == 0] <- NA
full_sign_sums <- rowMeans(full_sign, na.rm = TRUE) * ncol(full_sign)
full_sign_sums[is.nan(full_sign_sums)] <- NA
sd_full <- sqrt(1 / mean(delta_full, na.rm = TRUE) *
sum((1 / sqrt(number_of_companies) * full_sign_sums)^2, na.rm = TRUE))
event_sign_sums <- rowMeans(event_sign, na.rm = TRUE) * ncol(event_sign)
event_sign_sums[is.nan(event_sign_sums)] <- NA
event_number_of_companies[event_number_of_companies == 0] <- NA
statistics <- 1 / sqrt(event_number_of_companies) *
event_sign_sums / sd_full
statistics[is.nan(statistics)] <- NA
significance <- rep("", length(statistics))
significance[abs(statistics) >= const_q1] <- "*"
significance[abs(statistics) >= const_q2] <- "**"
significance[abs(statistics) >= const_q3] <- "***"
result <- cbind(result, data.frame(csign_stat = statistics,
csign_signif = significance))
rownames(result) <- NULL
return(result)
}
#' An event study rank test.
#'
#' An original rank test applied to an event study, which is based on
#' Wilcoxon (1945) rank test.
#'
#' This procedure uses ranks of abnormal returns to examine significance of each
#' day in the event window. In order to get ranks of corresponding abnormal
#' returns, the procedure uses regular R function \code{\link{rank}} with
#' parameter \code{ties.method = "average"} and \code{na.last = "keep"}. For
#' this test the estimation period and the event period must not overlap,
#' otherwise an error will be thrown. The test statistic is assumed to have a
#' normal distribution (as an approximation). The test is well-specified for the
#' case, when cross-sectional abnormal returns are not symmetric. The test is
#' stable to variance increase during event window. This test is more sensitive
#' to extreme values than sign test. For data with missing data see the
#' \code{\link{modified_rank_test}}. The significance levels of \eqn{\alpha} are
#' 0.1, 0.05, and 0.01 (marked respectively by *, **, and ***).
#'
#' @param list_of_returns a list of objects of S3 class \code{returns}, each
#' element of which is treated as a security.
#' @param event_start an object of \code{Date} class giving the first date of
#' the event period.
#' @param event_end an object of \code{Date} class giving the last date of the
#' event period.
#' @return A data frame of the following columns:
#' \itemize{
#' \item \code{date}: a calendar date
#' \item \code{weekday}: a day of the week
#' \item \code{percentage}: a share of non-missing observations for a given
#' day
#' \item \code{rank_stat}: a rank test statistic
#' \item \code{rank_signif}: a significance of the statistic
#' }
#'
#' @references \itemize{
#' \item Corrado C.J. \emph{A Nonparametric Test for Abnormal Security-Price
#' Performance in Event Studies}. Journal of Financial Economics 23:385-395,
#' 1989.
#' \item Cowan A.R. \emph{Nonparametric Event Study Tests}. Review of
#' Quantitative Finance and Accounting, 2:343-358, 1992.
#' \item Campbell C.J., Wasley C.E. \emph{Measuring Security Price Performance
#' Using Daily NASDAQ Returns}. Journal of Financial Economics 33:73-92, 1993.
#' \item Savickas R. \emph{Event-Induced Volatility and Tests for Abnormal
#' Performance}. The Journal of Financial Research, 26(2):156-178, 2003.
#' }
#'
#' @seealso \code{\link{nonparametric_tests}},\code{\link{sign_test}},
#' \code{\link{generalized_sign_test}}, \code{\link{corrado_sign_test}},
#' \code{\link{modified_rank_test}}, and \code{\link{wilcoxon_test}}.
#'
#' @examples
#' \dontrun{
#' library("magrittr")
#' rates_indx <- get_prices_from_tickers("^GSPC",
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous")
#' tickers <- c("AMZN", "ZM", "UBER", "NFLX", "SHOP", "FB", "UPWK")
#' get_prices_from_tickers(tickers,
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous") %>%
#' apply_market_model(regressor = rates_indx,
#' same_regressor_for_all = TRUE,
#' market_model = "sim",
#' estimation_method = "ols",
#' estimation_start = as.Date("2019-04-01"),
#' estimation_end = as.Date("2020-03-13")) %>%
#' rank_test(event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#' }
#' ## The result of the code above is equivalent to:
#' data(securities_returns)
#' rank_test(list_of_returns = securities_returns,
#' event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#'
#' @export
rank_test <- function(list_of_returns, event_start, event_end) {
# check event_start and event_end for class and value validity
if(!inherits(event_start, "Date")) {
stop("event_start must be an object of class Date.")
}
if(!inherits(event_end, "Date")) {
stop("event_end must be an object of class Date.")
}
if(event_start > event_end) {
stop("event_start must be earlier than event_end.")
}
# zoo objects of abnormal returns
full_rank <- NULL
event_rank <- NULL
delta_full <- numeric(length(list_of_returns))
avg_rank <- numeric(length(list_of_returns))
for(i in seq_along(list_of_returns)) {
# check whether each element of list_of_returns is returns
if(!inherits(list_of_returns[[i]], "returns")) {
stop("Each element of list_of_rates must have class returns.")
}
if(list_of_returns[[i]]$estimation_end >= event_start) {
stop(paste0("For ", as.character(i), "-th company estimation",
" period overlaps with event period."))
}
company_full_abnormal <- zoo::as.zoo(c(
list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >=
list_of_returns[[i]]$estimation_start &
zoo::index(list_of_returns[[i]]$abnormal) <=
list_of_returns[[i]]$estimation_end],
list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >= event_start &
zoo::index(list_of_returns[[i]]$abnormal) <= event_end]))
company_full_rank <- zoo::zoo(rank(x = zoo::coredata(company_full_abnormal),
na.last = "keep",
ties.method = "average"),
zoo::index(company_full_abnormal))
company_event_rank <- zoo::as.zoo(company_full_rank[
zoo::index(company_full_rank) >= event_start &
zoo::index(company_full_rank) <= event_end])
if(is.null(full_rank)) {
full_rank <- company_full_rank
} else {
full_rank <- merge(full_rank, company_full_rank, all = TRUE)
}
if(is.null(event_rank)) {
event_rank <- company_event_rank
} else {
event_rank <- merge(event_rank, company_event_rank, all = TRUE)
}
delta_full[i] <-
length(company_full_abnormal[!is.na(company_full_abnormal)])
avg_rank[i] <- mean(company_full_rank, na.rm = TRUE)
}
event_number_of_companies <- rowSums(!is.na(event_rank))
result <- data.frame(date = zoo::index(event_rank),
weekday = weekdays(zoo::index(event_rank)),
percentage = event_number_of_companies /
ncol(event_rank) * 100)
full_rank <- as.matrix(full_rank)
event_rank <- as.matrix(event_rank)
number_of_companies <- rowSums(!is.na(full_rank))
number_of_companies[number_of_companies == 0] <- NA
event_number_of_companies[event_number_of_companies == 0] <- NA
avg_rank_full <- matrix(rep(avg_rank, nrow(full_rank)),
nrow = nrow(full_rank), ncol = ncol(full_rank),
byrow = TRUE)
avg_rank_event <- matrix(rep(avg_rank, nrow(event_rank)),
nrow = nrow(event_rank), ncol = ncol(event_rank),
byrow = TRUE)
full_differences <- rowMeans(full_rank - avg_rank_full, na.rm = TRUE) * ncol(full_rank)
full_differences[is.nan(full_differences)] <- NA
event_differences <- rowMeans(event_rank - avg_rank_event, na.rm = TRUE) * ncol(event_rank)
event_differences[is.nan(event_differences)] <- NA
sd_full <- sqrt(1 / mean(delta_full) * sum((1 / number_of_companies * full_differences)^2, na.rm = TRUE))
statistics <- 1 / event_number_of_companies * event_differences / sd_full
statistics[is.nan(statistics)] <- NA
significance <- rep("", length(statistics))
significance[abs(statistics) >= const_q1] <- "*"
significance[abs(statistics) >= const_q2] <- "**"
significance[abs(statistics) >= const_q3] <- "***"
result <- cbind(result, data.frame(rank_stat = statistics,
rank_signif = significance))
rownames(result) <- NULL
return(result)
}
#' An event study modified rank test.
#'
#' The test is the modification of the original rank test, proposed by Corrado
#' 1989. This test is adapted to missing values in abnormal returns.
#'
#' In addition to the original rank test, the procedure divides corresponding
#' ranks by the number of nonmissing returns plus one for each security. This
#' leads to order statistics with uniform distribution. In limit overall
#' statistics under a null hypothesis is approximately normally distributed. For
#' this test the estimation period and the event period must not overlap,
#' otherwise an error will be thrown. The test is well-specified for the case,
#' when cross-sectional abnormal returns are not symmetric. The test is stable
#' to variance increase during the event window. This test is more sensitive to
#' extreme values than the sign test. The significance levels of \eqn{\alpha}
#' are 0.1, 0.05, and 0.01 (marked respectively by *, **, and ***).
#'
#' @param list_of_returns a list of objects of S3 class \code{returns}, each
#' element of which is treated as a security.
#' @param event_start an object of \code{Date} class giving the first date of
#' the event period.
#' @param event_end an object of \code{Date} class giving the last date of the
#' event period.
#' @return A data frame of the following columns:
#' \itemize{
#' \item \code{date}: a calendar date
#' \item \code{weekday}: a day of the week
#' \item \code{percentage}: a share of non-missing observations for a given
#' day
#' \item \code{mrank_stat}: a modified rank test statistic
#' \item \code{mrank_signif}: a significance of the statistic
#' }
#'
#' @references \itemize{
#' \item Corrado C.J., Zivney T.L. \emph{The Specification and Power of
#' the Sign Test in Event Study Hypothesis Tests Using Daily Stock Returns}.
#' Journal of Financial and Quantitative Analysis, 27(3):465-478, 1992.
#' \item Kolari J.W., Pynnonen S. \emph{Event Study Testing with Cross-sectional
#' Correlation of Abnormal Returns}. The Review of Financial Studies,
#' 23(11):3996-4025, 2010.
#' }
#'
#' @seealso \code{\link{nonparametric_tests}},\code{\link{sign_test}},
#' \code{\link{generalized_sign_test}}, \code{\link{corrado_sign_test}},
#' \code{\link{rank_test}}, and \code{\link{wilcoxon_test}}.
#'
#' @examples
#' \dontrun{
#' library("magrittr")
#' rates_indx <- get_prices_from_tickers("^GSPC",
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous")
#' tickers <- c("AMZN", "ZM", "UBER", "NFLX", "SHOP", "FB", "UPWK")
#' get_prices_from_tickers(tickers,
#' start = as.Date("2019-04-01"),
#' end = as.Date("2020-04-01"),
#' quote = "Close",
#' retclass = "zoo") %>%
#' get_rates_from_prices(quote = "Close",
#' multi_day = TRUE,
#' compounding = "continuous") %>%
#' apply_market_model(regressor = rates_indx,
#' same_regressor_for_all = TRUE,
#' market_model = "sim",
#' estimation_method = "ols",
#' estimation_start = as.Date("2019-04-01"),
#' estimation_end = as.Date("2020-03-13")) %>%
#' modified_rank_test(event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#' }
#' ## The result of the code above is equivalent to:
#' data(securities_returns)
#' modified_rank_test(list_of_returns = securities_returns,
#' event_start = as.Date("2020-03-16"),
#' event_end = as.Date("2020-03-20"))
#'
#' @export
modified_rank_test <- function(list_of_returns, event_start, event_end) {
# Corrado Zivney 1992
# Kolari Pynnonen 2010
# check event_start and event_end for class and value validity
if(!inherits(event_start, "Date")) {
stop("event_start must be an object of class Date.")
}
if(!inherits(event_end, "Date")) {
stop("event_end must be an object of class Date.")
}
if(event_start > event_end) {
stop("event_start must be earlier than event_end.")
}
# zoo objects of abnormal returns
full_rank_modif <- NULL
event_rank_modif <- NULL
delta_full <- numeric(length(list_of_returns))
for(i in seq_along(list_of_returns)) {
# check whether each element of list_of_returns is returns
if(!inherits(list_of_returns[[i]], "returns")) {
stop("Each element of list_of_rates must have class returns.")
}
if(list_of_returns[[i]]$estimation_end >= event_start) {
stop(paste0("For ", as.character(i), "-th company estimation",
" period overlaps with event period."))
}
company_full_abnormal <- zoo::as.zoo(c(
list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >=
list_of_returns[[i]]$estimation_start &
zoo::index(list_of_returns[[i]]$abnormal) <=
list_of_returns[[i]]$estimation_end],
list_of_returns[[i]]$abnormal[
zoo::index(list_of_returns[[i]]$abnormal) >= event_start &
zoo::index(list_of_returns[[i]]$abnormal) <= event_end]))
company_full_rank_modif <- zoo::zoo(rank(x =
zoo::coredata(company_full_abnormal),
na.last = "keep",
ties.method = "average") /
(1 + sum(!is.na(company_full_abnormal))),
zoo::index(company_full_abnormal))
company_event_rank_modif <- zoo::as.zoo(company_full_rank_modif[
zoo::index(company_full_rank_modif) >= event_start &
zoo::index(company_full_rank_modif) <= event_end])
if(is.null(full_rank_modif)) {
full_rank_modif <- company_full_rank_modif
} else {
full_rank_modif <- merge(full_rank_modif, company_full_rank_modif,
all = TRUE)
}
if(is.null(event_rank_modif)) {
event_rank_modif <- company_event_rank_modif
} else {
event_rank_modif <- merge(event_rank_modif,
company_event_rank_modif, all = TRUE)
}
delta_full[i] <-
length(company_full_abnormal[!is.na(company_full_abnormal)])
}
event_number_of_companies <- rowSums(!is.na(event_rank_modif))
result <- data.frame(date = zoo::index(event_rank_modif),
weekday = weekdays(zoo::index(event_rank_modif)),
percentage = event_number_of_companies /
ncol(event_rank_modif) * 100)
full_rank_modif <- as.matrix(full_rank_modif)
event_rank_modif <- as.matrix(event_rank_modif)
number_of_companies <- rowSums(!is.na(full_rank_modif))
number_of_companies[number_of_companies == 0] <- NA
event_number_of_companies[event_number_of_companies == 0] <- NA
full_differences <- rowMeans(full_rank_modif - 0.5, na.rm = TRUE) * ncol(full_rank_modif)
full_differences[is.nan(full_differences)] <- NA
event_differences <- rowMeans(event_rank_modif - 0.5, na.rm = TRUE) * ncol(event_rank_modif)
event_differences[is.nan(event_differences)] <- NA
sd_full <- sqrt(1 / mean(delta_full) * sum((1 / sqrt(number_of_companies) * full_differences)^2, na.rm = TRUE))
statistics <- 1 / sqrt(event_number_of_companies) * event_differences / sd_full
statistics[is.nan(statistics)] <- NA
significance <- rep("", length(statistics))
significance[abs(statistics) >= const_q1] <- "*"
significance[abs(statistics) >= const_q2] <- "**"
significance[abs(statistics) >= const_q3] <- "***"
result <- cbind(result, data.frame(mrank_stat = statistics,
mrank_signif = significance))
rownames(result) <- NULL
return(result)
}
#' An event study Wilcoxon signed rank test.
#'
#' Performs Wilcoxon test on the event period for abnormal returns
#' (abnormal returns are considered as differences).
#'
#' The estimation periods can overlap with event windows, because the procedure
#' takes into account only abnormal returns from the event window. The test has
#' the same algorithm as built-in \code{R} \code{\link{wilcox.test}}. The
#' critical values are exact values, which are obtained from
#' \code{\link{qsignrank}}. The algorithm is the following: for each day in
#' event window the cross-sectional abnormal returns treated as sample of
#' differences. Firstly the absolute value of these differences are computed,
#' and corresponding ranks of non-zero values are calculated. The test statistic
#' is the sum of ranks, corresponding to positive abnormal returns. The