Description

This shows the output of useful functions for training and testing from the package rwf. The functions include plotting ROC curves, confusion matrices, separability plots, and evaluating confusion matrix performance at different cut-off points. Additionally, it demonstrates k-fold and k-sample cross-validation procedures, scaling data, and calculating confusion matrix statistics.

Installation instructions for rwf can be found here

The code can be found here

Plot Receiver Operating Characteristic ROC Curve

# Observed vector binary
observed<-round(abs(rnorm(100,m=0,sd=.5)))
# Predicted vector continuous
predicted<-abs(rnorm(100,m=0,sd=.5))
plot_roc(observed=observed,predicted=predicted)

# An example with a good prediction
df1<-data.frame(matrix(.999,ncol=2,nrow=2))
correlation_martix<-as.matrix(df1)
diag(correlation_martix)<-1
df1<-generate_correlation_matrix(correlation_martix,nrows=1000)
df1$X1<-ifelse(abs(df1$X1)<1,0,1)
df1$X2<-abs(df1$X2)
df1$X2<-(df1$X2-min(df1$X2))/(max(df1$X2)-min(df1$X2))
plot_roc(observed=round(abs(df1$X1),0),predicted=abs(df1$X2))

Plot Confusion Matrix

plot_confusion(observed=c(1,2,3,1,2,3),predicted=c(1,2,3,1,2,3))

observed<-c(rep("male",10),rep("female",10),"male","male")
predicted<-c(rep("male",10),rep("female",10),"female","female")
plot_confusion(observed=observed,predicted=predicted)

Plot Separability

This function generates a separability plot using ggplot2. It shows the density distribution of predicted probabilities for different observed categories. The plot helps to visualize how well the predicted probabilities separate the different observed categories.

df1<-data.frame(matrix(.999,ncol=2,nrow=2))
correlation_martix<-as.matrix(df1)
diag(correlation_martix)<-1
df1<-generate_correlation_matrix(correlation_martix,nrows=1000)
df1$X1<-ifelse(abs(df1$X1)<1,0,1)
df1$X2<-abs(df1$X2)
df1$X2<-(df1$X2-min(df1$X2))/(max(df1$X2)-min(df1$X2))
plot_separability(observed=round(abs(df1$X1),0),predicted=abs(df1$X2))

Plot Confusion Matrix Performance

This function evaluates the performance of a confusion matrix at different cut-off points. It iterates through a range of cut-off points, calculates the confusion matrix,and evaluates the proportion of correct classifications for each cut-off.

df<-data.frame(matrix(.999,ncol=2,nrow=2))
correlation_martix<-as.matrix(df)
diag(correlation_martix)<-1
df<-generate_correlation_matrix(correlation_martix,nrows=1000)
df$X1<-ifelse(abs(df$X1)<1,0,1)
df$X2<-abs(df$X2)
df$X2<-(df$X2-min(df$X2))/(max(df$X2)-min(df$X2))
result_confusion_performance(observed=round(abs(df$X1),0),
                             predicted=abs(df$X2),
                             step=.01)
## $plot_performance
## 
## $cut_performance
##     cut_point Overall Collumn_Observed.1 Collumn_Observed.2 Row_Predicted.1 Row_Predicted.2 Mean_proportion
## 1        0.00    0.34               1.00               0.34            0.00            1.00          0.5850
## 2        0.01    0.37               1.00               0.35            0.04            1.00          0.5975
## 3        0.02    0.39               1.00               0.36            0.08            1.00          0.6100
## 4        0.03    0.42               1.00               0.37            0.13            1.00          0.6250
## 5        0.04    0.45               1.00               0.38            0.16            1.00          0.6350
## 6        0.05    0.47               1.00               0.39            0.20            1.00          0.6475
## 7        0.06    0.49               1.00               0.40            0.22            1.00          0.6550
## 8        0.07    0.51               1.00               0.41            0.26            1.00          0.6675
## 9        0.08    0.54               1.00               0.42            0.30            1.00          0.6800
## 10       0.09    0.55               1.00               0.43            0.32            1.00          0.6875
## 11       0.10    0.58               1.00               0.45            0.36            1.00          0.7025
## 12       0.11    0.60               1.00               0.46            0.39            1.00          0.7125
## 13       0.12    0.62               1.00               0.47            0.42            1.00          0.7225
## 14       0.13    0.65               1.00               0.49            0.47            1.00          0.7400
## 15       0.14    0.67               1.00               0.51            0.50            1.00          0.7525
## 16       0.15    0.69               1.00               0.52            0.53            1.00          0.7625
## 17       0.16    0.70               1.00               0.54            0.55            1.00          0.7725
## 18       0.17    0.74               1.00               0.56            0.60            1.00          0.7900
## 19       0.18    0.76               1.00               0.58            0.63            1.00          0.8025
## 20       0.19    0.78               1.00               0.61            0.66            1.00          0.8175
## 21       0.20    0.79               1.00               0.62            0.69            1.00          0.8275
## 22       0.21    0.82               1.00               0.66            0.73            1.00          0.8475
## 23       0.22    0.83               1.00               0.67            0.75            1.00          0.8550
## 24       0.23    0.85               1.00               0.70            0.78            1.00          0.8700
## 25       0.24    0.87               1.00               0.72            0.80            1.00          0.8800
## 26       0.25    0.89               1.00               0.75            0.83            1.00          0.8950
## 27       0.26    0.91               1.00               0.80            0.87            1.00          0.9175
## 28       0.27    0.93               1.00               0.83            0.89            1.00          0.9300
## 29       0.28    0.94               1.00               0.86            0.92            1.00          0.9450
## 30       0.29    0.96               1.00               0.89            0.94            1.00          0.9575
## 31       0.30    0.97               0.99               0.94            0.97            0.99          0.9725
## 32       0.31    0.98               0.98               0.96            0.98            0.96          0.9700
## 33       0.32    0.97               0.96               0.98            0.99            0.93          0.9650
## 34       0.33    0.97               0.95               1.00            1.00            0.90          0.9625
## 35       0.34    0.94               0.92               1.00            1.00            0.84          0.9400
## 36       0.35    0.93               0.91               1.00            1.00            0.80          0.9275
## 37       0.36    0.92               0.89               1.00            1.00            0.75          0.9100
## 38       0.37    0.90               0.86               1.00            1.00            0.70          0.8900
## 39       0.38    0.89               0.86               1.00            1.00            0.67          0.8825
## 40       0.39    0.88               0.84               1.00            1.00            0.64          0.8700
## 41       0.40    0.86               0.83               1.00            1.00            0.60          0.8575
## 42       0.41    0.85               0.82               1.00            1.00            0.57          0.8475
## 43       0.42    0.84               0.80               1.00            1.00            0.52          0.8300
## 44       0.43    0.82               0.79               1.00            1.00            0.48          0.8175
## 45       0.44    0.81               0.77               1.00            1.00            0.43          0.8000
## 46       0.45    0.80               0.77               1.00            1.00            0.41          0.7950
## 47       0.46    0.79               0.76               1.00            1.00            0.39          0.7875
## 48       0.47    0.78               0.75               1.00            1.00            0.36          0.7775
## 49       0.48    0.78               0.75               1.00            1.00            0.34          0.7725
## 50       0.49    0.77               0.74               1.00            1.00            0.31          0.7625
## 51       0.50    0.76               0.73               1.00            1.00            0.28          0.7525
## 52       0.51    0.75               0.73               1.00            1.00            0.27          0.7500
## 53       0.52    0.74               0.72               1.00            1.00            0.23          0.7375
## 54       0.53    0.73               0.71               1.00            1.00            0.21          0.7300
## 55       0.54    0.72               0.71               1.00            1.00            0.19          0.7250
## 56       0.55    0.72               0.70               1.00            1.00            0.16          0.7150
## 57       0.56    0.71               0.69               1.00            1.00            0.15          0.7100
## 58       0.57    0.70               0.69               1.00            1.00            0.13          0.7050
## 59       0.58    0.70               0.69               1.00            1.00            0.13          0.7050
## 60       0.59    0.70               0.69               1.00            1.00            0.12          0.7025
## 61       0.60    0.70               0.69               1.00            1.00            0.11          0.7000
## 62       0.61    0.70               0.68               1.00            1.00            0.11          0.6975
## 63       0.62    0.69               0.68               1.00            1.00            0.09          0.6925
## 64       0.63    0.68               0.68               1.00            1.00            0.07          0.6875
## 65       0.64    0.68               0.68               1.00            1.00            0.07          0.6875
## 66       0.65    0.68               0.67               1.00            1.00            0.06          0.6825
## 67       0.66    0.68               0.67               1.00            1.00            0.05          0.6800
## 68       0.67    0.68               0.67               1.00            1.00            0.05          0.6800
## 69       0.68    0.67               0.67               1.00            1.00            0.04          0.6775
## 70       0.69    0.67               0.67               1.00            1.00            0.04          0.6775
## 71       0.70    0.67               0.67               1.00            1.00            0.04          0.6775
## 72       0.71    0.67               0.67               1.00            1.00            0.03          0.6750
## 73       0.72    0.67               0.66               1.00            1.00            0.03          0.6725
## 74       0.73    0.67               0.66               1.00            1.00            0.03          0.6725
## 75       0.74    0.67               0.66               1.00            1.00            0.02          0.6700
## 76       0.75    0.67               0.66               1.00            1.00            0.02          0.6700
## 77       0.76    0.67               0.66               1.00            1.00            0.02          0.6700
## 78       0.77    0.67               0.66               1.00            1.00            0.02          0.6700
## 79       0.78    0.66               0.66               1.00            1.00            0.02          0.6700
## 80       0.79    0.66               0.66               1.00            1.00            0.02          0.6700
## 81       0.80    0.66               0.66               1.00            1.00            0.01          0.6675
## 82       0.81    0.66               0.66               1.00            1.00            0.01          0.6675
## 83       0.82    0.66               0.66               1.00            1.00            0.01          0.6675
## 84       0.83    0.66               0.66               1.00            1.00            0.01          0.6675
## 85       0.84    0.66               0.66               1.00            1.00            0.01          0.6675
## 86       0.85    0.66               0.66               1.00            1.00            0.01          0.6675
## 87       0.86    0.66               0.66               1.00            1.00            0.01          0.6675
## 88       0.87    0.66               0.66               1.00            1.00            0.00          0.6650
## 89       0.88    0.66               0.66               1.00            1.00            0.00          0.6650
## 90       0.89    0.66               0.66               1.00            1.00            0.00          0.6650
## 91       0.90    0.66               0.66               1.00            1.00            0.00          0.6650
## 92       0.91    0.66               0.66               1.00            1.00            0.00          0.6650
## 93       0.92    0.66               0.66               1.00            1.00            0.00          0.6650
## 94       0.93    0.66               0.66               1.00            1.00            0.00          0.6650
## 95       0.94    0.66               0.66               1.00            1.00            0.00          0.6650
## 96       0.95    0.66               0.66               1.00            1.00            0.00          0.6650
## 97       0.96    0.66               0.66               1.00            1.00            0.00          0.6650
## 98       0.97    0.66               0.66               1.00            1.00            0.00          0.6650
## 99       0.98    0.66               0.66               1.00            1.00            0.00          0.6650
## 100      0.99    0.66               0.66               1.00            1.00            0.00          0.6650
## 101      1.00    0.66               0.66               0.00            1.00            0.00          0.4150
## 
## $cut
## [1] 0.3
## 
## $confusion_matrix
##          0      1     sum    p
## 0   636.00   5.00  641.00 0.99
## 1    23.00 336.00  359.00 0.94
## sum 659.00 341.00 1000.00 1.00
## p     0.97   0.99    1.00 0.97
result_confusion_performance(observed=c(1,2,3,1,2,3),predicted=abs(rnorm(6,0,sd=.1)))
## $plot_performance
## 
## $cut_performance
##    cut_point Overall Collumn_Observed.1 Collumn_Observed.2 Collumn_Observed.3 Collumn_Observed.4 Row_Predicted.1 Row_Predicted.2 Row_Predicted.3 Row_Predicted.4 Mean_proportion
## 1 0.01309298    0.33                  0               0.40                  0                  0               0               1               0               0         0.17500
## 2 0.11309298    0.33                  0               0.67                  0                  0               0               1               0               0         0.20875
## 
## $cut
## [1] 0.113093
## 
## $confusion_matrix
##        0    1    2    3  sum    p
## 0   0.00 0.00 1.00 2.00 3.00 0.00
## 1   0.00 2.00 1.00 0.00 3.00 0.67
## 2   0.00 0.00 0.00 0.00 0.00 0.00
## 3   0.00 0.00 0.00 0.00 0.00 0.00
## sum 0.00 2.00 2.00 2.00 6.00 1.00
## p   0.00 1.00 0.00 0.00 1.00 0.33

KFOLD

Sample data for train test validation procedures

infert_formula<-as.formula(factor(case)~age+parity+education+spontaneous+induced)
result<-k_fold(infert,k=10,model_formula=infert_formula)
## Fold Cases: 1 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 2 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 3 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 4 Train: 224 Test: 24 Total: 248 Unique Train: 224 Unique Test: 24 
## Fold Cases: 5 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 6 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 7 Train: 224 Test: 24 Total: 248 Unique Train: 224 Unique Test: 24 
## Fold Cases: 8 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 9 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 10 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25
model_formula<-as.formula(mpg~cyl+disp+hp+drat+wt+qsec+vs+am+gear+carb)
result<-k_fold(mtcars,k=2,model_formula=model_formula)
## Fold Cases: 1 Train: 16 Test: 16 Total: 32 Unique Train: 16 Unique Test: 16 
## Fold Cases: 2 Train: 16 Test: 16 Total: 32 Unique Train: 16 Unique Test: 16

KSAMPLE

Sample data for train test validation procedures

infert_formula<-as.formula(factor(case)~age+parity+education+spontaneous+induced)
result<-k_sample(df=infert,k=10,model_formula=infert_formula)
## Fold Cases: 1 Train: 12 Test: 6 Validation: 6 Total: 25 Unique Train: 12 Unique Test: 6 Unique Validation: 7 
## Fold Cases: 2 Train: 12 Test: 6 Validation: 6 Total: 25 Unique Train: 12 Unique Test: 6 Unique Validation: 7 
## Fold Cases: 3 Train: 12 Test: 6 Validation: 6 Total: 25 Unique Train: 12 Unique Test: 6 Unique Validation: 7 
## Fold Cases: 4 Train: 12 Test: 6 Validation: 6 Total: 24 Unique Train: 12 Unique Test: 6 Unique Validation: 6 
## Fold Cases: 5 Train: 12 Test: 6 Validation: 6 Total: 25 Unique Train: 12 Unique Test: 6 Unique Validation: 7 
## Fold Cases: 6 Train: 12 Test: 6 Validation: 6 Total: 25 Unique Train: 12 Unique Test: 6 Unique Validation: 7 
## Fold Cases: 7 Train: 12 Test: 6 Validation: 6 Total: 24 Unique Train: 12 Unique Test: 6 Unique Validation: 6 
## Fold Cases: 8 Train: 12 Test: 6 Validation: 6 Total: 25 Unique Train: 12 Unique Test: 6 Unique Validation: 7 
## Fold Cases: 9 Train: 12 Test: 6 Validation: 6 Total: 25 Unique Train: 12 Unique Test: 6 Unique Validation: 7 
## Fold Cases: 10 Train: 12 Test: 6 Validation: 6 Total: 25 Unique Train: 12 Unique Test: 6 Unique Validation: 7
model_formula<-as.formula(mpg~cyl+disp+hp+drat+wt+qsec+vs+am+gear+carb)
result<-k_sample(df=mtcars,k=10,model_formula=model_formula)
## Fold Cases: 1 Train: 2 Test: 1 Validation: 1 Total: 4 Unique Train: 2 Unique Test: 1 Unique Validation: 1 
## Fold Cases: 2 Train: 1 Test: 1 Validation: 1 Total: 3 Unique Train: 1 Unique Test: 1 Unique Validation: 1 
## Fold Cases: 3 Train: 1 Test: 1 Validation: 1 Total: 3 Unique Train: 1 Unique Test: 1 Unique Validation: 1 
## Fold Cases: 4 Train: 1 Test: 1 Validation: 1 Total: 3 Unique Train: 1 Unique Test: 1 Unique Validation: 1 
## Fold Cases: 5 Train: 1 Test: 1 Validation: 1 Total: 3 Unique Train: 1 Unique Test: 1 Unique Validation: 1 
## Fold Cases: 6 Train: 1 Test: 1 Validation: 1 Total: 3 Unique Train: 1 Unique Test: 1 Unique Validation: 1 
## Fold Cases: 7 Train: 1 Test: 1 Validation: 1 Total: 3 Unique Train: 1 Unique Test: 1 Unique Validation: 1 
## Fold Cases: 8 Train: 1 Test: 1 Validation: 1 Total: 3 Unique Train: 1 Unique Test: 1 Unique Validation: 1 
## Fold Cases: 9 Train: 1 Test: 1 Validation: 1 Total: 3 Unique Train: 1 Unique Test: 1 Unique Validation: 1 
## Fold Cases: 10 Train: 2 Test: 1 Validation: 1 Total: 4 Unique Train: 2 Unique Test: 1 Unique Validation: 1

Scaling

Scale all data between 0 and 1

result<-recode_scale_dummy(infert)
head(result)
##   X6.11yrs X12..yrs X0.5yrs       age parity induced case spontaneous    stratum pooled.stratum
## 1        0        0       1 0.2173913    1.0     0.5    1         1.0 0.00000000     0.03225806
## 2        0        0       1 0.9130435    0.0     0.5    1         0.0 0.01219512     0.00000000
## 3        0        0       1 0.7826087    1.0     1.0    1         0.0 0.02439024     0.04838710
## 4        0        0       1 0.5652174    0.6     1.0    1         0.0 0.03658537     0.01612903
## 5        1        0       0 0.6086957    0.4     0.5    1         0.5 0.04878049     0.50000000
## 6        1        0       0 0.6521739    0.6     1.0    1         0.5 0.06097561     0.56451613

Confusion Matrix

confusion(observed=c(1,2,3,4,5,10),predicted=c(1,2,3,4,5,11))
##          observed
## predicted 1 2 3 4 5 10 11
##        1  1 0 0 0 0  0  0
##        2  0 1 0 0 0  0  0
##        3  0 0 1 0 0  0  0
##        4  0 0 0 1 0  0  0
##        5  0 0 0 0 1  0  0
##        10 0 0 0 0 0  0  0
##        11 0 0 0 0 0  1  0
confusion(observed=c(1,2,2,2,2),predicted=c(1,1,2,2,2))
##          observed
## predicted 1 2
##         1 1 1
##         2 0 3

Proportion Accutate

proportion_accurate(observed=c(1,2,3,4,5,10),predicted=c(1,2,3,4,5,11))
##   cm_diagonal cm_off_diagonal kappa_unweighted kappa_linear kappa_squared
## 1   0.8333333               1        0.8064516    0.9285714     0.9801325

Confusion Matrix Percent

observed<-factor(round(rnorm(10000,m=10,sd=1)))
predicted<-factor(round(rnorm(10000,m=10,sd=1)))
confusion_matrix_percent(observed,predicted)
##        6     7      8       9      10      11     12    13      sum    p
## 6   0.00  0.00   0.00    0.00    0.00    0.00   0.00  0.00     0.00 0.00
## 7   0.00  0.00   9.00   13.00   14.00    9.00   0.00  0.00    45.00 0.00
## 8   1.00  1.00  31.00  134.00  244.00  138.00  52.00  7.00   608.00 0.05
## 9   0.00 15.00 139.00  543.00  879.00  635.00 144.00 10.00  2365.00 0.23
## 10  0.00 20.00 249.00  915.00 1491.00  944.00 243.00 26.00  3888.00 0.38
## 11  0.00 16.00 157.00  584.00  904.00  602.00 141.00 16.00  2420.00 0.25
## 12  0.00  4.00  34.00  159.00  230.00  155.00  35.00  5.00   622.00 0.06
## 13  0.00  0.00   5.00   13.00   16.00   16.00   2.00  0.00    52.00 0.00
## sum 1.00 56.00 624.00 2361.00 3778.00 2499.00 617.00 64.00 10000.00 1.00
## p   0.00  0.00   0.05    0.23    0.39    0.24   0.06  0.00     1.00 0.27

Confusion Matrix Percent

plot_confusion_extended(observed=c(0,0,0,1,1,1),predicted=c(0,0,0,1,1,1))