0

I am trying to evaluate some linear discriminant models with the package caret. The problem is that when I use the predict function it predicts less values than the ones that are given in the data fame. For example, my testing sample has 664 observations, but only 550 values are predicted. I don't know why this is happening, and the weidest thing is that when I use a lda model from the package MASS this doesn't happen.

I leave a sample of my code:

hoja_excel <- function(nombre_libro, nombre_hoja, modelo, datos, submuestra) {
  libro <- createWorkbook(type = "xlsx")
  hoja <- createSheet(libro, sheetName = nombre_hoja)
  datos_t <- datos
  modelo_t <- modelo
  prediccion <- predict(modelo_t, datos_t)
  if (submuestra == "No") {
    correctos <- ifelse(datos_t$d_evento == prediccion$class, 1, 0)
    addDataFrame(modelo_t$scaling, hoja, startRow = 1, startColumn = 1)
    addDataFrame(prediccion$class, hoja, startRow = 1, startColumn = 5, 
                 row.names = FALSE)
    addDataFrame(prediccion$posterior, hoja, startRow = 1, startColumn = 7, 
                 row.names = FALSE)
  } else if (submuestra == "Si") {
    correctos <- ifelse(datos_t$d_evento == prediccion, 1, 0)  
    addDataFrame(modelo_t$finalModel$scaling, hoja, startRow = 1, startColumn = 1)
    addDataFrame(prediccion, hoja, startRow = 1, startColumn = 5, row.names = FALSE)
  } else {
    print("Valor no válido para submuestra")
    break
  }
  addDataFrame(correctos, hoja, startRow = 1, startColumn = 6, row.names = FALSE)
  addDataFrame(datos_t$d_evento, hoja, startRow = 1, startColumn = 4, 
               row.names = FALSE)
  direccion <- paste("C:\\Users\\Documents\\Indicador riesgo quiebra\\Bases R\\",
                     gsub(" ", "", paste(nombre_libro, ".xlsx")))
  saveWorkbook(libro, direccion)
} 

where modelo_t is a lda model using:

train_index <- createDataPartition(datos_temporal$d_evento, p = 0.8, list = FALSE)
training <- datos_temporal[train_index,]
testing <- datos_temporal[-train_index,]
control <- trainControl(method = "boot", number = 100)
indice_fila <- as.data.frame(as.numeric(row.names(training)))
colnames(indice_fila)[1] <- "x"
indice_fila <- filter(indice_fila, x > 103)
por_quitar <- sample(indice_fila$x, 457)
temp_cambio <- training[por_quitar,]
training <- training[!(as.numeric(row.names(training)) %in% por_quitar),]
  sum(training$Concurso)
  testing <- rbind(testing, temp_cambio)
modelo_temp <- train(as.factor(d_evento) ~  WCTA + RETA_div + EBITTA + MKTTL, data = training, method = "lda", trControl = control)

The data looks like this:

 d_evento         WCTA     RETA_div       EBITTA        MKTL
  [1,]        1  0.102328417  0.172985238  0.023823373  0.28150867
  [2,]        1  0.104033108  0.179734113  0.016205173  0.29846677
  [3,]        1  0.062691788  0.176511714  0.016526565  0.27814601
  [4,]        1 -0.006734752  0.176793439 -0.002690483  0.13984261
  [5,]        1 -0.531363807 -0.126163036 -0.370620144  0.09432537
  [6,]        1 -0.290474435 -0.211740091 -0.011615337  0.36301834
  [7,]        1 -0.466771743 -0.512553803 -0.224013967  0.25744777
  [8,]        1 -0.479582320 -0.511735167  0.002644372  0.09629447
  [9,]        1 -0.505509630 -0.515334951  0.000907695  0.08729133
 [10,]        1 -0.505772731 -0.519775895  0.003711561  0.07597238
 [11,]        1 -0.081045926  0.021407478  0.010135867  0.12172863
 [12,]        1 -0.089806538 -0.004288951  0.006143543  0.07635110
 [13,]        1 -0.089806538 -0.004288951  0.006143543  0.07635110
 [14,]        1 -0.089658569 -0.003487434  0.007065595  0.05881547
 [15,]        1 -0.071556860  0.000327249  0.009000073  0.04642630
 [16,]        1 -0.189795369 -0.082028980 -0.011347470          NA
 [17,]        1 -0.211664986  0.089646083 -0.013821279          NA
 [18,]        1 -0.240479274  0.076507841 -0.009437117          NA
 [19,]        1 -0.304366126 -0.154540326 -0.009604424          NA
 [20,]        1 -0.375355273 -0.199723398 -0.024588066          NA
 [21,]        1 -0.029508414 -0.403511498 -0.019443447          NA
 [22,]        1 -0.091914540 -0.488177906 -0.048564395          NA
 [23,]        1 -0.298168224 -0.732771269 -0.172331020          NA
 [24,]        1 -0.359041383 -0.790119097 -0.039597788          NA
 [25,]        1 -0.363742236 -0.803537586 -0.006065854          NA
 [26,]        1  0.589143817  0.028219641  0.026082170  0.32713918
 [27,]        1  0.593191389  0.036273767  0.020408613  0.26683046
 [28,]        1  0.173117821  0.235442662 -0.001729736  0.24438442
 [29,]        1  0.585950807  0.003848530  0.003906928  0.16745067
 [30,]        1  0.096615605 -0.240512670 -0.267238705  0.07577302
 [31,]        1 -0.236399326 -0.198074835 -0.082469329  0.02060548
 [32,]        1 -0.232475389 -0.162193604  0.008983155  0.02490547
 [33,]        1 -0.264922798 -0.195772340  0.004671963  0.01937249
 [34,]        1 -0.252437477 -0.177531447  0.008358117  0.01538876
 [35,]        1 -0.309372439 -0.157687566 -0.014696488  0.01566678
 [36,]        1  0.066139324  0.065790059  0.012793123          NA
 [37,]        1  0.019243390 -0.021989191  0.005530848          NA
 [38,]        1 -0.322167102 -0.076949045  0.009095588          NA
 [39,]        1 -0.339073771 -0.088969769  0.004273673          NA
 [40,]        1 -0.341548755 -0.095563596  0.004611532          NA
 [41,]        1  0.119794925 -0.362801650  0.017772949  0.12511261
 [42,]        1  0.117203237 -0.358876513  0.013895464  0.17573193
 [43,]        1  0.118311588 -0.349316342  0.011311633  0.16578745
 [44,]        1  0.108041129 -0.354728885  0.007139983  0.18585194
 [45,]        1  0.116226413  0.176991083  0.008437309  0.14523250
 [46,]        1 -0.417426682 -0.351217302 -0.001490772          NA
 [47,]        1 -0.521614557 -0.372067598 -0.004148459          NA
 [48,]        1 -0.556880835 -0.410263995 -0.010783372          NA
 [49,]        1 -0.538665877 -0.516306250 -0.015715087          NA
 [50,]        1 -0.552574629 -0.539557001  0.000512035          NA
 [51,]        1 -0.107124224 -0.198470866  0.009393807  0.37339624
 [52,]        1 -0.138723041 -0.180931099  0.009621652  0.37314090
 [53,]        1 -0.185465637 -0.209724465  0.007502654  0.15136401
 [54,]        1 -0.215398142 -0.194322294  0.005454704  0.10933219
 [55,]        1 -0.349924680 -0.181051506  0.007898349  0.10242043
 [56,]        1 -0.083508805 -0.234198161  0.014025553  0.21949797
 [57,]        1 -0.111655586 -0.153118296  0.000395625  0.20500320
 [58,]        1 -0.525398938 -0.204706959  0.004258002  0.22263430
 [59,]        1 -0.515984436  0.006542066  0.010284407  0.06157929
 [60,]        1 -0.472184418 -0.077255180  0.025169087  0.06360715
 [61,]        1 -0.915619436 -0.571898297 -0.964069641  0.06954642
 [62,]        1 -0.976531176 -0.604232871  0.002430217  0.07139988
 [63,]        1 -1.065185735 -0.769319144 -0.057748756  0.06288110
 [64,]        1 -1.105191220 -0.809208711 -0.008095136  0.06166852
 [65,]        1 -1.178063409 -0.878734357 -0.039329161  0.06082261
 [66,]        1 -0.535907882 -0.222222213  0.005872205  0.08846701
 [67,]        1 -0.589313029 -0.258109442  0.008729062  0.05007868
 [68,]        1 -0.551131177 -0.238096813  0.009023884  0.07187476
 [69,]        1 -0.608438635 -0.283387832  0.013695457  0.08705984
 [70,]        1 -0.535464916 -0.262771019  0.008364580  0.09314005
 [71,]        1 -0.452234218 -0.215778265 -0.000239063  0.02542849
 [72,]        1 -0.464330590 -0.218106230  0.002584991  0.03582109
 [73,]        1 -0.563726013 -0.224167223  0.000208095  0.04189381
 [74,]        1 -0.528027421 -0.198080661 -0.003679190  0.04983422
 [75,]        1 -0.575153818 -0.285992633 -0.005243519  0.03923600
 [76,]        1 -0.066170557 -0.239054398 -0.013494570  0.26130971
 [77,]        1 -0.066170557 -0.233443586  0.014990892  0.26130971
 [78,]        1 -0.121724697 -0.303849542 -0.040604471  0.28052208
 [79,]        1 -0.154056271 -0.343746431 -0.012184790  0.20584284
 [80,]        1 -0.147762785 -0.565698665 -0.020414877  0.17744193
 [81,]        0  0.150063189  0.474291335  0.062842129  4.06784731
 [82,]        0  0.216434477  0.400717535  0.153657901  5.36813447
 [83,]        0  0.227828887  0.414654219  0.060695982  6.78856190
 [84,]        0  0.074576430  0.452425070  0.048256387  3.48195705
 [85,]        0  0.075965420  0.053845661  0.032662793  4.23785100
 [86,]        0  0.068738217  0.179754934  0.026999530  5.81196756
 [87,]        0  0.002872408  0.176252411  0.036614004  5.22779285
 [88,]        0 -0.004109057  0.200224409  0.039233767  3.28458093
 [89,]        0  0.059667688  0.213209500  0.026436922  2.81650866
 [90,]        0  0.097700665  0.250799825  0.022532687  2.15307406
 [91,]        0  0.069989183  0.279071982  0.022807557  1.90599648
 [92,]        0  0.068567070  0.597104453  0.007370365  1.45980346
 [93,]        0 -0.055451852  0.005400669  0.006371529  0.80260752
 [94,]        0  0.063809942  0.002149151  0.003025512  1.62787332
 [95,]        0  0.156181914  0.148482202  0.018242339  1.54990111
 [96,]        0  0.191253103  0.152796761  0.028705654  1.27834785
 [97,]        0  0.179348459  0.026865682  0.021577617  1.10293585
 [98,]        0  0.220742857  0.257825369  0.025130265  1.38404540
 [99,]        0  0.197537878  0.247273292  0.032118478  1.17929573
[100,]        0  0.201030587  0.289748972  0.023089870  1.45426949
[101,]        0  0.198196754  0.251331400  0.018783216  0.78407849
[102,]        0  0.396957178  0.156756494  0.011094761  6.50241941
[103,]        0  0.417743322  0.192454416  0.021462730  4.17671640
[104,]        0  0.317611590  0.220966177  0.019126507  3.14608329
[105,]        0  0.292430463  0.236581805  0.027051067  1.23086306
[106,]        0  0.121646123  0.264528001  0.015549968  0.43782518
[107,]        0  0.254774431  0.313459475  0.020259342  0.90682847
[108,]        0  0.057719295  0.376094461  0.018757697  0.70161209
[109,]        0  0.111145476  0.074000580  0.021489573  0.50180140
[110,]        0  0.129263860  0.113104859  0.025172665  0.77670065
[111,]        0  0.161287802  0.364706919  0.027780009  1.71408772
[112,]        0  0.102491082  0.336435093  0.019495483  1.87903084
[113,]        0  0.102749342  0.179662445  0.019143735  0.66004885
[114,]        0  0.070178639  0.181968540  0.015834552  0.56760009
[115,]        0  0.060546180  0.144755108  0.023018262  0.41864727
[116,]        0  0.055087904  0.166166504  0.012629684  0.33324991
[117,]        0  0.303542935  0.156757706  0.031636761  1.64602627
[118,]        0  0.289001786  0.153547671 -0.016374501  1.76725379
[119,]        0  0.279778721  0.153493561  0.020405527  1.58651203
[120,]        0  0.236821990  0.133645930  0.022444817  1.26843879
[121,]        0  0.146744855  0.052687622  0.029447256  0.89323894
[122,]        0  0.077842968  0.127891750  0.014711354  0.71416451
[123,]        0  0.223389189  0.181411663  0.048342490  0.59089094
[124,]        0  0.011758736  0.293931328  0.036917562  2.87365445
[125,]        0  0.057638756  0.262690499  0.054119272  4.66983166
[126,]        0 -0.019033595  0.302711214  0.057767564  6.10251167
[127,]        0 -0.031502878  0.221783981  0.050314677  2.05150776
[128,]        0  0.053267372  0.068925682  0.042385349  1.91082180
[129,]        0 -0.057991241  0.028315757  0.040734790  1.38620499
[130,]        0 -0.036327184  0.103362962  0.036318668  0.84866029
[131,]        0 -0.068812042  0.109087403  0.020497603  0.58923088
[132,]        0 -0.075085174  0.138765553  0.023057622  0.86040295
[133,]        0 -0.053108777  0.129443014  0.023743674  0.89238510
[134,]        0 -0.109530408  0.173890428  0.025422135  0.76808692
[135,]        0  0.820570693  0.489072341  0.038025378  3.80283909
[136,]        0  0.755131402  0.480378231  0.023438901  1.83815578
[137,]        0  0.784152123  0.466333064  0.009787795  0.73929691
[138,]        0  0.770736924  0.535517543  0.010150374  0.99090286
[139,]        0  0.765962039  0.582485921  0.003677606  1.21952273
[140,]        0  0.799087764  0.590003093  0.012371284  1.41523522
[141,]        0  0.801033182  0.603842470  0.014718538  1.26282958
[142,]        0  0.655760851  0.603277797  0.013366921  1.29486993
[143,]        0  0.655626473  0.590216820  0.012908812  0.67140092
[144,]        0 -0.000931802  0.123956770 -0.009416716 17.37868387
[145,]        0  0.062881194  0.007211106 -0.009665915 41.78087232
[146,]        0  0.060060119  0.050727308  0.007171143 38.72104468
[147,]        0  0.044467119  0.036380079  0.003481140 60.80616220
[148,]        0  0.046114372  0.053310689  0.001938528 31.03505882
[149,]        0  0.160540154  0.069396180  0.012203609  8.54585817
[150,]        0  0.031013787  0.031720938  0.003055935  2.95383008
[151,]        0  0.132943141  0.012581698  0.005493957  0.36059554
[152,]        0  0.471412105  0.059070746  0.010334952  1.42581109
[153,]        0  0.479138052  0.060875122  0.013471127  1.45272493
[154,]        0  0.422477019  0.052316363  0.012458129  1.54526316
[155,]        0  0.388659477  0.052890384  0.005573127  1.95571714
[156,]        0  0.094920272  0.089369140  0.012778447 11.38579900
[157,]        0  0.118271048  0.118019690  0.018606786 11.99411451
[158,]        0  0.112967304  0.040162629  0.016856771  6.17653857
[159,]        0  0.118361014  0.101731873  0.026049746  5.51451976
[160,]        0  0.133620815  0.166899626  0.036407153  9.23376411
[161,]        0  0.088069784  0.282453608  0.030611453  9.54196168
[162,]        0  0.116470349  0.439463793  0.044199097 13.61400937
[163,]        0  0.090907488  0.382101833  0.041835183  8.40030914
[164,]        0  0.047117508  0.333423687  0.032047521  4.36771819
[165,]        0  0.096818844  0.392443561  0.035572024  4.76322219
[166,]        0 -0.209518025 -0.067392822  0.001041811  0.16838672
[167,]        0 -0.023669209  0.254000499  0.020112737  0.33192460
[168,]        0  0.083063378  0.240007756  0.004917441  0.77924814
[169,]        0  0.177073698  0.477966128  0.057321814  2.36844916
[170,]        0  0.159060297  0.210387595 -0.003819296  6.75228878
[171,]        0  0.169452935  0.278012721  0.031039004  2.34712579
[172,]        0  0.154945204  0.333207323  0.002886411  1.09408749
[173,]        0 -0.048780622  0.215153508  0.018259530  0.72343952
[174,]        0  0.055472408  0.285980563  0.006076962  0.31754091
[175,]        0  0.142383229  0.319639681  0.038277115  0.89486568
[176,]        0  0.077797895  0.345464464  0.031875647  0.72488824
[177,]        0  0.022357482  0.323707987  0.012566844  0.35923083
[178,]        0  0.346321819  0.941763870  0.015834019  3.93125929
[179,]        0  0.371809560  0.625682702  0.026887381  4.11670803
[180,]        0  0.298619068  0.598086800  0.001809750  2.89054586
[181,]        0  0.374593821  0.622298863 -0.010043981  3.12315505
[182,]        0  0.369303660  0.610048598  0.027247316  2.38288866
[183,]        0  0.379150968  0.643351880  0.014669807  3.03208857
[184,]        0  0.410197501  0.647344411  0.023170184  3.92832805
[185,]        0  0.403354158  0.650000682  0.050002996  3.75353977
[186,]        0  0.396623201  0.660019793  0.031378145  3.82157439
[187,]        0  0.404596355  0.676637410  0.021312227  3.44883813
[188,]        0  0.096942055  0.585831667  0.021951244  1.94042889
[189,]        0  0.102554188  0.670393761  0.021269146  3.90273154
[190,]        0  0.076417187  0.564216020  0.023645144  2.84388566
[191,]        0  0.080883097  0.524531395  0.016433972  2.60734449
[192,]        0  0.081513318  0.603303084  0.022487537  3.65309376
[193,]        0  0.024881995  0.610932352  0.023067898  5.39627899
[194,]        0  0.098384456  0.437738944  0.019957790  2.17333603
[195,]        0  0.019979034  0.461563686  0.020225360  2.24382732
[196,]        0  0.105557738  0.359034869  0.020744405  1.74355384
[197,]        0 -0.015178663  0.326976028  0.020274742  1.43860776
[198,]        0  0.206363998  0.079733555  0.001920620  1.39776775
[199,]        0  0.094099801  0.070677090  0.013279499  1.18434471
[200,]        0  0.066358250 -0.019835931  0.020976708  2.46798269

Ricky
  • 1
  • 1
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. What does your model fitting code look like (specifically the formula)? – MrFlick Oct 29 '21 at 22:04
  • Yes, I just added the formula. I use the createDataPartition function to split my data frame and a control given by ``` control <- trainControl(method = "boot", number = 100) ``` – Ricky Oct 29 '21 at 22:26
  • Unless you give enough code and data so that we can actually run the code ourselves so see what's going on, it's going to be very difficult to help you. – MrFlick Oct 29 '21 at 22:27
  • Sorry about that, I have added a sample of my data and more code – Ricky Oct 29 '21 at 22:42
  • The sample of 457 will not be possible with the sample, but you can use a smaller sample – Ricky Oct 29 '21 at 22:43

1 Answers1

0

The problem in this code was that the row index was in disorder, I just reseted the row index and it all worked perfectly.

Ricky
  • 1
  • 1