forked from ortont/iak3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cubist2XIAK3D.R
1280 lines (1054 loc) · 48.9 KB
/
cubist2XIAK3D.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
cubist2X <- function(cubistModel , dataFit , zFit = NULL , profIDFit = NULL , allKnotsd = c() , removeColinCols = TRUE , refineCubistModel = FALSE){
### removeColinCols and refineCubistModel are used in the cubist2XSetup function.
if(length(allKnotsd) == 0){
incdSpline <- FALSE
}else if(length(allKnotsd) >= 2){
incdSpline <- TRUE
}else{
stop('Error - if entering knots, then enter at least 2 knots (2 boundary knots plus any internal knots)')
}
if(is.null(cubistModel$listRules) || (is.null(cubistModel$allKnotsd) & (length(allKnotsd) > 0))){
tmp <- cubist2XSetup(cubistModel = cubistModel , dataFit = dataFit , zFit = zFit , profIDFit = profIDFit , allKnotsd = allKnotsd , removeColinCols = removeColinCols , refineCubistModel = refineCubistModel)
X <- tmp$X
matRuleData <- tmp$matRuleData
cubistModel <- tmp$cubistModel
}else{
### check order of columns...if different order, then resort...
if((length(names(dataFit)) != length(cubistModel$namesDataFit)) || any(names(dataFit) != cubistModel$namesDataFit)){
iOrdered <- NA * integer(length(cubistModel$namesDataFit))
for (i in 1:length(cubistModel$namesDataFit)){
iOrdered[i] <- which(names(dataFit) == cubistModel$namesDataFit[i])
}
dataFit <- dataFit[,iOrdered,drop=FALSE]
}else{}
tmp <- cubist2XGivenSetup(cubistModel , dataFit)
X <- tmp$X
matRuleData <- tmp$matRuleData
}
iNA <- which(rowSums(is.na(X) | is.nan(X)) > 0)
if(length(iNA) > 0){
X[iNA,] <- NA
}else{}
return(list('X' = X , 'matRuleData' = matRuleData , 'cubistModel' = cubistModel))
}
cubist2XGivenSetup <- function(cubistModel , dataFit){
nRules <- nrow(cubistModel$coefficients)
n <- nrow(dataFit)
#################################################
### get the total number of linear parameters...
#################################################
p <- length(cubistModel$names4XCubist)
X <- matrix(0 , n , p)
colnames(X) <- cubistModel$names4XCubist
matRuleData <- matrix(0 , n , nRules)
for (iR in 1:nRules){
if(nRules > 1){
nVThis <- nrow(cubistModel$listRules[[iR]])
### get all the data that fall into rule iR...
for (jS in 1:nVThis){
dirThis <- cubistModel$listRules[[iR]]$dir[jS]
valThis <- cubistModel$listRules[[iR]]$valUpdated[jS]
ivariableThis <- cubistModel$listRules[[iR]]$ivariable[jS]
if (dirThis == '<='){
iThisIneq <- which(dataFit[,ivariableThis] <= valThis)
}else if (dirThis == '>'){
iThisIneq <- which(dataFit[,ivariableThis] > valThis)
}else{
vecCatsThis <- cubistModel$listSplitCats[[iR]][[jS]]
iThisIneq <- which(is.element(dataFit[,ivariableThis] , vecCatsThis))
}
if (jS == 1){
iThis <- iThisIneq
}else{
iThis <- intersect(iThis , iThisIneq)
}
}
matRuleData[iThis , iR] <- 1
}else{
### only one rule, so no condits in it!
iThis <- seq(n)
matRuleData[iThis,1] <- 1
}
if (iR == 1){
jThis <- 1:length(which(!is.na(cubistModel$dfCoeffs[1:iR,])))
}else{
jThis <- (length(which(!is.na(cubistModel$dfCoeffs[1:iR-1,]))) + 1):length(which(!is.na(cubistModel$dfCoeffs[1:iR,])))
}
ivThis <- which(!is.na(cubistModel$dfCoeffs[iR,]))
namesThis <- names(cubistModel$dfCoeffs)[ivThis]
pThis <- cubistModel$pVec[iR]
### put the values into X...
X[iThis,jThis[1]] <- 1
if(length(jThis) > 1){
if(length(jThis) == (length(cubistModel$listCoeffs_iv[[iR]]) + 1)){
### first of these columns is const, not in listCoeffs_iv
X[iThis,jThis[-1]] <- as.matrix(dataFit[iThis,cubistModel$listCoeffs_iv[[iR]]])
}else if(length(jThis) == length(cubistModel$listCoeffs_iv[[iR]])){
### this rule does not have a const, must have been removed because of colinearity between rules...
X[iThis,jThis] <- as.matrix(dataFit[iThis,cubistModel$listCoeffs_iv[[iR]]])
}else{
### something gone wrong.
print(iR)
print(jThis)
print(length(jThis))
print(cubistModel$listCoeffs_iv[[iR]])
print(length(cubistModel$listCoeffs_iv[[iR]]))
stop('Error - something has gone wrong with listCoeffs_iv!')
}
}else{}
} # end of loop over rules.
############################################
### cubsti predictions are X %*% betaCbst...
############################################
### divide by the number of rules applying for each row...
if(nRules > 1){
normWithinComms <- TRUE
if(!normWithinComms){
nRulesPerRow <- rowSums(matRuleData)
iTmp <- which(nRulesPerRow > 0)
X[iTmp,] <- X[iTmp,,drop=FALSE] / matrix(nRulesPerRow[iTmp] , length(iTmp) , ncol(X))
}else{
### within each committee...
iRTmp <- 1
for(iC in 1:cubistModel$committees){
jThis <- which(cubistModel$comms4XCubist == iC)
nRulesThis <- length(which(as.numeric(cubistModel$coefficients$committee) == iC))
if(length(jThis) > 0 & nRulesThis > 0){
matRuleDataThis <- matRuleData[,seq(iRTmp , iRTmp + nRulesThis - 1),drop=FALSE]
nRulesPerRowThis <- rowSums(matRuleDataThis)
iTmp <- which(nRulesPerRowThis > 0)
X[iTmp,jThis] <- X[iTmp,jThis,drop=FALSE] / matrix(nRulesPerRowThis[iTmp] , length(iTmp) , length(jThis))
iRTmp <- iRTmp + nRulesThis
}else{}
}
X <- X / length(unique(cubistModel$comms4XCubist))
}
}else{}
###########################################################
### if spline included, then cbind this...
###########################################################
if(length(cubistModel$allKnotsd) > 0){
XSpline <- allKnotsd2X(dIMidPts = dataFit$dIMidPts, allKnotsd = cubistModel$allKnotsd)
X <- cbind(X , XSpline)
}else{}
return(list('X' = X , 'matRuleData' = matRuleData))
}
###############################################################
### fn to make X given knots and dIMidPts.
### dIMidPts is a vector, evaluated as pt-support for these depths
### intercept is not included.
###############################################################
allKnotsd2X <- function(dIMidPts , allKnotsd){
dIMidPts <- as.numeric(dIMidPts)
if(length(allKnotsd) > 0){
intKnots <- allKnotsd[-1]
intKnots <- intKnots[-length(intKnots)]
bdryKnots <- c(allKnotsd[1] , allKnotsd[length(allKnotsd)])
XSpline <- bs(dIMidPts , knots = intKnots , degree = 3 , intercept = F , Boundary.knots = bdryKnots)
colnames(XSpline) <- paste0('dSpline.' , seq(ncol(XSpline)))
}else{
XSpline <- matrix(NA , n , 0)
}
return(XSpline)
}
cubist2XSetup <- function(cubistModel , dataFit , zFit = NULL , profIDFit = NULL , allKnotsd = c() , removeColinCols = TRUE , refineCubistModel = FALSE){
if(refineCubistModel & (!removeColinCols)){ stop('Error - if you want to refine the Cubist model, use removeColinCols = TRUE to make sure X is legal first.') }else{}
if(length(allKnotsd) == 0){
incdSpline <- FALSE
}else if(length(allKnotsd) >= 2){
incdSpline <- TRUE
}else{
stop('Error - if entering knots, then enter at least 2 knots (2 boundary knots plus any internal knots)')
}
##########################################################################
### check first row of data in cubistModel agrees with 1st row in dataFit
##########################################################################
dataRow1 <- strsplit(cubistModel$data , split = '\n')
nRowTmp <- length(dataRow1[[1]])
dataRow1 <- strsplit(dataRow1[[1]][1] , split = ',')[[1]]
dataRow1 <- dataRow1[-1]
if(length(dataRow1) != ncol(dataFit)){
stop(paste0('Error - to run cubist2XSetup, dataFit must be the same as the data used to fit the cubist model! But they have different numbers of columns!' ,
'\nTo get prediction design matrix, first update cubistModel by running cubist2X with the fitting data!'))
}else{}
if(nRowTmp != nrow(dataFit)){
stop(paste0('Error - to run cubist2XSetup, dataFit must be the same as the data used to fit the cubist model! But they have different numbers of rows!' ,
'\nTo get prediction design matrix, first update cubistModel by running cubist2X with the fitting data!'))
}else{}
for (j in 1:ncol(dataFit)){
if(is.numeric(dataFit[1,j])){
# checkThis <- dataFit[1,j] == as.numeric(dataRow1[j])
### only bother checking to 4 dp (rounding issues in above)...
### may not be sensible for some scales, but should be good enough to flag any big errors.
checkThis <- abs(dataFit[1,j] - as.numeric(dataRow1[j])) < 1E-4
}else{
checkThis <- dataFit[1,j] == dataRow1[j]
}
if(!checkThis){
warning(paste0('Warning - to run cubist2XSetup, dataFit must be the same as the data used to fit the cubist model!' ,
'\nColumn ' , j , ' does not seem to agree, check it! (Though difference may be because of rounding, which would be ok.)'))
}else{}
}
nRules <- nrow(cubistModel$coefficients)
dfCoeffs <- cubistModel$coefficients
#################################################
### check and remove the columns 'committee' and 'rule'
#################################################
committeeCheck <- as.integer(dfCoeffs$committee)
ruleCheck <- as.integer(dfCoeffs$rule)
doChecks <- FALSE
if(doChecks){
if ((max(committeeCheck) == 1) & (min(committeeCheck) == 1)){
### all ok
}else{
stop('More than 1 committees...not sure how to convert this to a design matrix')
}
if ((max(ruleCheck - seq(nRules)) == 0 ) & (min(ruleCheck - seq(nRules)) == 0 )){
### all ok
}else{
stop('I think this should not happen - check that my code deals with rules not ordered!')
}
}else{}
dfCoeffs$committee <- NULL
dfCoeffs$rule <- NULL
if(names(dfCoeffs)[1] != '(Intercept)'){ stop('Error - my coding assumes that intercept is the first column in dfCoeffs - check this!') }else{}
#################################################
### initialise listRules, listSplitCats and listCoeffs_iv...
### listCoeffs_iv is to quickly convert the variable names (col names of dfCoeffs without the intercept) into column numbers of the namesDataFit...
#################################################
namesdataFit <- names(dataFit)
listRules <- listSplitCats <- listCoeffs_iv <- vector("list" , length = nRules)
#################################################
### get the total number of linear parameters...
#################################################
p <- length(which(!is.na(dfCoeffs)))
pVec <- NA * integer(nRules)
if (nRules > 1){
### a way of extracting splits, though still with rounding errors.
vecSplitValues <- getCubistSplitsFromModel(cubistModel$model , dataFit)
}else{
vecSplitValues <- c()
}
for (iR in 1:nRules){
iC <- as.numeric(cubistModel$coefficients$committee[iR])
iRIniC <- as.numeric(cubistModel$coefficients$rule[iR])
if(nRules > 1){
### get all the data that fall into rule iRIniC of this committee...
### update for multiple committees, 28/02/19...
iTmp <- which(as.numeric(cubistModel$splits$rule) == iRIniC & as.numeric(cubistModel$splits$committee) == iC)
splitsThis <- cubistModel$splits[iTmp,]
splitValsThis <- vecSplitValues[iTmp]
vNamesThis <- gsub("\"" , "" , splitsThis$variable) # gsub used to get rid of "", which somehow got added.
### number of variables conditions in conditions for this rule...
listRules[[iR]] <- cubistModel$splits[iTmp,]
nVThis <- nrow(listRules[[iR]])
listRules[[iR]]$variable <- vNamesThis
### add which column number this is for quicker access...
listRules[[iR]]$ivariable <- NA
listRules[[iR]]$valUpdated <- splitValsThis
listSplitCats[[iR]] <- vector("list" , length = nVThis)
for (jS in 1:nVThis){
ivThis <- which(namesdataFit == vNamesThis[jS])
listRules[[iR]]$ivariable[jS] <- ivThis # order in the data frame.
dirThis <- splitsThis$dir[jS]
valThis <- splitValsThis[jS]
if (dirThis == ''){
### categorical variable...so get all cats
catsThis <- as.character(splitsThis$category[jS])
tmp <- gregexpr(',' , catsThis)
startsTmp <- c(1 , tmp[[1]]+1)
endsTmp <- c(tmp[[1]] - 1 , nchar(catsThis))
nCatsThis <- length(tmp[[1]]) + 1
vecCatsThis <- character(nCatsThis)
for (iCat in 1:nCatsThis){
vecCatsThis[iCat] <- substr(catsThis , startsTmp[iCat] , endsTmp[iCat])
}
listSplitCats[[iR]][[jS]] <- vecCatsThis
}else{}
}
}else{
### only one rule, so no condits in it!
}
namesTmp <- names(dfCoeffs)[!is.na(dfCoeffs[iR,])]
namesTmp <- namesTmp[-1]
ivTmp <- NA * integer(length(namesTmp))
for(j in 1:length(ivTmp)){
ivTmp[j] <- which(namesdataFit == namesTmp[j])
}
listCoeffs_iv[[iR]] <- ivTmp
pThis <- length(which(!is.na(dfCoeffs[iR,])))
pVec[iR] <- pThis
} # end of loop over rules.
if (nRules > 1){
cubistModel$splits$valUpdated <- vecSplitValues
}else{}
############################################
### cubsti predictions are X %*% betaCbst...
############################################
betaCbst <- as.numeric(t(as.matrix(dfCoeffs)))
iOK <- which(!is.na(betaCbst))
betaCbst <- matrix(betaCbst[iOK] , ncol = 1)
names4XCubist <- names(dfCoeffs)
names4XCubist[1] <- 'const'
names4XCubist <- rep(names4XCubist , nRules)
names4XCubist <- paste0(names4XCubist , rep(paste0('_R' , seq(nRules)) , each = ncol(dfCoeffs)))
comms4XCubist <- as.numeric(t(matrix(as.numeric(cubistModel$coefficients$committee) , nrow(dfCoeffs) , ncol(dfCoeffs))))
names4XCubist <- names4XCubist[iOK]
comms4XCubist <- comms4XCubist[iOK]
###########################################################################
#### if we have a d spline + d is a predictor in every rule,
### then remove d as a predictor from the final rule of the cubist model...
### otherwise numberical errors (lin fn appears twice).
### CURRENTLY REMOVED FROM SETUP, BUT MAYBE EASIER TO TRACK IF STILL INCLUDED IN SETUP,
### BUT A LIST OF COLUMNS TO BE REMOVED FROM X IS SAVED.
###########################################################################
if(incdSpline){
if(is.element('dIMidPts' , names(cubistModel$coefficients)) && (length(which(is.na(cubistModel$coefficients$dIMidPts))) == 0)){
dfCoeffs$dIMidPts[nRules] <- NA
listCoeffs_iv[[nRules]] <- setdiff(listCoeffs_iv[[nRules]] , which(namesdataFit == 'dIMidPts'))
ipRemoveFromCubist <- which(names4XCubist == paste0("dIMidPts_R" , nRules))
names4XCubist <- names4XCubist[-ipRemoveFromCubist]
comms4XCubist <- comms4XCubist[-ipRemoveFromCubist]
betaCbst <- betaCbst[-ipRemoveFromCubist,,drop=FALSE]
pVec[nRules] <- pVec[nRules] - 1
}else{}
}else{}
#######################################################
### add all useful info to the cubist model object...
#######################################################
cubistModel$dfCoeffs <- dfCoeffs
cubistModel$namesDataFit <- names(dataFit)
cubistModel$listCoeffs_iv <- listCoeffs_iv
cubistModel$listRules <- listRules
cubistModel$listSplitCats <- listSplitCats
cubistModel$pVec <- pVec
cubistModel$betaCbst <- betaCbst
cubistModel$names4XCubist <- names4XCubist
cubistModel$comms4XCubist <- comms4XCubist
if(length(allKnotsd) > 0){
cubistModel$allKnotsd <- allKnotsd
}else{ # assigning as c() doesn't work, so:
cubistModel['allKnotsd'] <- list(NULL)
}
#######################################################
### convert to X, and check for potential colinearity within rules...
### remove columns from X and the cubist set up variables if found.
#######################################################
tmp <- cubist2XGivenSetup(cubistModel , dataFit)
X <- tmp$X
matRuleData <- tmp$matRuleData
if(any(rowSums(is.na(X)) > 0)){ stop('Some error in conversion of cubist model to X has produced NA values!') }else{}
if(removeColinCols){
if(cubistModel$committees == 1){
# ipRmvd <- getColinPreds(X = X)
if(is.null(zFit)){ stop('Error - to use legalizeXIAK3D, must input zFit') }else{}
ipRmvd <- legalizeXIAK3D(X = X , z = zFit)$ipRemove
tmp <- removeColinPreds(X = X , cubistModel = cubistModel , dataFit = dataFit , ipRmvd = ipRmvd , reasonRmv = 0)
X <- tmp$X
cubistModel <- tmp$cubistModel
if(refineCubistModel){
### further refine the model, by removing predictors with p > 0.15
if(is.null(profIDFit)){ stop('Error - to use refineXIAK3D, must input profIDFit') }else{}
ipRmvd <- refineXIAK3D(X = X , z = zFit , profID = profIDFit , alpha = 0.15)$ipRemove
tmp <- removeColinPreds(X = X , cubistModel = cubistModel , dataFit = dataFit , ipRmvd = ipRmvd , reasonRmv = 1)
X <- tmp$X
cubistModel <- tmp$cubistModel
}else{}
}else{
### look for any rule conditions that are duplicated...
# listRulesTmp <- cubistModel$listRules
# for (i in 1:length(listRulesTmp)){
# listRulesTmp[[i]]$committee <- NULL
# listRulesTmp[[i]]$rule <- NULL
# listRulesTmp[[i]]$type <- NULL
# listRulesTmp[[i]]$percentile <- NULL
# listRulesTmp[[i]]$valUpdated <- NULL
# oTmp <- order(listRulesTmp[[i]]$ivariable , listRulesTmp[[i]]$value)
# listRulesTmp[[i]] <- listRulesTmp[[i]][oTmp,]
# }
# iDup <- which(duplicated(listRulesTmp))
### if multiple committees, remove colinearity from each committee first, then
### remove colinearity from combined model
iRTmp <- 1
ipRmvd <- c()
for(iC in 1:cubistModel$committees){
jThis <- which(cubistModel$comms4XCubist == iC)
nRulesThis <- length(which(as.numeric(cubistModel$coefficients$committee) == iC))
iRThis <- seq(iRTmp , iRTmp + nRulesThis - 1)
ipRmvdThis <- getColinPreds(X = X[,jThis,drop=FALSE])
if(!is.null(ipRmvdThis)){
ipRmvd <- c(ipRmvd , jThis[ipRmvdThis])
}else{}
iRTmp <- iRTmp + nRulesThis
}
tmp <- removeColinPreds(X = X , cubistModel = cubistModel , dataFit = dataFit , ipRmvd = ipRmvd)
X <- tmp$X
cubistModel <- tmp$cubistModel
nRPerC <- NA * integer(cubistModel$committees)
for(iC in 1:cubistModel$committees){
nRPerC[iC] <- length(which(cubistModel$coefficients$committee == iC))
}
### remove const from final rule of all comms > 1, due to perfect colinearity with sum of constants from comm = 1.
### though if final rule is const only, remove pre-ceding rule.
ipRmvd <- c()
for(iC in 2:cubistModel$committees){
rTmp <- sum(nRPerC[1:iC])
while(pVec[rTmp] <= 1){
rTmp <- rTmp - 1
}
if(rTmp <= (sum(nRPerC[1:iC]) - nRPerC[iC])){
### all rules have just const. not sure if removing will work in my code.
stop('All rules in this committee are constant only - not sure this will work!')
}else{}
ipRmvd <- c(ipRmvd , which(cubistModel$names4XCubist == paste0('const_R' , rTmp)))
}
tmp <- removeColinPreds(X = X , cubistModel = cubistModel , dataFit = dataFit , ipRmvd = ipRmvd)
X <- tmp$X
cubistModel <- tmp$cubistModel
### now check full combined model, and if any colin exists, delete more...
ipRmvd <- getColinPreds(X = X , comms4XCubist = cubistModel$comms4XCubist)
tmp <- removeColinPreds(X = X , cubistModel = cubistModel , dataFit = dataFit , ipRmvd = ipRmvd)
X <- tmp$X
cubistModel <- tmp$cubistModel
}
}else{}
### add namesX to the cubistModel, which has the names4XCubist as well as the spline names.
cubistModel$namesX <- colnames(X)
return(list('X' = X , 'cubistModel' = cubistModel , 'matRuleData' = matRuleData))
}
###########################################################################
### just get the indices of columns that can be removed from X to make XX invertible...
###########################################################################
getColinPreds <- function(X , y = NULL , comms4XCubist = NULL){
namesX <- colnames(X)
ipRmvd <- namesXRmvd <- c()
origColNumbers <- seq(ncol(X))
XCurrent <- X
XX <- t(XCurrent) %*% XCurrent
eXX <- eigen(XX)
iXX <- try(solve(XX) , silent = TRUE)
### overkill using ncol(X) here, but safe.
# isConstTmp <- is.element(namesX , paste0('const_R' , seq(ncol(X))))
# isConstTmp <- is.element(namesX , paste0('constjibberjabber_R' , seq(ncol(X))))
### get which columns of X are the only column of that rule...
### and don't allow those columns to be removed.
tmp <- strsplit(namesX , '_R')
ruleNumbers <- NA * integer(length(tmp))
for(i in 1:length(tmp)){
ruleNumbers[i] <- as.numeric(tmp[[i]][length(tmp[[i]])])
}
tblTmp <- table(ruleNumbers)
iTmp <- which(tblTmp == 1)
rulesKeepTmp <- as.integer(rownames(tblTmp)[iTmp])
colsKeepTmp <- which(is.element(ruleNumbers , rulesKeepTmp))
while(is.character(iXX)){
jMin <- which.min(eXX$value)
evec <- eXX$vectors[,jMin]
evec[colsKeepTmp] <- 0 # set these to 0 so they won't be removed.
if(is.null(comms4XCubist)){
ipRmvFromCurrent <- which.max(abs(evec))
}else{
# take the top 5 contributing columns, get the subset of these which have the largest comm number
# then the max abs(evec) value of these
iPoss <- order(-abs(evec))
if(length(iPoss) > 5){
iPoss <- iPoss[1:5]
}else{}
commsPoss <- comms4XCubist[iPoss]
iPoss <- iPoss[which(commsPoss == max(commsPoss))]
ipRmvFromCurrent <- iPoss[which.max(abs(evec[iPoss]))]
}
ipRmvThis <- origColNumbers[ipRmvFromCurrent]
ipRmvd <- c(ipRmvd , ipRmvThis)
namesXRmvd <- c(namesXRmvd , namesX[ipRmvThis])
comms4XCubist <- comms4XCubist[-ipRmvFromCurrent]
origColNumbers <- origColNumbers[-ipRmvFromCurrent]
ruleNumbers <- ruleNumbers[-ipRmvFromCurrent]
tblTmp <- table(ruleNumbers)
iTmp <- which(tblTmp == 1)
rulesKeepTmp <- as.integer(rownames(tblTmp)[iTmp])
colsKeepTmp <- which(is.element(ruleNumbers , rulesKeepTmp))
XCurrent <- XCurrent[,-ipRmvFromCurrent,drop=FALSE]
XX <- XX[-ipRmvFromCurrent,-ipRmvFromCurrent,drop=FALSE]
eXX <- eigen(XX)
iXX <- try(solve(XX) , silent = TRUE)
}
return(ipRmvd)
}
removeColinPreds <- function(X , cubistModel , dataFit , ipRmvd , reasonRmv = 0){
n <- nrow(X)
### if there are any NAs, remove...
nRules <- length(cubistModel$listRules)
namesX <- colnames(X)
#####################################################################
### update the things in the cubistModel in light of the removed parameters...
#####################################################################
if(length(ipRmvd) > 0){
cubistModel$names4XCubistRmvd <- c(cubistModel$names4XCubistRmvd , namesX[ipRmvd])
cubistModel$betaCbst <- cubistModel$betaCbst[-ipRmvd,,drop=FALSE]
cubistModel$names4XCubist <- cubistModel$names4XCubist[-ipRmvd]
cubistModel$comms4XCubist <- cubistModel$comms4XCubist[-ipRmvd]
if(reasonRmv == 0){
print('Some colinear columns found in X - legalizing X by removing the following columns:')
}else if(reasonRmv == 1){
print('Some redundant columns found in X - refining X by removing the following columns:')
}else{
stop('Enter valid reasonRmv value to removeColinPreds (0 or 1)!')
}
for(i in 1:length(ipRmvd)){ print(paste0(i , '. ' , namesX[ipRmvd[i]])) }
X <- X[,-ipRmvd,drop=FALSE]
rulesBad <- colsBad <- colsBad2 <- NA * integer(length(ipRmvd))
for (i in 1:length(ipRmvd)){
badTmp <- strsplit(namesX[ipRmvd[i]] , '_R')
rulesBad[i] <- as.numeric(badTmp[[1]][length(badTmp[[1]])]) # rule numbers in my notation (cts over all committees)
cubistModel$pVec[rulesBad[i]] <- cubistModel$pVec[rulesBad[i]] - 1
if(rulesBad[i] < 10){
nameBad <- substr(namesX[ipRmvd[i]] , 1 , nchar(namesX[ipRmvd[i]]) - 3)
}else if(rulesBad[i] < 100){
nameBad <- substr(namesX[ipRmvd[i]] , 1 , nchar(namesX[ipRmvd[i]]) - 4)
}else if(rulesBad[i] < 1000){
nameBad <- substr(namesX[ipRmvd[i]] , 1 , nchar(namesX[ipRmvd[i]]) - 5)
}else{
nameBad <- substr(namesX[ipRmvd[i]] , 1 , nchar(namesX[ipRmvd[i]]) - 6)
}
### first within dfCoeffs...
if(nameBad == 'const'){ nameBad <- '(Intercept)' }else{}
jBad <- which(names(cubistModel$dfCoeffs) == nameBad)
if(length(jBad) == 0){
stop(paste0('Error - the name of the bad predictor (' , nameBad , ') has not been found in dfCoeffs!'))
}else if(length(jBad) > 1){
stop(paste0('Error - multiple names of the bad predictor (' , nameBad , ') have been found in dfCoeffs!'))
}else{}
colsBad[i] <- jBad
### second within dataFit...
if(nameBad == '(Intercept)'){
jBad <- NA
}else{
jBad <- which(names(dataFit) == nameBad)
}
if(length(jBad) == 0){
stop(paste0('Error - the name of the bad predictor (' , nameBad , ') has not been found in dataFit!'))
}else if(length(jBad) > 1){
stop(paste0('Error - multiple names of the bad predictor (' , nameBad , ') have been found in dataFit!'))
}else{}
colsBad2[i] <- jBad
}
for(i in 1:length(rulesBad)){
cubistModel$dfCoeffs[rulesBad[i] , colsBad[i]] <- NA
}
for(i in 1:length(rulesBad)){
if(!is.na(colsBad2[i])){
tmp <- cubistModel$listCoeffs_iv[[rulesBad[i]]]
itmp <- which(tmp == colsBad2[i])
if(length(itmp) == 1){
tmp <- tmp[-itmp]
cubistModel$listCoeffs_iv[[rulesBad[i]]] <- tmp
}else if(length(itmp) == 0){
stop('Error - not found a removed variable in listCoeffs_iv!')
}else{
stop('Error - found multiple occurences of a removed variable in listCoeffs_iv!')
}
}else{}
}
for(i in 1:nRules){ cubistModel$listCoeffs_iv[[i]] <- cubistModel$listCoeffs_iv[[i]][which(!is.na(cubistModel$listCoeffs_iv[[i]]))] }
}else{}
return(list('X' = X , 'cubistModel' = cubistModel))
}
getCubistSplitsFromModel <- function(x , dataFit = NULL)
{
### argument x is cubistModel$model
### dataFit (if given) is the data that were used to fit model
### if given, snap any splits to nearest data point if one within 1E-4
x <- strsplit(x, "\n")[[1]]
comNum <- ruleNum <- condNum <- rep(NA, length(x))
comIdx <- rIdx <- 0
for(i in seq(along = x))
{
tt <- parserTmp(x[i])
## Start of a new rule
if(names(tt)[1] == "rules")
{
comIdx <- comIdx + 1
rIdx <- 0
}
comNum[i] <-comIdx
## Start of a new condition
if(names(tt)[1] == "conds")
{
rIdx <- rIdx + 1
cIdx <- 0
}
ruleNum[i] <-rIdx
## Within a rule, type designates the type of conditional statement
## type = 2 appears to be a simple split of a continuous predictor
if(names(tt)[1] == "type")
{
cIdx <- cIdx + 1
condNum[i] <- cIdx
}
}
numCom <- sum(grepl("^rules=", x))
rulesPerCom <- unlist(lapply(split(ruleNum, as.factor(comNum)), max))
rulesPerCom <- rulesPerCom[rulesPerCom > 0]
if (! is.null(rulesPerCom) && numCom > 0)
names(rulesPerCom) <- paste("Com", 1:numCom)
## In object x, what element starts a new rule
isNewRule <- ifelse(grepl("^conds=", x), TRUE, FALSE)
splitVar <- rep("", length(x))
splitVal <- rep(NA, length(x))
splitCats <- rep("", length(x))
splitDir <- rep("", length(x))
## This is a simple continuous split, such as
##
## nox > 0.668
##
## or
##
## type="2" att="nox" cut="0.66799998" result=">"
##
isType2 <- grepl("^type=\"2\"", x)
if(any(isType2))
{
splitVar[isType2] <- type2Tmp(x[isType2])$var
splitVar[isType2] <- gsub("\"", "", splitVar[isType2])
splitDir[isType2] <- type2Tmp(x[isType2])$rslt
splitVal[isType2] <- type2Tmp(x[isType2])$val
}
## This is a split of categorical data such as
##
## X4 in {c, d}
##
## or
##
## type="3" att="X4" elts="c","d"
##
isType3 <- grepl("^type=\"3\"", x)
if(any(isType3))
{
splitVar[isType3] <- type3Tmp(x[isType3])$var
splitCats[isType3] <- type3Tmp(x[isType3])$val
splitCats[isType3] <- gsub("[{}]", "", splitCats[isType3])
splitCats[isType3] <- gsub("\"", "", splitCats[isType3])
splitCats[isType3] <- gsub(" ", "", splitCats[isType3])
}
if(!any(isType2) & !any(isType3)) return(NULL)
splitData <- data.frame(committee = comNum,
rule = ruleNum,
variable = splitVar,
dir = splitDir,
value = as.numeric(splitVal),
category = splitCats)
splitData$type <- ""
if(any(isType2)) splitData$type[isType2] <- "type2"
if(any(isType3)) splitData$type[isType3] <- "type3"
splitData <- splitData[splitData$variable != "" ,]
splitData
if(!is.null(dataFit)){
### snap cts splits to nearest datapoint if within 1E-4 (as some rounding errors in display/table)
### better way might be to round appropriately before fitting cubist model
for(i in 1:length(splitData$val)){
if(splitData$type[i] == 'type2'){
zThis <- dataFit[[as.character(splitData$variable[i])]]
iTmp <- which.min(abs(zThis - splitData$val[i]))
if(abs(zThis[iTmp] - splitData$val[i]) < 1E-4){
splitData$val[i] <- zThis[iTmp]
}else{}
}else{}
}
}else{}
return(splitData$val)
}
type3Tmp <- function(x)
{
aInd <- regexpr("att=", x)
eInd <- regexpr("elts=", x)
var <- substring(x, aInd + 4, eInd - 2)
val <- substring(x, eInd + 5)
multVals <- grepl(",", val)
val <- gsub(",", ", ", val)
val <- ifelse(multVals, paste("{", val, "}", sep = ""), val)
txt <- ifelse(multVals, paste(var, "in", val), paste(var, "=", val))
list(var = var, val = val, text = txt)
}
type2Tmp <- function(x, dig = 16)
{
x <- gsub("\"", "", x)
aInd <- regexpr("att=", x)
cInd <- regexpr("cut=", x)
rInd <- regexpr("result=", x)
vInd <- regexpr("val=", x)
var <- val <- rslt <- rep("", length(x))
missingRule <- cInd < 1 & vInd > 0
if(any(missingRule))
{
var[missingRule] <- substring(x[missingRule], aInd[missingRule] + 4, vInd[missingRule] - 2)
val[missingRule] <- "NA"
rslt[missingRule] <- "="
}
if(any(!missingRule))
{
var[!missingRule] <- substring(x[!missingRule], aInd[!missingRule] + 4, cInd[!missingRule] - 2)
val[!missingRule] <- substring(x[!missingRule], cInd[!missingRule] + 4, rInd[!missingRule] - 1)
val[!missingRule] <- format(as.numeric(val[!missingRule]), digits = dig)
rslt[!missingRule] <- substring(x[!missingRule], rInd[!missingRule] + 7)
}
list(var = var, val = as.numeric(val), rslt = rslt,
text = paste(var, rslt, val))
}
parserTmp <- function(x)
{
x <- strsplit(x, " ")
x <- lapply(x,
function(y)
{
y <- strsplit(y, "=")
nms <- unlist(lapply(y, function(z) z[1]))
val <- unlist(lapply(y, function(z) z[2]))
names(val) <- nms
val
})
if(length(x) == 1) x <- x[[1]]
x
}
##############################################################
### function to get all d Breaks from cubist model (which has already had cubist2X run on it).
##############################################################
getAlldBreaks <- function(cubistModel){
if(!is.element('listRules' , names(cubistModel))){ stop('Error - run cubist2X function before getting all dBreaks!') }else{}
dfdBreaks <- lapply(cubistModel$listRules , function(dfIn){ dfIn[which(dfIn$variable == 'dIMidPts'),,drop=FALSE] })
dfdBreaks <- do.call(rbind , dfdBreaks)
if(!is.null(dfdBreaks)){
dBreaks <- unique(dfdBreaks$valUpdated)
dBreaks <- dBreaks[order(dBreaks)]
}else{
dBreaks <- c()
}
return(dBreaks)
}
##############################################################
### function to get the indices of the rules with d in the rule conditions from cubist model (which has already had cubist2X run on it).
##############################################################
getRulesWithdInCondits <- function(cubistModel){
if(!is.element('listRules' , names(cubistModel))){ stop('Error - run cubist2X function before getting all dBreaks!') }else{}
ndBreaksPerRule <- unlist(lapply(cubistModel$listRules , function(dfIn){ length(which(dfIn$variable == 'dIMidPts')) }))
return(which(ndBreaksPerRule > 0))
}
##############################################################
### function to rule numbers from X column names from cubist model (which has already had cubist2X run on it).
##############################################################
getRuleNumbersFromColNames <- function(cubistModel){
if(!is.element('listRules' , names(cubistModel))){ stop('Error - run cubist2X function before getting all dBreaks!') }else{}
### split by '_'
tmp <- strsplit(cubistModel$names4XCubist , '_')
### get final elements...
tmp <- unlist(lapply(tmp, tail , n = 1L))
### remove 'R' and convert to integer...
jRules <- as.integer(substr(tmp , 2 , nchar(tmp)))
return(jRules)
}
##############################################################
### function to remove variables from X to make inv(XX) exist.
### does this by fitting models if possible with 1 column removed
### if any are legal, select the one with smallest nll
### else continue looking at all models with 2 columns removed
### if 2 doesn't work, function will stop and return error
### could continue but will get more time consuming -
### so either continue or write something different.
##############################################################
legalizeXIAK3D <- function(X , z){
XX <- t(X) %*% X
Xz <- t(X) %*% z
n <- length(z)
# tmp0 <- try(solve(XX , Xz) , silent = TRUE)
# tmp <- lndetANDinvCb(XX, Xz)
iXXXz <- try(solve(XX , Xz) , silent = TRUE)
nCols2Rmv <- 0
# continueRemoving <- (is.na(tmp$lndet) | is.character(tmp0))
continueRemoving <- (is.character(iXXXz))
ipRemove <- c()
while(continueRemoving){
nCols2Rmv <- nCols2Rmv + 1
cands <- setdiff(seq(ncol(X)) , which(is.element(colnames(X) , paste0('const_R' , seq(100)))))
cands <- setdiff(cands , which(colnames(X) == 'dSpline'))
if (nCols2Rmv == 1){
nllTest <- NA * numeric(ncol(X))
for (j in cands){
nllTest[j] <- nllLm(z = z , X = X[,-j,drop=FALSE] , REML = F)$nll
}
if(all(is.na(nllTest))){
continueRemoving <- TRUE
}else{
ipRemove <- which.min(nllTest)
namesRemove <- colnames(X)[ipRemove]
continueRemoving <- FALSE
}
}else if (nCols2Rmv == 2){
nllTest <- matrix(NA , ncol(X) , ncol(X))
for (j in cands){
for(k in cands){
if(k>j){
nllTest[j,k] <- nllLm(z = z , X = X[,-c(j,k),drop=FALSE] , REML = F)$nll
}else{}
}
}
if(all(is.na(nllTest))){
continueRemoving <- TRUE
# stop('Generalise this algorithm or write another to better remove colinear predictors when >2 need removing!')
}else{
ipRemove <- which(nllTest == min(nllTest , na.rm = TRUE) , arr.ind = TRUE)
ipRemove <- as.numeric(ipRemove[1,])
namesRemove <- colnames(X)[ipRemove]
continueRemoving <- FALSE
}
}else if (nCols2Rmv == 3){
nllTest <- array(NA , dim = c(ncol(X) , ncol(X) , ncol(X)))
for (j in cands){
for(k in cands){
for(l in cands){
if(k>j & l>k){
nllTest[j,k,l] <- nllLm(z = z , X = X[,-c(j,k,l),drop=FALSE] , REML = F)$nll
}else{}
}
}
}
if(all(is.na(nllTest))){
continueRemoving <- TRUE
stop('Generalise this algorithm or write another to better remove colinear predictors when >3 need removing!')
}else{
ipRemove <- which(nllTest == min(nllTest , na.rm = TRUE) , arr.ind = TRUE)
ipRemove <- as.numeric(ipRemove[1,])
namesRemove <- colnames(X)[ipRemove]
continueRemoving <- FALSE
}
}else{
stop('Generalise this algorithm or write another to better remove colinear predictors when >3 need removing!')
}
}
if(length(ipRemove) > 0){
namesRemove <- colnames(X)[ipRemove]
X <- X[,-ipRemove,drop=FALSE]
}else{