-
Notifications
You must be signed in to change notification settings - Fork 2
/
miscstat.R
1596 lines (1371 loc) · 55.4 KB
/
miscstat.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
# miscstat.R
local({
tmp_require_package_namespace <- function(...) {
packages <- as.character(match.call(expand.dots = FALSE)[[2]])
for (p in packages) if (!requireNamespace(p)) install.packages(p)
}
tmp_require_package_namespace(
data.table,
ggplot2,
lmerTest,
emmeans,
MASS,
moments,
multcomp,
plyr
)
})
# =============================================================================
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
# =============================================================================
miscstat <- new.env()
miscstat$MAX_EXPONENT <- log(.Machine$double.xmax)
miscstat$VERY_SMALL_NUMBER <- 1e-323 # .Machine$double.xmin is 2.2e-308, but this seems to manage.
# =============================================================================
# Cosmetics
# =============================================================================
miscstat$heading <- function(x)
{
line <- "==============================================================================="
cat(paste("\n", line, "\n", x, "\n", line, "\n", sep = ""))
}
miscstat$subheading <- function(x)
{
line <- "-------------------------------------------------------------------------------"
cat(paste("\n", line, "\n", x, "\n", line, "\n", sep = ""))
}
# =============================================================================
# Working with machine limits
# =============================================================================
miscstat$convert_zero_to_very_small_number <- function(x) {
# for logs: or log(0) will give -Inf and crash the L-BFGS-B optimzer
ifelse(x == 0, miscstat$VERY_SMALL_NUMBER, x)
}
miscstat$reset_rng_seed <- function() {
set.seed(0xbeef)
}
# =============================================================================
# Efficient calculation with extremely small numbers
# =============================================================================
miscstat$log_of_mean_of_numbers_in_log_domain <- function(log_v) {
# http://stackoverflow.com/questions/7355145/mean-of-very-small-values
max_log <- max(log_v)
logsum <- max_log + log(sum(exp(log_v - max_log)))
logmean <- logsum - log(length(log_v))
return(logmean)
}
# =============================================================================
# Summary statistics
# =============================================================================
miscstat$sem <- function(x, na.rm = FALSE) {
# Calculate the standard error of the mean (SEM) for the sample x.
# - SEM = SD / sqrt(n) = sqrt(variance / n).
# - If na.rm is FALSE: won't do anything silly with NA values since var()
# will return NA in that case... so it will return NA.
# - If na.rm is TRUE, all NA values are removed.
# - But it does fail with NULL == c() as a parameter.
if (na.rm) {
x <- x[!is.na(x)]
}
if (is.null(x)) {
return(NA) # as for mean(NULL)
}
sqrt(var(x) / length(x))
}
# TEST:
# # QQ2, p62, of my 2004 handout (answers p97):
# q1 <- c(4.32, 5.07, 4.29, 6.02, 5.11, 4.93, 3.98, 4.83, 5.50, 6.10)
# sem(q1) # 0.2249901; correct
# q2 <- c(605, 460, 752, 321, 550, 612, 700, 680, 800, 491, 523, 594)
# sem(q2) # 38.57447; correct
miscstat$half_confidence_interval_t <- function(x, ci = 0.95, na.rm = FALSE) {
# Calculate half the confidence interval associated with a sample x, via a
# t test.
if (na.rm) {
x <- x[!is.na(x)]
}
n <- length(x)
if (n <= 1) {
return(NA)
}
df <- n - 1
sem <- miscstat$sem(x)
crit_p <- 1 - ((1 - ci) / 2) # e.g. 0.975 for ci == 0.95
crit_t <- qt(crit_p, df = df)
return(crit_t * sem)
# confidence interval is mean +/- that
}
# See working in my 2004 lecture handout, p54.
# Tests in confidence_interval_t().
miscstat$confidence_interval_t <- function(x, ci = 0.95, na.rm = FALSE) {
# Calculate the confidence interval associated with a sample x, via a t
# test.
hci <- half_confidence_interval_t(x, ci, na.rm = na.rm)
m <- mean(x, na.rm = na.rm)
return(c("ci_lower" = m - hci, "ci_upper" = m + hci))
}
# TEST:
# # Q1-Q2, p62, of my 2004 handout (answers p97):
# q1 <- c(4.32, 5.07, 4.29, 6.02, 5.11, 4.93, 3.98, 4.83, 5.50, 6.10)
# confidence_interval_t(q1, ci = 0.90) # 4.602568 5.427432; correct
# q2 <- c(605, 460, 752, 321, 550, 612, 700, 680, 800, 491, 523, 594)
# confidence_interval_t(q2) # 505.7648 675.5685; correct
miscstat$confidence_interval_from_mu_sem_df_via_t <- function(
mu, sem, df, ci = 0.95
) {
# From a mean, standard error of the mean, and degrees of freedom,
# calculate the confidence interval via a t test.
crit_p <- 1 - ((1 - ci) / 2) # e.g. 0.975 for ci == 0.95
crit_t <- qt(crit_p, df = df)
half_ci <- crit_t * sem
return(data.frame("ci_lower" = mu - half_ci, "ci_upper" = mu + half_ci))
}
# TEST:
# confidence_interval_from_mu_sem_df_via_t(0, 1, 1e6)
# # ... -1.959966, 1.959966
# # ... approaches the Z version as df -> infinity.
miscstat$confidence_interval_from_mu_sem_via_Z <- function(
mu, sem, ci = 0.95
) {
# From a mean and standard error of the mean, calculate the confidence
# interval via a Z test.
# - CI = mean +/- Z(CI) * SEM = mean +/- Z(CI) * SD/sqrt(n)
crit_p <- 1 - ((1 - ci) / 2) # e.g. 0.975 for 95% CI
crit_z <- qnorm(crit_p) # e.g. +1.96 for 95% CI
half_ci <- crit_z * sem
return(data.frame("ci_lower" = mu - half_ci, "ci_upper" = mu + half_ci))
}
# TEST:
# confidence_interval_from_mu_sem_via_Z(0, 1)
# # ... -1.959964, 1.959964
# This is definitional (working in my 2004 lecture handout, p16).
miscstat$logistic_regression_odds_ratios <- function(model,
confint_level = 0.95)
{
# The summary() shows log odds in the "Estimate" column.
# Confidence intervals:
# - https://stats.stackexchange.com/questions/8661/logistic-regression-in-r-odds-ratio
require(MASS)
result <- exp(cbind(coef(model), confint(model, level = confint_level)))
# Could also use confint.default:
# https://stats.stackexchange.com/questions/304833/how-to-calculate-odds-ratio-and-95-confidence-interval-for-logistic-regression/304909
colnames(result)[1] <- "Odds ratio"
return(result)
}
miscstat$summarize_by_factors <- function(data, depvarname, factornames,
na.rm = FALSE) {
data.table(plyr::ddply(
data,
factornames,
function(drow) {
values <- drow[, depvarname]
if (na.rm) {
values <- values[!is.na(values)]
}
c(
n = length(values),
mean = mean(values),
min = min(values),
max = max(values),
sd = sd(values),
var = var(values),
sem = miscstat$sem(values)
)
}
))
}
miscstat$summarize_by_factors_datatable <- function(dt, depvarname, factornames) {
# http://stackoverflow.com/questions/12391950/variably-selecting-assigning-to-fields-in-a-data-table
dt[
,
.(
n = length(get(depvarname)),
mean = mean(get(depvarname)),
min = min(get(depvarname)),
max = max(get(depvarname)),
sd = sd(get(depvarname)),
var = var(get(depvarname)),
sem = miscstat$sem(get(depvarname))
),
by = factornames
]
# NOTE: doesn't care whether the "by" things are really factors or not.
}
miscstat$weighted_sd <- function(x, weights = NULL, ...) {
# Weighted standard deviation.
# See also:
# - https://stackoverflow.com/questions/10049402/calculating-weighted-mean-and-standard-deviation
# - https://www.statology.org/weighted-standard-deviation-in-r/
# Example:
#
# x <- c(rep(10, 5), rep(20, 15))
# w <- c(rep( 3, 5), rep(1, 15))
#
# mean(x) # 17.5
#
# stats::weighted.mean(x, w) # 15
# Hmisc::wtd.mean(x, w) # 15
#
# stats::var(x) # 19.7
# Hmisc::wtd.var(x, w) # 25.9
#
# stats::sd(x) # 4.44
# sqrt(Hmisc::wtd.var(x, w)) # 5.08
weighted_variance <- Hmisc::wtd.var(x, weights = weights, ...)
return(sqrt(weighted_variance))
}
# Weighted SEM: no clear definition
# - https://stats.stackexchange.com/questions/25895/computing-standard-error-in-weighted-mean-estimation
# =============================================================================
# Simple comparisons of data
# =============================================================================
miscstat$pretty_two_group_t_test <- function(values_a, values_b,
familywise_n = 1,
alpha_variance = 0.05) {
values_a <- values_a[!is.na(values_a)]
values_b <- values_b[!is.na(values_b)]
mean_a <- mean(values_a)
mean_b <- mean(values_b)
sem_a <- miscstat$sem(values_a)
sem_b <- miscstat$sem(values_b)
n_a <- length(values_a)
n_b <- length(values_b)
r <- list(
t = NA,
df = NA,
uncorrected_p = NA,
corrected_p = NA,
mean_a = mean_a,
mean_b = mean_b,
sem_a = sem_a,
sem_b = sem_b,
equal_variances = NA,
pretty_mean_sem_a = ifelse(
n_a > 0,
paste(miscmath$format_sf(mean_a), "±",
miscmath$format_sf(sem_a)),
""),
pretty_mean_sem_b = ifelse(
n_b > 0,
paste(miscmath$format_sf(mean_b), "±",
miscmath$format_sf(sem_b)),
""),
pretty_p = "",
familywise_n = familywise_n,
n_a = n_a,
n_b = n_b
)
if (n_a == 0 || n_b == 0 || is.null(values_a) || is.null(values_b)) {
return(r)
}
v <- var.test(values_a, values_b)
r$equal_variances <- v$p.value <= alpha_variance
t <- t.test(values_a, values_b, var.equal = r$equal_variances)
r$t <- t$statistic
r$df <- t$parameter
r$uncorrected_p <- t$p.value
r$corrected_p <- miscstat$sidak_p(r$uncorrected_p, familywise_n)
r$pretty_p <- miscmath$describe_p_value_with_stars(r$corrected_p)
return(r)
}
miscstat$pretty_two_group_chisq_contingency_test <- function(
values_a, values_b, familywise_n = 1, factor_levels = NULL,
count_sep = ":", # can also use ":n="
DEBUG = FALSE) {
values_a <- values_a[!is.na(values_a)]
values_b <- values_b[!is.na(values_b)]
n_a <- length(values_a)
n_b <- length(values_b)
r <- list(
chisq = NA,
df = NA,
uncorrected_p = NA,
corrected_p = NA,
pretty_counts_a = "",
pretty_counts_b = "",
pretty_p = "",
familywise_n = familywise_n,
n_a = n_a,
n_b = n_b
)
value_vec <- c(as.character(values_a), as.character(values_b))
# if you don't use as.character, then existing factors get munged into
# integers: http://stackoverflow.com/questions/3443576/how-to-concatenate-factors
if (is.null(factor_levels)) {
final_levels <- unique(value_vec)
} else {
final_levels <- factor_levels
}
relevelled <- factor(value_vec, levels = final_levels)
dat <- data.table(
group = c(rep("a", length(values_a)), rep("b", length(values_b))),
value = relevelled
)
datsum <- dat[, .(count = .N), by = .(group, value)]
setkey(datsum, group, value)
if (DEBUG) {
print(values_a)
print(values_b)
print(factor_levels)
print(final_levels)
print(relevelled)
print(dat)
print(datsum)
}
r$pretty_counts_a <- paste(
datsum[group == "a",
.(pretty = paste(value, count, sep = count_sep))]$pretty,
collapse = ", "
)
r$pretty_counts_b <- paste(
datsum[group == "b",
.(pretty = paste(value, count, sep = count_sep))]$pretty,
collapse = ", "
)
if (n_a == 0 || n_b == 0) {
return(r)
}
datgrid <- cbind(
datsum[group == "a", .(count_a = count)],
datsum[group == "b", .(count_b = count)]
)
chi <- chisq.test(datgrid)
r$chisq <- chi$statistic
r$df <- chi$parameter
r$uncorrected_p <- chi$p.value
r$corrected_p <- miscstat$sidak_p(r$uncorrected_p, familywise_n)
r$pretty_p <- miscmath$describe_p_value_with_stars(r$corrected_p)
return(r)
}
miscstat$pretty_two_group_paired_regression <- function(
DT, ycolname, xcolname, groupcolname, grouplevel_a, grouplevel_b,
overall_positive_symbol = "/", overall_negative_symbol = "\\",
overall_ns_symbol = "·",
slope_positive_symbol = "/ ", slope_negative_symbol = "\\ ",
slope_ns_symbol = "· ",
a_positive_symbol = "/", a_negative_symbol = "\\", a_ns_symbol = "·",
b_positive_symbol = "/", b_negative_symbol = "\\", b_ns_symbol = "·",
slopediff_b_bigger_symbol = "↑", slopediff_b_smaller_symbol = "↓",
slopediff_ns_symbol = "·",
b_bigger_symbol = ">", b_smaller_symbol = "<",
groupdiff_ns_symbol = "·",
simple_test_prefix = " ",
familywise_n = 1, alpha = 0.05, alpha_variance = 0.05, sep = "",
DEBUG = FALSE, show_p = FALSE
) {
# Returns a string with:
# - overall (simple) correlation, y ~ x
# - main linear effect (X) in y ~ x + group + x:group
# - simple correlation y ~ x within subset where group == grouplevel_a
# - simple correlation y ~ x within subset where group == grouplevel_b
# - x:group interaction in y ~ x + group + x:group
# - group effect in y ~ x + group + x:group
# Spare characters:
# –¶§
groupvar <- DT[[groupcolname]]
passed_levels <- sort(c(grouplevel_a, grouplevel_b))
dt_levels <- sort(levels(groupvar))
if (!all(passed_levels == dt_levels)) {
stop(paste("Levels from parameters (",
paste(passed_levels, collapse = ", "),
") don't match levels from data table (",
paste(dt_levels, collapse = ", "),
")", sep = ""))
}
newdt <- data.table(x = DT[[xcolname]],
y = DT[[ycolname]],
group = groupvar)
if (xcolname == ycolname) {
# Comparing something to itself. Just a t-test.
values_a <- newdt[group == grouplevel_a, x]
values_b <- newdt[group == grouplevel_b, x]
t_result <- miscstat$pretty_two_group_t_test(
values_a, values_b,
familywise_n = familywise_n,
alpha_variance = alpha_variance)
result <- paste(
simple_test_prefix,
ifelse(
t_result$corrected_p <= alpha,
ifelse(t_result$mean_b > t_result$mean_a,
b_bigger_symbol,
b_smaller_symbol),
groupdiff_ns_symbol
),
sep = sep
)
if (DEBUG) print(t_result)
return(result)
}
# Could do the "overall" test ignoring group:
overall_test <- cor.test(~ x + y, data = newdt)
overall_p <- miscstat$sidak_p(overall_test$p.value, familywise_n)
overall_positive <- overall_p <= alpha && overall_test$estimate > 0
overall_negative <- overall_p <= alpha && overall_test$estimate < 0
a_test <- cor.test(~ x + y, data = subset(newdt, group == grouplevel_a))
a_p <- miscstat$sidak_p(a_test$p.value, familywise_n)
a_positive <- a_p <= alpha && a_test$estimate > 0
a_negative <- a_p <= alpha && a_test$estimate < 0
b_test <- cor.test(~ x + y, data = subset(newdt, group == grouplevel_b))
b_p <- miscstat$sidak_p(b_test$p.value, familywise_n)
b_positive <- b_p <= alpha && b_test$estimate > 0
b_negative <- b_p <= alpha && b_test$estimate < 0
fullmodel <- glm(y ~ x + group + x:group, data = newdt)
fullmodel_summ <- summary(fullmodel)
COEFF_NUM_SLOPE <- 2
# ... the effect of the linear predictor, x
slope_coeff <- fullmodel$coefficients[COEFF_NUM_SLOPE]
raw_slope_p <- fullmodel_summ$coefficients[COEFF_NUM_SLOPE, "Pr(>|t|)"]
slope_p <- miscstat$sidak_p(raw_slope_p, familywise_n)
slope_positive <- slope_p <= alpha && slope_coeff > 0
slope_negative <- slope_p <= alpha && slope_coeff < 0
# Group differences: are these particularly important here?
# The "self-to-self" comparison does a plain t-test; not sure if this
# adds important and useful information (an effect of Group in predicting
# Y, over and above X, whether or not X differs by Group...). Maybe it
# does. But maybe not for a giant summary plot.
COEFF_NUM_GROUPDIFF <- 3
# ... the effect of the factor, as group_level_b
groupdiff_coeff <- fullmodel$coefficients[COEFF_NUM_GROUPDIFF]
raw_groupdiff_p <- fullmodel_summ$coefficients[COEFF_NUM_GROUPDIFF,
"Pr(>|t|)"]
groupdiff_p <- miscstat$sidak_p(raw_groupdiff_p, familywise_n)
groupdiff_b_bigger <- groupdiff_p <= alpha && groupdiff_coeff > 0
groupdiff_b_smaller <- groupdiff_p <= alpha && groupdiff_coeff < 0
COEFF_NUM_SLOPEDIFF <- 4
# ... the additive effect of x:group_level_b
slopediff_coeff <- fullmodel$coefficients[COEFF_NUM_SLOPEDIFF]
raw_slopediff_p <- fullmodel_summ$coefficients[COEFF_NUM_SLOPEDIFF,
"Pr(>|t|)"]
slopediff_p <- miscstat$sidak_p(raw_slopediff_p, familywise_n)
slopediff_b_bigger <- slopediff_p <= alpha && slopediff_coeff > 0
slopediff_b_smaller <- slopediff_p <= alpha && slopediff_coeff < 0
result <- paste(
ifelse(overall_positive, overall_positive_symbol,
ifelse(overall_negative, overall_negative_symbol,
overall_ns_symbol)),
ifelse(slope_positive, slope_positive_symbol,
ifelse(slope_negative, slope_negative_symbol, slope_ns_symbol)),
ifelse(a_positive, a_positive_symbol,
ifelse(a_negative, a_negative_symbol, a_ns_symbol)),
ifelse(b_positive, b_positive_symbol,
ifelse(b_negative, b_negative_symbol, b_ns_symbol)),
ifelse(slopediff_b_bigger, slopediff_b_bigger_symbol,
ifelse(slopediff_b_smaller,
slopediff_b_smaller_symbol, slopediff_ns_symbol)),
ifelse(groupdiff_b_bigger, b_bigger_symbol,
ifelse(groupdiff_b_smaller, b_smaller_symbol,
groupdiff_ns_symbol)),
ifelse(
show_p,
paste(
" (",
paste(
miscmath$describe_p_value(overall_p),
miscmath$describe_p_value(slope_p),
miscmath$describe_p_value(a_p),
miscmath$describe_p_value(b_p),
miscmath$describe_p_value(slopediff_p),
miscmath$describe_p_value(groupdiff_p),
sep = ", "
),
")",
sep = ""
),
""
),
sep = sep
)
if (DEBUG) {
print(newdt)
cat("--- overall_test:\n")
print(overall_test)
cat("--- a_test:\n")
print(a_test)
cat("--- b_test:\n")
print(b_test)
cat("--- slopetest:\n")
print(slopetest)
print(slopetest_summ)
}
return(result)
}
miscstat$IGNORE_ME <- '
DT <- data.table(
x = c(1, 2, 3, 10, 11, 12),
y = c(4, 5, 6, 20, 19, 18),
g = factor(c(1, 1, 1, 2, 2, 2))
)
miscstat$pretty_two_group_paired_regression(DT, "y", "x", "g", 1, 2)
'
miscstat$two_group_multiple_regression_table <- function(
DT, groupcolname, varcolnames,
grouplevel_a, grouplevel_b,
blank = "", upper = FALSE, DEBUG = FALSE,
correct_multiple_comparisons = TRUE, # only use FALSE for debugging!
transpose = FALSE
) {
# If correct_multiple_comparisons is TRUE (the default),
# group-difference comparisons are treated as a family (ttest_k),
# and all other comparisons are treated as a family (pairwise_k).
# For example, for 59 groups, group differences are corrected for 59
# comparisons, and other comparisons for 59 * 58 / 2 = 1711 comparisons.
# For things other than "self-to-self = group test" comparisons,
# Within each cell there are 6 tests (pretty_two_group_paired_regression)
# but these are not additionally corrected for.
n <- length(varcolnames)
if (!correct_multiple_comparisons) {
warning("Not correcting for multiple comparisons!")
}
ttest_k <- ifelse(correct_multiple_comparisons, n, 1)
pairwise_k <- ifelse(correct_multiple_comparisons, n * (n - 1) / 2, 1)
cat("miscstat$two_group_multiple_regression_table: ttest_k = ", ttest_k,
", pairwise_k = ", pairwise_k, "\n", sep = "")
m <- matrix(blank, ncol = n, nrow = n)
for (rownum in 1:n) {
if (upper) {
cstart <- rownum
cend <- n
} else {
cstart <- 1
cend <- rownum
}
if (DEBUG) cat(paste("cstart", cstart, "cend", cend, "\n"))
for (colnum in cstart:cend) {
xcolname <- varcolnames[rownum]
ycolname <- varcolnames[colnum]
familywise_n <- ifelse(rownum == colnum, ttest_k, pairwise_k)
txt <- miscstat$pretty_two_group_paired_regression(
DT, ycolname, xcolname,
groupcolname, grouplevel_a, grouplevel_b,
familywise_n = familywise_n
)
if (DEBUG) {
cat(paste("row", rownum, "column", colnum, "text", txt, "\n"))
}
m[rownum, colnum] <- txt
}
}
if (transpose) {
m <- t(m)
}
if (DEBUG) print(m)
DF <- data.frame(m)
rownames(DF) <- varcolnames
colnames(DF) <- varcolnames
return(DF)
}
# =============================================================================
# Sanity checks (e.g. for refereeing), such as t-tests based on mean/SD without
# access to raw data.
# =============================================================================
miscstat$t_test_unpaired_eq_var <- function(mean1, mean2, sd1, sd2, n1, n2) {
# Manual t test, for unpaired data, assuming equal variances.
df <- n1 + n2 - 2
var1 <- sd1 ^ 2
var2 <- sd2 ^ 2
pooled_var <- ((n1 - 1) * var1 + (n2 - 1) * var2) / (n1 + n2 - 2)
t <- (mean1 - mean2) / sqrt((pooled_var / n1) + (pooled_var / n2))
p <- 2 * pt(-abs(t), df) # http://www.cyclismo.org/tutorial/R/pValues.html
cat("Group 1: mean = ", mean1, ", sd = ", sd1, ", var = ", var1,
", n = ", n1, "\n", sep = "")
cat("Group 2: mean = ", mean2, ", sd = ", sd2, ", var = ", var2,
", n = ", n2, "\n", sep = "")
cat("Mean difference (mean2 - mean1) = ", mean2 - mean1, "\n", sep = "")
cat("t =", t, "\n")
cat("df =", df, "\n")
cat("p =", p, "\n")
}
miscstat$t_test_unpaired_uneq_var <- function(mean1, mean2, sd1, sd2, n1, n2) {
# Manual t test, for unpaired data, assuming unequal variances, via the
# Welch-Satterthwaite approximation. Also known as Welch's t-test.
EXAMPLE_CODE <- '
# The three examples from Wikipedia:
# - https://en.wikipedia.org/wiki/Welch%27s_t-test
# - see also https://en.wikipedia.org/wiki/Welch%E2%80%93Satterthwaite_equation
ex1_a1 = c(27.5, 21.0, 19.0, 23.6, 17.0, 17.9, 16.9, 20.1, 21.9, 22.6, 23.1, 19.6, 19.0, 21.7, 21.4)
ex1_a2 = c(27.1, 22.0, 20.8, 23.4, 23.4, 23.5, 25.8, 22.0, 24.8, 20.2, 21.9, 22.1, 22.9, 20.5, 24.4)
ex2_a1 = c(17.2, 20.9, 22.6, 18.1, 21.7, 21.4, 23.5, 24.2, 14.7, 21.8)
ex2_a2 = c(21.5, 22.8, 21.0, 23.0, 21.6, 23.6, 22.5, 20.7, 23.4, 21.8, 20.7, 21.7, 21.5, 22.5, 23.6, 21.5, 22.5, 23.5, 21.5, 21.8)
ex3_a1 = c(19.8, 20.4, 19.6, 17.8, 18.5, 18.9, 18.3, 18.9, 19.5, 22.0)
ex3_a2 = c(28.2, 26.6, 20.1, 23.3, 25.2, 22.1, 17.7, 27.6, 20.6, 13.7, 23.2, 17.5, 20.6, 18.0, 23.9, 21.6, 24.3, 20.4, 24.0, 13.2)
t_test_unpaired_uneq_var(mean1 = mean(ex1_a1), mean2 = mean(ex1_a2), sd1 = sd(ex1_a1), sd2 = sd(ex1_a2), n1 = length(ex1_a1), n2 = length(ex1_a2))
# Wikipedia: t = -2.46, v = 25.0, p = 0.021
# This code: t = -2.46, v = 25.0, p = 0.021
t_test_unpaired_uneq_var(mean1 = mean(ex2_a1), mean2 = mean(ex2_a2), sd1 = sd(ex2_a1), sd2 = sd(ex2_a2), n1 = length(ex2_a1), n2 = length(ex2_a2))
# Wikipedia: t = -1.57, v = 9.9, p = 0.149
# This code: t = -1.57, v = 9.9, p = 0.149
t_test_unpaired_uneq_var(mean1 = mean(ex3_a1), mean2 = mean(ex3_a2), sd1 = sd(ex3_a1), sd2 = sd(ex3_a2), n1 = length(ex3_a1), n2 = length(ex3_a2))
# Wikipedia: t = -2.22, v = 24.5, p = 0.036
# This code: t = -2.22, v = 24.5, p = 0.036
'
var1 <- sd1 ^ 2
var2 <- sd2 ^ 2
t <- (mean1 - mean2) / sqrt((var1 / n1) + (var2 / n2))
v1 <- n1 - 1
v2 <- n2 - 1
v <- (
((var1 / n1) + (var2 / n2)) ^ 2 /
((var1 ^ 2 / (n1 ^ 2 * v1)) + (var2 ^ 2 / (n2 ^ 2 * v2)))
) # degrees of freedom
p <- 2 * pt(-abs(t), v)
cat("Group 1: mean = ", mean1, ", sd = ", sd1, ", var = ", var1,
", n = ", n1, "\n", sep = "")
cat("Group 2: mean = ", mean2, ", sd = ", sd2, ", var = ", var2,
", n = ", n2, "\n", sep = "")
cat("Mean difference (mean2 - mean1) = ", mean2 - mean1, "\n", sep = "")
cat("t =", t, "\n")
cat("df (Welch-Satterthwaite) =", v, "\n")
cat("p =", p, "\n")
}
# =============================================================================
# p-values
# =============================================================================
miscstat$sidak_alpha <- function(familywise_alpha, n_comparisons) {
# returns corrected alpha, which gets lower with n_comparisons
1 - (1 - familywise_alpha) ^ (1 / n_comparisons)
}
miscstat$sidak_p <- function(uncorrected_p, n_comparisons) {
# returns corrected p, which gets higher with n_comparisons
# ... the problem here is equivalent to taking a corrected alpha
# and returning an uncorrected alpha, i.e. reversing the alpha
# calculation
# ... that is, sidak_p(sidak_alpha(x, n), n) == x
1 - ((1 - uncorrected_p) ^ n_comparisons)
}
miscstat$sidak_familywise_alpha <- function(alpha_per_test, n_comparisons) {
# If you apply "alpha_per_test" to each test, "n_comparisons" times, what
# is the effective overall (familywise) alpha?
1 - (1 - alpha_per_test) ^ (n_comparisons)
}
miscstat$sidak_corrected_p <- function(uncorrected_p, n_comparisons) {
# returns corrected p value
# http://v8doc.sas.com/sashtml/stat/chap43/sect14.htm
1 - (1 - uncorrected_p) ^ (n_comparisons)
}
# =============================================================================
# Goodness of fit
# =============================================================================
miscstat$aic <- function(nLL, k) {
# Akaike Information Criterion
2 * k + 2 * nLL
# = 2k - 2ln(L)
}
miscstat$nll_from_aic <- function(aic, k) {
(aic - 2 * k ) / 2
}
# Strong argument to prefer AICc over AIC:
# http://en.wikipedia.org/wiki/Akaike_information_criterion
# http://www.sortie-nd.org/lme/Statistical%20Papers/Burnham_and_Anderson_2004_Multimodel_Inference.pdf
# k = num_parameters:
miscstat$aicc <- function(nLL, k, n) {
# Akaike Information Criterion, corrected
# Parameter meanings as for BIC; see below.
aic(nLL, k) + 2 * k * (k + 1) / (n - k - 1)
}
miscstat$bic <- function(nLL, k, n) {
# Bayesian Information Criterion
# nLL = negative log-likelihood (where log likelihood = sum of ln(p) values)
# ... since p(...) <= 1, ln(p) <= 0
# ... so the LL is negative and the nLL is positive
# ... a good fit is where the p values are close to 1
# ... so the LL are negative but close to 0
# ... so the nLL values are positive but close to 0
# ... a bigger nLL is bad
# ... a bigger BIC is bad; smaller BIC means better fit (penalized for number of parameters)
# k = number of parameters in the model
# n = number of observations
2 * nLL + k * log(n)
# ... = -2 ln(L) + k ln(n)
}
miscstat$lr_test <- function(model1_nLL, model1_df, model2_nLL, model2_df) {
# Ensure df2 > df1
if (model2_df > model1_df) {
df1 <- model1_df
df2 <- model2_df
nLL1 <- model1_nLL
nLL2 <- model2_nLL
}
else {
df1 <- model2_df
df2 <- model1_df
nLL1 <- model2_nLL
nLL2 <- model1_nLL
}
# Don't work with exp(-nLL) -- numbers too small, get rounded to zero (see e.g. "exp(-3000)")
D <- 2 * nLL1 - 2 * nLL2
# D = -2 ln(likelihood for null model) + 2 ln(likelihood for alternative model)
df <- df2 - df1
p <- 1 - pchisq(D, df)
cat("df1 = ", df1, "\n")
cat("df2 = ", df2, "\n")
cat("D (= chi-square) = ", D, "\n")
cat("df = ", df, "\n")
cat("p = ", p, "\n")
return(p)
}
# =============================================================================
# Distributions
# =============================================================================
# AVOID, use PDF instead for MAP
miscstat$p_data_or_more_extreme_from_normal <- function(x, means, sds) {
ifelse(
x > means,
2 * (1 - pnorm(x, means, sds, lower.tail = TRUE) ),
2 * (1 - pnorm(x, means, sds, lower.tail = FALSE) )
)
}
# =============================================================================
# softmax function
# =============================================================================
miscstat$softmax <- function(x, b = 1, debug = TRUE) {
# x: vector of values
# b: exploration parameter, or inverse temperature [Daw2009], or 1/t where:
# t: temperature (towards infinity: all actions equally likely; towards
# zero: probability of action with highest value tends to 1)
# - DO NOT USE TEMPERATURE DIRECTLY: the optimizer may take it to zero,
# giving an infinity.
# - input vector may have NA values in
# - return value: vector of probabilities
constant <- mean(x, na.rm = TRUE)
products <- x * b - constant
# ... softmax is invariant to addition of a constant: Daw article and
# http://www.faqs.org/faqs/ai-faq/neural-nets/part2/section-12.html#b
if (max(products, na.rm = TRUE) > MAX_EXPONENT) {
if (debug) {
cat("OVERFLOW in softmax(): x =", x, ", b =", b,
", constant =", constant, ", products = ", products, "\n")
}
answer <- rep(0, length(x))
answer[which.max(x)] <- 1
answer[is.na(x)] <- NA
}
else {
exponented <- exp(products)
answer <- exponented / sum(exponented, na.rm = TRUE)
}
return(answer)
}
# =============================================================================
# proportion
# =============================================================================
miscstat$proportion_x_from_a_to_b <- function(x, a, b) {
(1 - x) * a + x * b
}
# =============================================================================
# randomness
# =============================================================================
miscstat$coin <- function(p) {
n <- length(p)
return(p > runif(n))
}
miscstat$roulette <- function(p) {
# p is a vector of probabilities that sum to 1
# return value: vector of truth values: one TRUE, the rest FALSE, selected according to the probabilities
n_options <- length(p)
cum_p <- cumsum(p)
r <- runif(1) # random variable
choice <- rep(FALSE, n_options)
choice[cum_p == min(cum_p[cum_p > r])] <- TRUE
return(choice)
}
# =============================================================================
# ANOVA/linear modelling
# =============================================================================
# -----------------------------------------------------------------------------
# Diagnostic plots
# -----------------------------------------------------------------------------
miscstat$rvfPlot <- function(model, FONTSIZE = 10) {
# https://rpubs.com/therimalaya/43190
# Note that the other diagnostic plots shown there fail with lme models.
return (
ggplot(model, aes(.fitted, .resid))
+ geom_point()
+ stat_smooth(method = "loess")
+ geom_hline(yintercept = 0, col = "red", linetype = "dashed")
+ xlab("Fitted values")
+ ylab("Residuals")
+ ggtitle("Residual vs Fitted Plot")
+ theme_classic()
+ theme(
text = element_text(size = FONTSIZE),
plot.title = element_text(hjust = 0, face = "bold") # left title
)
)
}
# -----------------------------------------------------------------------------
# Post-hoc analysis; SEDs
# -----------------------------------------------------------------------------
miscstat$pairwise_contrasts <- function(
term,
model,
alternative = c("two.sided", "less", "greater"),
debug = FALSE
)
{
# NB the "model" parameter is used in an eval() statement, below.
alternative <- match.arg(alternative)
# We'd normally do:
#
# multcomp::glht(model, linfct = multcomp::mcp(area = "Tukey"))
#
# where "area" is a factor in the model.
# But this can't do interactions, I don't think. An alternative is:
#
# multcomp::glht(model, linfct = lsmeans::lsm(pairwise ~ area)
# multcomp::glht(model, linfct = lsmeans::lsm(pairwise ~ area:treatment)
#
# Some refs:
#
# https://mailman.ucsd.edu/pipermail/ling-r-lang-l/2012-November/000393.html
# http://mindingthebrain.blogspot.co.uk/2013/04/multiple-pairwise-comparisons-for.html
# http://stats.stackexchange.com/questions/43664/mixed-model-multiple-comparisons-for-interaction-between-continuous-and-categori
# http://stats.stackexchange.com/questions/120604/which-post-hoc-is-more-valid-for-multiple-comparison-of-an-unbalanced-lmer-model
#
# However, we want the "area" or "area:treatment" thing to come in as a
# variable. This is all a bit ugly...
#
# http://adv-r.had.co.nz/Computing-on-the-language.html
# http://stackoverflow.com/questions/5542945/opposite-of-rs-deparsesubstitutevar
#
# Anyway, the answer in R is to eval it.
#
# As of 2021-05-21 and lsmmeans_2.30-0 / emmeans_1.4.8, lsmeans::lsm has
# been replaced by emmeans::lsm.
if (debug) {
cat(paste0(
"miscstat$pairwise_contrasts(): evaluating for term: ", term, "\n"
))
}
expr <- paste0(
"multcomp::glht(model, linfct = emmeans::lsm(pairwise ~ ", term, "), ",
"alternative=\"", alternative, "\")"
)
g <- eval(parse(text = expr))
summ <- summary(g)
test <- summ$test
# test includes:
# coefficients = "Estimate"
# sigma = "Std. Error" (of the difference) = SED
# tstat = "t value"
# pvalues = "Pr(>|t|)"
# ... for each of which, names() gives the tests
d <- data.frame(
term = term,
comparison = names(test$coefficients),
estimate = test$coefficients,
sed = test$sigma,
t = test$tstat,
p = test$pvalues,
alternative = alternative
)
return(d)