-
Notifications
You must be signed in to change notification settings - Fork 23
/
helper.R
1300 lines (901 loc) · 33.3 KB
/
helper.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
# helper.R:
# Auxiliary/utility functions.
# --------------------------
# (1) Basic checks and calculations: ------
# valid_train_test_data: ------
# Goal: Ensure that train and test data are sufficiently similar (e.g., contain the same variables)
# and provide feedback on any existing differences.
#
# Currently, it is only verified that both DFs have some cases and
# contain the SAME names (but the content or order of variables is not checked or altered).
# Future versions may want to verify that 'test' data is valid, given current 'train' data
# (e.g., "test" contains all required variables of "train" to create the current FFTs).
#
# Output: Boolean.
valid_train_test_data <- function(train_data, test_data){
# Initialize:
valid <- FALSE
train_names <- names(train_data)
test_names <- names(test_data)
train_names_not_in_test <- setdiff(train_names, test_names)
test_names_not_in_train <- setdiff(test_names, train_names)
# Conditions:
if (nrow(train_data) < 1){
msg <- paste("The 'train' data contains no cases (rows).")
warning(msg)
} else if (nrow(test_data) < 1){
msg <- paste("The 'test' data contains no cases (rows).")
warning(msg)
} else if (length(train_names_not_in_test) > 0){
msg <- paste("Some variables occur in 'train' data, but not in 'test' data:",
paste(train_names_not_in_test, collapse = ", "))
warning(msg)
} else if (length(test_names_not_in_train) > 0){
msg <- paste("Some variables occur in 'test' data, but not in 'train' data:",
paste(test_names_not_in_train, collapse = ", "))
warning(msg)
same_names <- FALSE
} else { # all tests passed:
valid <- TRUE
}
# Output:
return(valid)
} # valid_train_test_data().
# # Check:
# (df1 <- data.frame(matrix( 1:9, nrow = 3)))
# (df2 <- data.frame(matrix(11:22, ncol = 3)))
# (df3 <- data.frame(matrix(31:45, nrow = 3)))
# (df0 <- df1[-(1:3), ])
#
# # FALSE cases:
# valid_train_test_data(df0, df1)
# valid_train_test_data(df1, df0)
# valid_train_test_data(df1, df3)
# valid_train_test_data(df3, df1)
# # TRUE cases:
# valid_train_test_data(df1, df2)
# valid_train_test_data(df1, df2[ , 3:1])
# select_best_tree: ------
#' Select the best tree (from the current set)
#'
#' \code{select_best_tree} selects (looks up and identifies) the best tree
#' from the set (or \dQuote{fan}) of FFTs contained in the current \code{FFTrees} object \code{x},
#' an existing type of \code{data} ('train' or 'test'), and
#' a \code{goal} for which corresponding statistics are available
#' in the designated \code{data} type (in \code{x$trees$stats}).
#'
#' Importantly, \code{select_best_tree} only identifies and selects from the set of
#' \emph{existing} trees with known statistics,
#' rather than creating new trees or computing new cue thresholds.
#' More specifically, \code{goal} is used for identifying and selecting the best of
#' an existing set of FFTs, but not for
#' computing new cue thresholds (see \code{goal.threshold} and \code{fftrees_cuerank()}) or
#' creating new trees (see \code{goal.chase} and \code{fftrees_ranktrees()}).
#'
#' @param x An \code{FFTrees} object.
#'
#' @param data character. Must be either "train" or "test".
#'
#' @param goal character. A goal to maximize or minimize when selecting a tree from an existing \code{x}
#' (for which values exist in \code{x$trees$stats}).
#'
#' @return An integer denoting the \code{tree} that maximizes/minimizes \code{goal} in \code{data}.
#'
#' @seealso
#' \code{\link{FFTrees}} for creating FFTs from and applying them to data.
select_best_tree <- function(x, data, goal){
# Verify inputs: ------
# x: ----
testthat::expect_true(inherits(x, "FFTrees"),
info = "Argument x is no FFTrees object")
# data: ----
testthat::expect_true(data %in% c("train", "test"))
if (is.null(x$trees$stats$test) & (data == "test")){
message("You asked for 'test' data, but x only contains training statistics. I'll use data = 'train' instead...")
data <- "train"
}
# goal: ----
# # (a) narrow goal range:
#
# goal_valid <- c("acc", "bacc", "wacc", "dprime", "cost") # ToDo: Is "dprime" being computed?
# testthat::expect_true(goal %in% goal_valid)
# (b) wide goal range:
# Goals to maximize (more is better):
max_goals <- c("hi", "cr",
"sens", "spec",
"ppv", "npv",
"acc", "bacc", "wacc", "dprime",
"pci")
# Goals to minimize (less is better):
min_goals <- c("mi", "fa",
"cost", "cost_decisions", "cost_cues",
"mcu")
goal_valid <- c(max_goals, min_goals)
testthat::expect_true(goal %in% goal_valid)
# Get tree stats (from x given data): ------
cur_stats <- x$trees$stats[[data]]
cur_names <- names(cur_stats)
ix_goal <- which(cur_names == goal)
cur_goal_vals <- as.vector(cur_stats[[ix_goal]])
if (goal %in% max_goals){ # more is better:
cur_ranks <- rank(-cur_goal_vals, ties.method = "first") # low ranks indicate higher/better values
} else { # goal %in% min_goals / less is better:
cur_ranks <- rank(+cur_goal_vals, ties.method = "first") # low rank indicate lower/better values
}
tree <- cur_stats$tree[cur_ranks == min(cur_ranks)] # tree with minimum rank
# Output: -----
testthat::expect_true(is.integer(tree)) # verify output
return(tree) # as number
} # select_best_tree().
# apply_break: ------
# Takes a direction, threshold value, and cue vector, and returns a vector of decisions.
apply_break <- function(direction,
threshold.val,
cue.v,
cue.class) {
testthat::expect_true(direction %in% c("!=", "=", "<", "<=", ">", ">="))
testthat::expect_length(threshold.val, 1)
# direction = cue_direction_new
# threshold.val = cue_threshold_new
# cue.v = data_current[[cues_name_new]]
# cue.class = cue_class_new
if (is.character(threshold.val)) {
threshold.val <- unlist(strsplit(threshold.val, ","))
}
if (cue.class %in% c("numeric", "integer")) {
threshold.val <- as.numeric(threshold.val)
}
if (direction == "!=") {
output <- (cue.v %in% threshold.val) == FALSE
}
if (direction == "=") {
output <- cue.v %in% threshold.val
}
if (direction == "<") {
output <- cue.v < threshold.val
}
if (direction == "<=") {
output <- cue.v <= threshold.val
}
if (direction == ">") {
output <- cue.v > threshold.val
}
if (direction == ">=") {
output <- cue.v >= threshold.val
}
return(output)
} # apply_break().
# cost_cues_append: ------
# Create cost.cues:
cost_cues_append <- function(formula,
data,
cost.cues = NULL) {
criterion_name <- paste(formula)[2]
data_mf <- model.frame(
formula = formula,
data = data
)
cue_df <- data_mf[, 2:ncol(data_mf), drop = FALSE]
cue_name_v <- names(cue_df)
if (is.null(cost.cues) == FALSE) {
# Make sure all named cues in cost.cues are in data:
{
cue.not.in.data <- sapply(names(cost.cues), FUN = function(x) {
x %in% cue_name_v == FALSE
})
if (any(cue.not.in.data)) {
missing.cues <- paste(cost.cues[cue.not.in.data, 1], collapse = ",")
warning(paste0("The cue(s) {", missing.cues, "} specified in cost.cues are not present in the data."))
}
}
# Add any missing cue costs as 0:
{
cost.cues.o <- cost.cues
cost.cues <- lapply(1:ncol(cue_df), FUN = function(x) {
0
})
names(cost.cues) <- names(cue_df)
for (i in 1:length(cost.cues)) {
cue_name_i <- names(cost.cues)[i]
if (names(cost.cues)[i] %in% names(cost.cues.o)) {
cost.cues[[i]] <- cost.cues.o[[cue_name_i]]
}
}
}
}
if (is.null(cost.cues)) {
cost.cues <- lapply(1:ncol(cue_df), FUN = function(x) {
0
})
names(cost.cues) <- names(cue_df)
}
return(cost.cues)
} # cost_cues_append().
# comp_pred: ------
#' A wrapper for competing classification algorithms.
#'
#' \code{comp_pred} provides the main wrapper for running alternative classification algorithms, such as CART (\code{rpart::rpart}),
#' logistic regression (\code{glm}), support vector machines (\code{svm::svm}), and random forests (\code{randomForest::randomForest}).
#'
#' @param formula A formula (usually \code{x$formula}, for an \code{FFTrees} object \code{x}).
#' @param data.train A training dataset (as data frame).
#' @param data.test A testing dataset (as data frame).
#'
#' @param algorithm character string. An algorithm in the set:
#' "lr" -- logistic regression;
#' "rlr" -- regularized logistic regression;
#' "cart" -- decision trees;
#' "svm" -- support vector machines;
#' "rf" -- random forests.
#'
#' @param model model. An optional existing model, applied to the test data.
#' @param sens.w Sensitivity weight parameter (from 0 to 1, required to compute \code{wacc}).
#' @param new.factors string. What should be done if new factor values are discovered in the test set?
#' "exclude" = exclude (i.e.; remove these cases), "base" = predict the base rate of the criterion.
#'
#' @importFrom dplyr bind_rows
#' @importFrom stats model.frame formula glm model.matrix
#' @importFrom e1071 svm
#' @importFrom rpart rpart
#' @importFrom randomForest randomForest
comp_pred <- function(formula,
data.train,
data.test = NULL,
algorithm = NULL,
model = NULL,
sens.w = NULL,
new.factors = "exclude") {
# formula = x$formula
# data.train = x$data$train
# data.test = x$data$test
# algorithm = "lr"
# model = NULL
if (is.null(formula)) {
stop("You must enter a valid formula")
}
if (is.null(algorithm)) {
stop("You must specify one of the following models: 'rlr', 'lr', 'cart', 'svm', 'rf'")
# ToDo: 'rlr' does currently not seem to be implemented.
}
# SETUP: ----
{
if (is.null(data.test) & (is.null(data.train) == FALSE)) {
data.all <- data.train
train.cases <- 1:nrow(data.train)
test.cases <- c()
}
if (is.null(data.test) == FALSE & (is.null(data.train) == FALSE)) {
# data.all <- rbind(data.train, data.test) # Note: fails when both dfs have different variables!
data.all <- dplyr::bind_rows(data.train, data.test) # fills any non-matching columns with NAs.
train.cases <- 1:nrow(data.train)
test.cases <- (nrow(data.train) + 1):nrow(data.all)
}
if (is.null(data.train) & is.null(data.test) == FALSE) {
data.all <- data.test
train.cases <- c()
test.cases <- 1:nrow(data.all)
}
data.all <- model.frame(
formula = formula,
data = data.all
)
train.crit <- data.all[train.cases, 1]
# Remove columns with no variance in training data:
if (is.null(data.train) == FALSE) {
if (isTRUE(all.equal(length(unique(data.all[train.cases, 1])), 1))) {
do.test <- FALSE
} else {
do.test <- TRUE
}
} else {
do.test <- TRUE
}
}
if (do.test) {
if (is.null(train.cases) == FALSE) {
ok.cols <- sapply(1:ncol(data.all), FUN = function(x) {
length(unique(data.all[train.cases, x])) > 1
})
data.all <- data.all[, ok.cols]
}
# Convert character columns to factors:
for (col.i in 1:ncol(data.all)) {
if (inherits(data.all[,col.i], c("logical", "character", "factor"))) {
data.all[, col.i] <- factor(data.all[, col.i])
}
}
}
# Get data, cue, crit objects: ----
if (is.null(train.cases) == FALSE) {
data.train <- data.all[train.cases, ]
cue.train <- data.all[train.cases, -1]
crit.train <- data.all[train.cases, 1]
} else {
data.train <- NULL
cue.train <- NULL
crit.train <- NULL
}
# Build models and get training data: ------
# LR: ----
if (algorithm == "lr") {
if (is.null(model)) {
# Create new LR model:
train.mod <- suppressWarnings(glm(formula, data.train, family = "binomial"))
} else {
train.mod <- model
}
if (is.null(data.train) == FALSE) {
pred.train <- suppressWarnings(round(1 / (1 + exp(-predict(train.mod, data.train))), 0))
} else {
pred.train <- NULL
}
}
# RLR: ToDo ----
# SVM: ----
if (algorithm == "svm") {
if (is.null(model)) {
# Create new SVM model:
train.mod <- e1071::svm(formula,
data = data.train, type = "C"
)
} else {
train.mod <- model
}
if (is.null(data.train) == FALSE) {
pred.train <- predict(train.mod,
data = data.train
)
} else {
pred.train <- NULL
}
}
# CART: ----
if (algorithm == "cart") {
if (is.null(model)) {
# Create new CART model:
train.mod <- rpart::rpart(formula,
data = data.train,
method = "class"
)
} else {
train.mod <- model
}
if (is.null(data.train) == FALSE) {
pred.train <- predict(train.mod,
data.train,
type = "class"
)
} else {
pred.train <- NULL
}
}
# RF: ----
if (algorithm == "rf") {
if (is.null(model)) {
# Create new RF model:
data.train[, 1] <- factor(data.train[, 1])
train.mod <- randomForest::randomForest(formula,
data = data.train
)
} else {
train.mod <- model
}
if (is.null(data.train) == FALSE) {
# Get training decisions:
pred.train <- predict(
train.mod,
data.train
)
} else {
pred.train <- NULL
}
}
# Get testing data: ------
pred.test <- NULL
if (is.null(data.test) == FALSE) {
data.test <- data.all[test.cases, ]
cue.test <- data.all[test.cases, -1]
crit.test <- data.all[test.cases, 1]
# Check for new factor values:
{
if (is.null(train.cases) == FALSE) {
factor.ls <- lapply(1:ncol(data.train), FUN = function(x) {
unique(data.train[, x])
})
} else {
factor.ls <- lapply(1:ncol(data.test), FUN = function(x) {
cue.x <- names(data.test)[x]
if (cue.x %in% names(model$xlevels)) {
return(model$xlevels[[cue.x]])
} else {
return(unique(data.test[, x]))
}
})
}
cannot.pred.mtx <- matrix(0, nrow = nrow(data.test), ncol = ncol(data.test))
for (i in 1:ncol(cannot.pred.mtx)) {
if (inherits(data.test[, i], c("factor", "character"))) {
cannot.pred.mtx[, i] <- data.test[, i] %in% factor.ls[[i]] == FALSE
}
} # for().
cannot.pred.v <- rowSums(cannot.pred.mtx) > 0
if (any(cannot.pred.v)) {
if (substr(new.factors, 1, 1) == "e") {
warning(paste(sum(cannot.pred.v), "cases in the test data could not be predicted by 'e' due to new factor values. These cases will be excluded"))
data.test <- data.test[cannot.pred.v == FALSE, ]
cue.test <- cue.test[cannot.pred.v == FALSE, ]
crit.test <- crit.test[cannot.pred.v == FALSE]
}
if (substr(new.factors, 1, 1) == "b") {
warning(paste(sum(cannot.pred.v), "cases in the test data could not be predicted by 'b' due to new factor values. They will be predicted to be", mean(train.crit) > .5))
}
}
}
# Get predictions (pred.test) from each model: ------
# LR: ----
if (algorithm == "lr") {
if (is.null(data.test) == FALSE) {
pred.test <- rep(0, nrow(data.test))
if (any(cannot.pred.v) & substr(new.factors, 1, 1) == "b") {
pred.test[cannot.pred.v] <- mean(train.crit) > .5
pred.test[cannot.pred.v == FALSE] <- round(1 / (1 + exp(-predict(train.mod, data.test[cannot.pred.v == FALSE, ]))), 0)
} else {
pred.test[!cannot.pred.v] <- round(1 / (1 + exp(-predict(train.mod, data.test[cannot.pred.v == FALSE, ]))), 0)
}
}
}
# RLR: ToDo ----
# SVM: ----
if (algorithm == "svm") {
if (is.null(data.test) == FALSE) {
# See if we can do any predictions
try.pred <- try(predict(train.mod, data.test), silent = TRUE)
if (inherits(try.pred, "try-error")) {
warning("svm crashed predicting new data. That's all I can say")
pred.test <- NULL
} else {
pred.test <- predict(train.mod, data.test)
}
}
}
# CART: ----
if (algorithm == "cart") {
if (is.null(data.test) == FALSE) {
if (any(cannot.pred.v) & substr(new.factors, 1, 1) == "b") {
pred.test <- rep(0, nrow(data.test))
pred.test[cannot.pred.v] <- mean(train.crit) > .5
pred.test[cannot.pred.v == FALSE] <- predict(train.mod, data.test[cannot.pred.v == FALSE, ], type = "class")
} else {
pred.test <- predict(train.mod, data.test[cannot.pred.v == FALSE, ], type = "class")
}
}
}
# RF: ----
if (algorithm == "rf") {
if (is.null(data.test) == FALSE) {
crit.test <- as.factor(crit.test)
# Get levels of training criterion
train.crit.levels <- levels(train.mod$y)
# convert test crit to factor
crit.name <- paste(formula)[2]
data.test.2 <- data.test
data.test.2[crit.name] <- factor(data.test.2[[crit.name]], levels = train.crit.levels)
# See if we can do any predictions:
try.pred <- try(predict(train.mod, data.test.2), silent = TRUE)
if (inherits(try.pred, "try-error")) {
warning("randomForest crashed predicting new data. That's all I can say")
pred.test <- NULL
} else {
pred.test <- predict(train.mod, data.test)
}
# if(any(cannot.pred.v) & substr(new.factors, 1, 1) == "b") {
#
# pred.test <- rep(0, nrow(data.test))
# pred.test[cannot.pred.v] <- mean(train.crit) > .5
# pred.test[cannot.pred.v == FALSE] <- predict(train.mod, data.test[cannot.pred.v == FALSE,])
#
# } else {
#
# pred.test <- predict(train.mod, data.test[cannot.pred.v == FALSE,])
#
# }
}
}
}
# Convert predictions to logical if necessary: ----
if (is.null(pred.train) == FALSE) {
if ("TRUE" %in% paste(pred.train)) {
pred.train <- as.logical(paste(pred.train))
}
if ("1" %in% paste(pred.train)) {
pred.train <- as.logical(as.numeric(paste(pred.train)))
}
# Calculate training accuracy stats: ----
acc.train <- classtable(
prediction_v = as.logical(pred.train),
criterion_v = as.logical(crit.train),
sens.w = sens.w
)
}
if (is.null(pred.train)) {
acc.train <- classtable(
prediction_v = c(TRUE, TRUE, FALSE),
criterion_v = c(FALSE, FALSE, TRUE),
sens.w = sens.w
)
acc.train[1, ] <- NA
}
if (is.null(pred.test) == FALSE) {
if ("TRUE" %in% paste(pred.test)) {
pred.test <- as.logical(paste(pred.test))
}
if ("1" %in% paste(pred.test)) {
pred.test <- as.logical(as.numeric(paste(pred.test)))
}
acc.test <- classtable(
prediction_v = as.logical(pred.test),
criterion_v = as.logical(crit.test),
sens.w = sens.w
)
} else {
acc.test <- classtable(
prediction_v = c(TRUE, FALSE, TRUE),
criterion_v = c(TRUE, TRUE, FALSE),
sens.w = sens.w
)
acc.test[1, ] <- NA
}
if (do.test == FALSE) {
acc.train <- classtable(
prediction_v = c(TRUE, FALSE, TRUE),
criterion_v = c(FALSE, TRUE, TRUE),
sens.w = sens.w
)
acc.train[1, ] <- NA
acc.test <- classtable(
prediction_v = c(TRUE, FALSE, TRUE),
criterion_v = c(FALSE, TRUE, TRUE),
sens.w = sens.w
)
acc.test[1, ] <- NA
}
# ORGANIZE output: ----
output <- list(
"accuracy" = list(train = acc.train, test = acc.test),
"model" = train.mod,
"algorithm" = algorithm
)
return(output)
} # comp_pred().
# fact_clean: ------
#' Clean factor variables in prediction data
#'
#' @param data.train A training dataset
#' @param data.test A testing dataset
#' @param show.warning logical
fact_clean <- function(data.train,
data.test,
show.warning = T) {
# 1. Look for new factor values in test set that are not in training set: ----
orig.vals.ls <- lapply(1:ncol(data.train), FUN = function(x) {
unique(data.train[, x])
})
# 2. can.predict.mtx: ----
can.predict.mtx <- matrix(1, nrow = nrow(data.test), ncol = ncol(data.test))
for (i in 1:ncol(can.predict.mtx)) {
test.vals.i <- data.test[, i]
if (is.numeric(test.vals.i)) {
can.predict.mtx[, i] <- 1
} else {
can.predict.mtx[, i] <- paste(test.vals.i) %in% paste(orig.vals.ls[[i]])
}
}
# 3. model.can.predict: ----
model.can.predict <- isTRUE(all.equal(rowMeans(can.predict.mtx), 1))
if (identical(mean(model.can.predict), 1) == FALSE & show.warning == TRUE) {
warning(paste(sum(model.can.predict), " out of ",
nrow(data.test), " cases (", round(sum(model.can.predict == 0) / length(model.can.predict), 2) * 100,
"%) were removed from the test dataset.",
sep = ""
))
}
# Output: ----
output <- data.test[model.can.predict, ]
return(output)
} # fact_clean().
# add_stats: ------
#' Add decision statistics to data (containing counts of a 2x2 contingency table)
#'
#' \code{add_stats} assumes the input of essential 2x2 frequency counts
#' (as a data frame \code{data} with variable names \code{"hi"}, \code{"fa"}, \code{"mi"}, and \code{"cr"})
#' and uses them to compute various decision accuracy measures.
#'
#' Providing numeric values for \code{cost.each} (as a vector) and \code{cost.outcomes} (as a named list)
#' allows computing cost information for the counts of corresponding classification decisions.
#'
#' @param data A data frame with (integer) values named \code{"hi"}, \code{"fa"}, \code{"mi"}, and \code{"cr"}.
#' @param sens.w numeric. Sensitivity weight (for computing weighted accuracy, \code{wacc}).
#' @param cost.each numeric. An optional fixed cost added to all outputs (e.g.; the cost of the cue).
#' @param cost.outcomes list. A list of length 4 named \code{"hi"}, \code{"fa"}, \code{"mi"}, \code{"cr"}, and
#' specifying the costs of a hit, false alarm, miss, and correct rejection, respectively.
#' E.g.; \code{cost.outcomes = listc("hi" = 0, "fa" = 10, "mi" = 20, "cr" = 0)} means that a
#' false alarm and miss cost 10 and 20 units, respectively, while correct decisions incur no costs.
#'
#' @return A data frame with variables of computed accuracy and cost measures (but dropping inputs).
add_stats <- function(data,
sens.w = .50,
cost.each = NULL,
cost.outcomes = list(hi = 0, fa = 1, mi = 1, cr = 0)) {
# Prepare: ----
if (is.null(cost.each)) {
cost.each <- 0
}
# Compute measures: ----
N <- with(data, (hi + cr + fa + mi))
# Sensitivity:
data$sens <- with(data, hi / (hi + mi))
# Specificity:
data$spec <- with(data, cr / (cr + fa))
# False alarm rate:
data$far <- with(data, 1 - spec)
# Positive predictive value (PPV):
data$ppv <- with(data, hi / (hi + fa))
# Negative predictive value (NPV):
data$npv <- with(data, cr / (cr + mi))
# Accuracy:
data$acc <- with(data, (hi + cr) / N)
# Balanced accuracy:
data$bacc <- with(data, (sens + spec) / 2) # = (sens * .50) + (spec * .50)
# Weighted accuracy:
data$wacc <- with(data, (sens * sens.w) + (spec * (1 - sens.w)))
# Outcome cost:
data$cost_decisions <- with(data, -1 * ((hi * cost.outcomes$hi) + (fa * cost.outcomes$fa)
+ (mi * cost.outcomes$mi) + (cr * cost.outcomes$cr))) / data$n
# Total cost:
data$cost <- data$cost_decisions - cost.each
# Output: ----
# Drop inputs and order columns (of df):
data <- data[, c("sens", "spec", "far", "ppv", "npv",
"acc", "bacc", "wacc", "cost_decisions", "cost")]
return(data)
} # add_stats().
# # Check:
# (freq <- data.frame(hi = 2, mi = 3, fa = 1, cr = 4))
# add_stats(freq)
# add_stats(freq, sens.w = 3/4, cost.each = 1,
# cost.outcomes = list(hi = 0, mi = 3, fa = 2, cr = 0))
# dim(add_stats(freq)) # 1 x 10
# classtable: ------
#' Compute classification statistics for binary prediction and criterion (e.g.; truth) vectors
#'
#' The main input are 2 logical vectors of prediction and criterion values.
#'
#' The primary confusion matrix is computed by \code{\link{confusionMatrix}} of the \strong{caret} package.
#'
#' @param prediction_v logical. A logical vector of predictions.
#' @param criterion_v logical. A logical vector of (TRUE) criterion values.
#' @param sens.w numeric. Sensitivity weight parameter (from 0 to 1, for computing \code{wacc}).
#' Default: \code{sens.w = NULL} (to enforce that actual value is being passed by the calling function).
#' @param cost.v list. An optional list of additional costs to be added to each case.
#' @param correction numeric. Correction added to all counts for calculating \code{dprime}.
#' @param cost.outcomes list. A list of length 4 with names 'hi', 'fa', 'mi', and 'cr' specifying
#' the costs of a hit, false alarm, miss, and correct rejection, respectively.
#' For instance, \code{cost.outcomes = listc("hi" = 0, "fa" = 10, "mi" = 20, "cr" = 0)} means that
#' a false alarm and miss cost 10 and 20, respectively, while correct decisions have no cost.
#' @param na_prediction_action What happens when no prediction is possible? (experimental).
#'
#' @importFrom stats qnorm
#' @importFrom caret confusionMatrix
classtable <- function(prediction_v = NULL,
criterion_v = NULL,
sens.w = NULL, # to be passed by calling function!
cost.v = NULL,
correction = .25,
cost.outcomes = list(hi = 0, fa = 1, mi = 1, cr = 0),
na_prediction_action = "ignore") {
# prediction_v <- sample(c(TRUE, FALSE), size = 20, replace = TRUE)
# criterion_v <- sample(c(TRUE, FALSE), size = 20, replace = TRUE)
# sens.w = .50
# cost.v = NULL
# correction = .25
# cost.outcomes = list(hi = 0, fa = 1, mi = 1, cr = 0)
if (is.null(cost.v)) {
cost.v <- rep(0, length(prediction_v))
}
if (any(c("FALSE", "TRUE") %in% paste(prediction_v))) {
prediction_v <- as.logical(paste(prediction_v))
}
if (any(c("FALSE", "TRUE") %in% paste(criterion_v))) {