32
32
#
33
33
# 1. Implement general F-test for linear combinations in SARA_exploratory_analysis_general_F_test().
34
34
35
+ # ###################################
36
+ # Update by Tianchen Qian, 2019/2/5
37
+ #
38
+ # 1. Changed from one-sided test to two-sided test in all t-tests
39
+
40
+
35
41
library(rootSolve ) # for solver function multiroot()
36
42
37
43
# #########################################
@@ -62,7 +68,7 @@ binary_outcome_moderated_effect <- function(
62
68
outcome_var = " Y" ,
63
69
avail_var = NULL ,
64
70
prob_treatment = 1 / 2 ,
65
- significance_level = 0.025 )
71
+ significance_level = 0.05 )
66
72
{
67
73
# ############# description ###############
68
74
# #
@@ -93,7 +99,7 @@ binary_outcome_moderated_effect <- function(
93
99
# # avail_var.............variable name for availability variable
94
100
# # NULL (default) means always-available
95
101
# # prob_treatment........probability of treatment (default to 1/2)
96
- # # significance_level....significance level for the hypothesis testing (default to 0.025 )
102
+ # # significance_level....significance level for the hypothesis testing (default to 0.05 )
97
103
98
104
99
105
# ############# return value ###############
@@ -106,7 +112,7 @@ binary_outcome_moderated_effect <- function(
106
112
# # alpha_se..............standard error for alpha_hat
107
113
# # beta_se_ssa...........standard error for beta_hat, with small sample correction (hat matrix)
108
114
# # alpha_se..............standard error for alpha_hat, with small sample correction (hat matrix)
109
- # # test_result_t.........(one -sided) t-test result for each entry in beta_hat, which is a list consisting of test_stat, critical_value, p_value
115
+ # # test_result_t.........(two -sided) t-test result for each entry in beta_hat, which is a list consisting of test_stat, critical_value, p_value
110
116
# # test_result_f.........F-test result for beta = 0, which is a list consisting of test_stat, critical_value, p_value
111
117
# # varcov................estimated variance-covariance matrix for (beta_hat, alpha_hat)
112
118
# # varcov_ssa............estimated variance-covariance matrix for (beta_hat, alpha_hat), with small sample correction (hat matrix)
@@ -319,11 +325,11 @@ binary_outcome_moderated_effect <- function(
319
325
320
326
# ############# part 5 :: p-value with small sample correction ###############
321
327
322
- # t test (one sided, because we are using significance_level instead of significance_level/2)
328
+ # t test (two- sided -- note the use of significance_level/2)
323
329
324
330
test_stat <- beta_root / beta_se_ssa
325
- critical_value <- qt(1 - significance_level , df = n - p - q )
326
- p_val <- pt(abs(test_stat ), df = n - p - q , lower.tail = FALSE )
331
+ critical_value <- qt(1 - significance_level / 2 , df = n - p - q ) # two-sided
332
+ p_val <- 2 * pt(abs(test_stat ), df = n - p - q , lower.tail = FALSE ) # two-sided
327
333
names(test_stat ) <- names(p_val ) <- Xnames
328
334
test_result_t <- list (test_stat = test_stat ,
329
335
critical_value = critical_value ,
@@ -334,7 +340,6 @@ binary_outcome_moderated_effect <- function(
334
340
test_stat <- as.numeric( t(beta_root ) %*% solve(asymp_varcov_ssa [1 : p , 1 : p ] / sample_size ) %*% beta_root )
335
341
n <- sample_size
336
342
critical_value <- qf((n - q - p ) * (1 - significance_level ) / (p * (n - q - 1 )), df1 = p , df2 = n - q - p )
337
- # browser()
338
343
p_val <- pf(test_stat , df1 = p , df2 = n - q - p , lower.tail = FALSE )
339
344
test_result_f <- list (test_stat = test_stat ,
340
345
critical_value = critical_value ,
@@ -369,7 +374,7 @@ SARA_primary_hypothesis_1 <- function(
369
374
outcome_var = " Y" ,
370
375
avail_var = NULL ,
371
376
prob_treatment = 1 / 2 ,
372
- significance_level = 0.025
377
+ significance_level = 0.05
373
378
) {
374
379
# ############# description ###############
375
380
# #
@@ -392,17 +397,17 @@ SARA_primary_hypothesis_1 <- function(
392
397
# # avail_var.............variable name for availability variable
393
398
# # NULL (default) means always-available
394
399
# # prob_treatment........probability of treatment (default to 1/2)
395
- # # significance_level....significance level for the hypothesis testing (default to 0.025 )
400
+ # # significance_level....significance level for the hypothesis testing (default to 0.05 )
396
401
397
402
# ############# return value ###############
398
403
# #
399
404
# # This function returns a list of the following components:
400
405
# #
401
406
# # beta..................estimated beta (marginal treatment effect)
402
407
# # beta_se...............standard error for beta, with small sample correction
403
- # # test_stat.............(one sided) t-test statsitic for testing beta = 0
404
- # # critical_value........(one sided) critical value for t-test with the input significance level
405
- # # p_value...............(one sided) p-value for t-test
408
+ # # test_stat.............(two- sided) t-test statsitic for testing beta = 0
409
+ # # critical_value........(two- sided) critical value for t-test with the input significance level
410
+ # # p_value...............(two- sided) p-value for t-test
406
411
407
412
# make sure dta is sorted by id_var then day_var
408
413
dta <- dta [order(dta [, id_var ], dta [, day_var ]), ]
@@ -437,7 +442,7 @@ SARA_primary_hypothesis_2 <- function(
437
442
outcome_var = " Y" ,
438
443
avail_var = NULL ,
439
444
prob_treatment = 1 / 2 ,
440
- significance_level = 0.025
445
+ significance_level = 0.05
441
446
) {
442
447
443
448
# ############# description ###############
@@ -463,17 +468,17 @@ SARA_primary_hypothesis_2 <- function(
463
468
# # avail_var.............variable name for availability variable
464
469
# # NULL (default) means always-available
465
470
# # prob_treatment........probability of treatment (default to 1/2)
466
- # # significance_level....significance level for the hypothesis testing (default to 0.025 )
471
+ # # significance_level....significance level for the hypothesis testing (default to 0.05 )
467
472
468
473
# ############# return value ###############
469
474
# #
470
475
# # This function returns a list of the following components:
471
476
# #
472
477
# # beta..................estimated beta (marginal treatment effect)
473
478
# # beta_se...............standard error for beta, with small sample correction
474
- # # test_stat.............(one sided) t-test statsitic for testing beta = 0
475
- # # critical_value........(one sided) critical value for t-test with the input significance level
476
- # # p_value...............(one sided) p-value for t-test
479
+ # # test_stat.............(two- sided) t-test statsitic for testing beta = 0
480
+ # # critical_value........(two- sided) critical value for t-test with the input significance level
481
+ # # p_value...............(two- sided) p-value for t-test
477
482
478
483
# make sure survey_completion_var is binary
479
484
stopifnot(all(dta [, survey_completion_var ] %in% c(0 , 1 )))
@@ -533,7 +538,7 @@ SARA_exploratory_analysis <- function(
533
538
outcome_var = " Y" ,
534
539
avail_var = NULL ,
535
540
prob_treatment = 1 / 2 ,
536
- significance_level = 0.025
541
+ significance_level = 0.05
537
542
) {
538
543
# ############# description ###############
539
544
# #
@@ -556,17 +561,17 @@ SARA_exploratory_analysis <- function(
556
561
# # avail_var.............variable name for availability variable
557
562
# # NULL (default) means always-available
558
563
# # prob_treatment........probability of treatment (default to 1/2)
559
- # # significance_level....significance level for the hypothesis testing (default to 0.025 )
564
+ # # significance_level....significance level for the hypothesis testing (default to 0.05 )
560
565
561
566
# ############# return value ###############
562
567
# #
563
568
# # This function returns a list of the following components:
564
569
# #
565
570
# # beta..................estimated beta (moderated treatment effect)
566
571
# # beta_se...............standard error for beta, with small sample correction
567
- # # test_stat_t.............(one sided) t-test statsitic for testing beta = 0
568
- # # critical_value_t........(one sided) critical value for t-test with the input significance level
569
- # # p_value_t...............(one sided) p-value for t-test
572
+ # # test_stat_t.............(two- sided) t-test statsitic for testing beta = 0
573
+ # # critical_value_t........(two- sided) critical value for t-test with the input significance level
574
+ # # p_value_t...............(two- sided) p-value for t-test
570
575
# # test_stat_f.............F-test statsitic for testing all beta's = 0
571
576
# # critical_value_f........critical value for F-test with the input significance level
572
577
# # p_value_f...............p-value for F-test
@@ -610,7 +615,7 @@ SARA_exploratory_analysis_general_F_test <- function(
610
615
outcome_var = " Y" ,
611
616
avail_var = NULL ,
612
617
prob_treatment = 1 / 2 ,
613
- significance_level = 0.025 ,
618
+ significance_level = 0.05 ,
614
619
F_test_L ,
615
620
F_test_c = NULL
616
621
) {
@@ -635,7 +640,7 @@ SARA_exploratory_analysis_general_F_test <- function(
635
640
# # avail_var.............variable name for availability variable
636
641
# # NULL (default) means always-available
637
642
# # prob_treatment........probability of treatment (default to 1/2)
638
- # # significance_level....significance level for the hypothesis testing (default to 0.025 )
643
+ # # significance_level....significance level for the hypothesis testing (default to 0.05 )
639
644
# # F_test_L, F_test_c....test for H_0: F_test_L %*% beta_hat = F_test_c,
640
645
# # where dim(beta) = p * 1, dim(F_test_L) = p1 * p, dim(F_test_c) = p1 * 1.
641
646
# # If F_test_L is passed in as a vector, it will be treated as a row vector.
@@ -647,9 +652,9 @@ SARA_exploratory_analysis_general_F_test <- function(
647
652
# #
648
653
# # beta..................estimated beta (moderated treatment effect)
649
654
# # beta_se...............standard error for beta, with small sample correction
650
- # # test_stat_t.............(one sided) t-test statsitic for testing beta = 0
651
- # # critical_value_t........(one sided) critical value for t-test with the input significance level
652
- # # p_value_t...............(one sided) p-value for t-test
655
+ # # test_stat_t.............(two- sided) t-test statsitic for testing beta = 0
656
+ # # critical_value_t........(two- sided) critical value for t-test with the input significance level
657
+ # # p_value_t...............(two- sided) p-value for t-test
653
658
# # test_stat_f.............F-test statsitic for testing F_test_L %*% beta_hat = F_test_c
654
659
# # critical_value_f........critical value for F-test with the input significance level
655
660
# # p_value_f...............p-value for F-test
@@ -677,6 +682,7 @@ SARA_exploratory_analysis_general_F_test <- function(
677
682
p <- length(beta_hat )
678
683
beta_hat <- matrix (beta_hat , ncol = 1 )
679
684
varcov_beta_hat <- result $ varcov_ssa [1 : p , 1 : p ]
685
+
680
686
# # general F test for F_test_L %*% beta_hat = F_test_c ##
681
687
if (is.vector(F_test_L )) {
682
688
F_test_L <- matrix (F_test_L , nrow = 1 )
@@ -760,7 +766,10 @@ if (0) {
760
766
761
767
if (0 ) {
762
768
# output of the above example
763
- # Version: 2018/10/22
769
+ # Version: 2019/2/5
770
+
771
+ # All the p-values for t-tests are doubled compared to the previous version.
772
+ # This is as expected, because we changed from one-sided to two-sided test.
764
773
765
774
766
775
> # ## try out the three analysis functions ###
@@ -780,7 +789,7 @@ if (0) {
780
789
[1 ] 1.984984
781
790
782
791
$ p_value
783
- [1 ] 5.112921e-15
792
+ [1 ] 1.022584e-14
784
793
785
794
>
786
795
> # primary hypothesis 2
@@ -798,7 +807,7 @@ if (0) {
798
807
[1 ] 1.984984
799
808
800
809
$ p_value
801
- [1 ] 0.3619496
810
+ [1 ] 0.7238992
802
811
803
812
>
804
813
> # exploratory analysis
@@ -818,7 +827,7 @@ if (0) {
818
827
[1 ] 1.985251
819
828
820
829
$ p_value_t
821
- [1 ] 1.285891e -09 2.581879e -04
830
+ [1 ] 2.571782e -09 5.163757e -04
822
831
823
832
$ test_stat_f
824
833
[1 ] 95.53464
@@ -846,7 +855,7 @@ if (0) {
846
855
[1 ] 1.985251
847
856
848
857
$ p_value_t
849
- [1 ] 1.285891e -09 2.581879e -04
858
+ [1 ] 2.571782e -09 5.163757e -04
850
859
851
860
$ test_stat_f
852
861
[1 ] 95.53464
@@ -873,7 +882,7 @@ if (0) {
873
882
[1 ] 1.985251
874
883
875
884
$ p_value_t
876
- [1 ] 1.285891e -09 2.581879e -04
885
+ [1 ] 2.571782e -09 5.163757e -04
877
886
878
887
$ test_stat_f
879
888
[1 ] 40.83885
@@ -905,7 +914,7 @@ if (0) {
905
914
[1 ] 1.984984
906
915
907
916
$ p_value
908
- [1 ] 0.01703565
917
+ [1 ] 0.03407131
909
918
910
919
> SARA_primary_hypothesis_2(dta2 , control_var = c(" Y_lag1" , " at_tapcount_lag1" ), survey_completion_var = " Y" , avail_var = " avail" )
911
920
$ beta
@@ -921,7 +930,7 @@ if (0) {
921
930
[1 ] 1.984984
922
931
923
932
$ p_value
924
- [1 ] 0.3034204
933
+ [1 ] 0.6068408
925
934
926
935
> SARA_exploratory_analysis(dta2 , control_var = c(" Y_lag1" , " at_tapcount_lag1" ), moderator = " Y_lag1" , avail_var = " avail" )
927
936
$ beta
@@ -939,7 +948,7 @@ if (0) {
939
948
[1 ] 1.985251
940
949
941
950
$ p_value_t
942
- [1 ] 0.1047124 0.2798671
951
+ [1 ] 0.2094249 0.5597343
943
952
944
953
$ test_stat_f
945
954
[1 ] 4.588463
@@ -949,4 +958,4 @@ if (0) {
949
958
950
959
$ p_value_f
951
960
[1 ] 0.01252342
952
- }
961
+ }
0 commit comments