结果中的一个或多个因子水平没有数据:''

时间:2017-01-21 14:37:58

标签: r r-caret

我想在以下train上使用插入符data.frame函数进行逻辑回归:

lr_scaled <- structure(list(p1 = c(0.485563579637833, -0.616626170629997, 
-0.0250480996345426, -0.616626170629997, 0.208380452315413, -0.616626170629997, 
-0.333805162045523, -0.616626170629997, -0.616626170629997, -0.309228455158053, 
-0.433434503521256, 0.0734725254771786, -0.616626170629997, -0.459274544148966, 
-0.0493709436323291, -0.276140036968083, -0.414846609862374, 
-0.616626170629997, -0.616626170629997, 0.359028071432863, -0.616626170629997, 
-0.616626170629997, -0.321313571508438, 3.39691793282239, -0.100220313353603, 
0.809741618549432, -0.248731060310829, -0.616626170629997, 0.364383734647446, 
1.17539355633811, -0.616626170629997, -0.329019940413539, -0.616626170629997, 
-0.616626170629997, -0.616626170629997, -0.616626170629997, -0.616626170629997, 
-0.616626170629997, -0.616626170629997, -0.616626170629997, -0.173452332264823, 
-0.507682173468909, -0.35667186323112, -0.616626170629997, -0.550115152093123, 
-0.616626170629997, -0.616626170629997, -0.0337291299009939, 
-0.518345981156203, -0.616626170629997, -0.148297620530964, -0.574220576753038, 
-0.616626170629997, -0.616626170629997, -0.616626170629997, -0.369792808582316, 
-0.616626170629997, 0.706616690714088, -0.271886732113555, 0.691792206193782, 
-0.136396990370825, 0.0323012052030333, -0.616626170629997, -0.616626170629997, 
-0.371063469797007, 0.421656103795491, -0.616626170629997, -0.249114775847093, 
-0.616626170629997, -0.616626170629997, -0.433132175361504, 0.584781210337272, 
-0.616626170629997, -0.616626170629997, 0.643554034364543, -0.588884509295734, 
-0.243872757020996, -0.421611848245268, 1.55696906433779, 0.36120896875192, 
-0.131770296597707, -0.616626170629997, -0.252068948453334, -0.616626170629997, 
-0.459538184583675, -0.616626170629997, -0.0143558096319424, 
-0.556184923640476, -0.616626170629997, 3.60593996246077, 1.37981290707695, 
1.18054082218048, 0.467284488469581, -0.616626170629997, 0.0596958688519063, 
-0.36451582519427, 0.0672681551243149, -0.442779429829593, 0.20389524764585, 
0.567819106516067, 4.7180012472465, -0.228547441461734, -0.368894090934536, 
1.21027570136396, -0.616626170629997, -0.172511760353098, 0.201928057182541, 
-0.0826981139144883, 0.68775519552982, -0.616626170629997, -0.616626170629997, 
1.79262370183792, -0.510869098710685, -0.274822498297383, -0.254274969303695, 
0.221310337896831, -0.616626170629997, 0.106682905037584, -0.361493000977647, 
-0.389775949209029, 3.10495549581137, -0.0495308377138273, -0.518954408350508, 
-0.522496631739946, 0.518211572110217, -0.386411699775549, 0.212109438699833, 
-0.616626170629997, -0.417490038640113, 0.00993457046836585, 
-0.0904890659922932, -0.316815364511235, 2.37403635847706, -0.552260686795496, 
-0.510190319803746, -0.311442315425675, 0.936863710350044, -0.616626170629997, 
-0.616626170629997, 0.915273643709255, -0.616626170629997, -0.412402386974972, 
0.46054094978938, 0.326407200403359, 1.8702332130044, 0.527478245786128, 
0.695842175122785, -0.616626170629997, 0.37792655799122, -0.252912078451454, 
0.789549810404364, -0.454580601740622, 0.127780313866469, 0.289410000926002, 
-0.616626170629997, -0.166858279169853), p2 = c(-0.127972374792073, 
-0.463084062742088, -0.257371937497701, -0.463084062742088, -0.161151749885253, 
-0.463084062742088, -0.0955281117827976, -0.463084062742088, 
-0.463084062742088, -0.141244124753178, -0.0649315622688245, 
-0.317094812567496, -0.463084062742088, -0.0028112951587533, 
0.0213348128293369, -0.217556687342361, 0.0932168362935861, -0.463084062742088, 
-0.463084062742088, -0.36374461651055, -0.463084062742088, -0.463084062742088, 
-0.184377312412396, -0.413315000182598, -0.134608249852906, -0.0715674372779065, 
-0.300829633909097, -0.463084062742088, -0.413414339626319, 0.0578321254290484, 
-0.463084062742088, -0.164469687389131, -0.463084062742088, -0.463084062742088, 
-0.463084062742088, -0.463084062742088, -0.463084062742088, -0.463084062742088, 
-0.463084062742088, -0.463084062742088, -0.280597500028824, -0.0359244437946737, 
-0.300505124996358, -0.463084062742088, -0.257371937444623, -0.463084062742088, 
-0.463084062742088, -0.283915437532702, -0.125329945534691, -0.463084062742088, 
-0.104746812318009, -0.0212486963897363, -0.463084062742088, 
-0.463084062742088, -0.463084062742088, -0.290551312428993, -0.463084062742088, 
-0.0715674371690965, -0.254053999942072, 0.223729000679983, -0.0782033122869885, 
-0.17774143740066, -0.463084062742088, -0.463084062742088, -0.171105562396886, 
2.11848323981842, -0.463084062742088, 6.33598578814306, -0.463084062742088, 
-0.463084062742088, 0.14288655935495, 0.0269905386953153, -0.463084062742088, 
-0.463084062742088, -0.356910062562275, -0.360228000119231, -0.240782249869504, 
-0.184377312412396, 0.46064640413971, -0.185649656346226, -0.179344328967404, 
-0.463084062742088, 0.00981549374942071, -0.463084062742088, 
-0.302298213499996, -0.463084062742088, -0.25500825800026, -0.0658484353378401, 
-0.463084062742088, 0.611974262204094, -0.132054373248721, 0.0035101662060567, 
-0.0973750724508974, -0.463084062742088, -0.377962142532852, 
-0.103680399827066, -0.264466249012098, -0.26446624906783, -0.409488779644583, 
-0.412641443387735, 0.621432253326069, -0.42840476194161, -0.409488779698988, 
0.0066628300022873, -0.463084062742088, -0.25816092163593, 0.069716104059879, 
-0.302298213557055, 0.296707891211525, -0.463084062742088, -0.463084062742088, 
-0.141512364425101, 0.139074705768318, -0.21717629345663, -0.295992886178233, 
-0.2802295675673, -0.463084062742088, -0.346435505532586, -0.103680399828392, 
0.0476474581802603, -0.362198824033382, -0.374809478844105, -0.135207036994528, 
-0.0690010990823196, -0.141512364369369, -0.242397603023669, 
-0.2329396119561, -0.463084062742088, -0.135207036937469, -0.128901709611725, 
-0.198260311156949, -0.428404761942937, -0.463084062742088, -0.327519523288637, 
-0.32436685954283, -0.412641443387735, -0.182496992603074, -0.463084062742088, 
-0.463084062742088, -0.201412974847023, -0.463084062742088, -0.242397603024996, 
-0.157275682925898, 0.277791908910516, -0.0847644175831163, -0.324366859544157, 
0.27463924522575, -0.463084062742088, -0.299145549922713, -0.362198824032056, 
0.167448679136896, -0.226634284469794, -0.0626957715933606, 0.00981549369236181, 
-0.463084062742088, -0.381114806333064), p3 = c(-0.31836604331677, 
-0.793024559634861, -0.160146537877407, -0.793024559634861, -0.00192703243804359, 
-0.793024559634861, 0.63095098931941, -0.793024559634861, -0.793024559634861, 
0.789170494758773, 0.314511978440683, -0.160146537877407, -0.793024559634861, 
0.63095098931941, 0.472731483880047, -0.00192703243804359, 1.58026802195559, 
-0.793024559634861, -0.793024559634861, -0.476585548756134, -0.793024559634861, 
-0.793024559634861, 0.472731483880047, -0.634805054195497, 0.15629247300132, 
0.15629247300132, -0.160146537877407, -0.793024559634861, -0.634805054195497, 
-0.160146537877407, -0.793024559634861, 0.15629247300132, -0.793024559634861, 
-0.793024559634861, -0.793024559634861, -0.793024559634861, -0.793024559634861, 
-0.793024559634861, -0.793024559634861, -0.793024559634861, -0.00192703243804359, 
0.947390000198137, 0.314511978440683, -0.793024559634861, -0.160146537877407, 
-0.793024559634861, -0.793024559634861, -0.160146537877407, 0.314511978440683, 
-0.793024559634861, -0.31836604331677, 0.314511978440683, -0.793024559634861, 
-0.793024559634861, -0.793024559634861, -0.476585548756134, -0.793024559634861, 
-0.00192703243804359, 0.314511978440683, 0.472731483880047, 0.15629247300132, 
-0.00192703243804359, -0.793024559634861, -0.793024559634861, 
-0.00192703243804359, 3.47890208722795, -0.793024559634861, -0.634805054195497, 
-0.793024559634861, -0.793024559634861, 1.26382901107686, 0.472731483880047, 
-0.793024559634861, -0.793024559634861, -0.634805054195497, -0.476585548756134, 
-0.160146537877407, -0.160146537877407, -0.160146537877407, -0.160146537877407, 
0.15629247300132, -0.793024559634861, 0.789170494758773, -0.793024559634861, 
-0.160146537877407, -0.793024559634861, -0.160146537877407, 0.314511978440683, 
-0.793024559634861, -0.476585548756134, -0.31836604331677, -0.31836604331677, 
-0.00192703243804359, -0.793024559634861, -0.160146537877407, 
1.1056095056375, -0.160146537877407, 0.314511978440683, -0.476585548756134, 
-0.634805054195497, -0.634805054195497, -0.634805054195497, -0.634805054195497, 
0.15629247300132, -0.793024559634861, -0.31836604331677, 0.947390000198137, 
-0.476585548756134, -0.160146537877407, -0.793024559634861, -0.793024559634861, 
-0.160146537877407, 0.947390000198137, -0.31836604331677, -0.160146537877407, 
-0.160146537877407, -0.793024559634861, -0.31836604331677, 0.314511978440683, 
0.947390000198137, -0.476585548756134, -0.634805054195497, 0.314511978440683, 
0.472731483880047, -0.31836604331677, -0.160146537877407, -0.00192703243804359, 
-0.793024559634861, 0.63095098931941, 0.15629247300132, -0.31836604331677, 
-0.634805054195497, -0.634805054195497, -0.31836604331677, -0.160146537877407, 
-0.634805054195497, -0.160146537877407, -0.793024559634861, -0.793024559634861, 
-0.31836604331677, -0.793024559634861, 0.314511978440683, -0.00192703243804359, 
-0.31836604331677, -0.160146537877407, -0.31836604331677, 0.15629247300132, 
-0.793024559634861, -0.160146537877407, -0.476585548756134, -0.00192703243804359, 
0.15629247300132, 0.314511978440683, -0.00192703243804359, -0.793024559634861, 
-0.476585548756134), p4 = c(1.71708492765528, 0.0418364019987543, 
-1.54364479747194, 0.0418364019987543, -1.31858028479049, 0.0418364019987543, 
0.0412078049789405, 0.0418364019987543, 0.0418364019987543, 0.947112571343273, 
-1.405989106953, -0.063399428938362, 0.0418364019987543, -0.838743846411565, 
-1.23320626519838, -0.298752172777495, 0.576941140139043, 0.0418364019987543, 
0.0418364019987543, -2.2237207505851, 0.0418364019987543, 0.0418364019987543, 
0.0507592720170419, 0.0418364019987543, -1.77897663531067, -1.23640554789896, 
-1.42113156646985, 0.0418364019987543, 0.0418364019987543, -0.975270869295795, 
0.0418364019987543, -0.562498575396531, 0.0418364019987543, 0.0418364019987543, 
0.0418364019987543, 0.0418364019987543, 0.0418364019987543, 0.0418364019987543, 
0.0418364019987543, 0.0418364019987543, -0.582770878196485, 1.90859926878591, 
1.00127892144903, 0.0418364019987543, 0.530896043625171, 0.0418364019987543, 
0.0418364019987543, 0.654890323741625, 1.32294000537731, 0.0418364019987543, 
1.82944910227629, -1.69469721064105, 0.0418364019987543, 0.0418364019987543, 
0.0418364019987543, -2.2237207505851, 0.0418364019987543, 0.442744795433564, 
0.645688823840173, 0.861533115106296, 0.789658023656087, 0.220217531030149, 
0.0418364019987543, 0.0418364019987543, -1.38435902525416, -0.940423197145085, 
0.0418364019987543, 0.0418364019987543, 0.0418364019987543, 0.0418364019987543, 
0.62309596028077, -1.16112246575662, 0.0418364019987543, 0.0418364019987543, 
0.0418364019987543, -2.2237207505851, 0.281627800060233, 1.57870489318466, 
1.56154926930941, -1.79789496148549, 0.644807405661516, 0.0418364019987543, 
-0.248862309492805, 0.0418364019987543, -0.326901604444684, 0.0418364019987543, 
-2.15102367207365, 1.11755364393315, 0.0418364019987543, 2.30739355458261, 
2.25118092214019, -1.95290762313714, -1.07741326252174, 0.0418364019987543, 
1.68668534289401, 0.0248365417557386, 1.53260839613903, 1.06337547968499, 
-2.2237207505851, 0.0418364019987543, 0.0418364019987543, 0.0418364019987543, 
0.0418364019987543, 0.56031019485015, 0.0418364019987543, -1.55546263278715, 
-0.803784038158336, -2.2237207505851, 0.0863774299376856, 0.0418364019987543, 
0.0418364019987543, -0.0751703448797162, -0.553054732433199, 
0.256848500466934, 1.95012518189416, 2.08018951667591, 0.0418364019987543, 
1.57443273381421, -0.88066748656914, -0.891411205187825, 2.30739355458261, 
0.0418364019987543, -0.540101342331963, 0.0627326236643338, 2.0293101597303, 
-1.08955231239295, -1.72911733412937, 0.0418364019987543, 1.36077350909574, 
-0.74048614166489, 2.2444480342536, 0.0418364019987543, 0.0418364019987543, 
0.387060805855782, 1.72745729567544, 0.0418364019987543, -1.37003770535551, 
0.0418364019987543, 0.0418364019987543, -1.50649100383624, 0.0418364019987543, 
-0.0296881380413355, 0.926560930336045, 0.660515554915189, -2.03184580528568, 
-2.05915796777212, 1.74286000880681, 0.0418364019987543, 1.06996516565913, 
2.30739355458261, -0.872910436402336, -0.949825228838339, 1.46571652261657, 
1.34169527041468, 0.0418364019987543, -2.2237207505851), Class = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("FALSE", 
"TRUE"), class = "factor")), .Names = c("p1", "p2", "p3", "p4", 
"Class"), row.names = c(NA, -156L), class = "data.frame")

在这个数据框架上,我想使用插入符号列车功能:

lr_scaled <- train(lr_scaled$Class, lr_scaled[,1:4], method='glm',metric = 'Accuracy', trControl= trainControl(method='boot', savePredictions = TRUE))

但是,我总是收到此错误消息:

Error in train.default(lr_scaled$Class, lr_scaled[, 1:4], method = "glm",  : 
  One or more factor levels in the outcome has no data: '

为什么我的列车数据集不包含TRUEFALSE两个因素级别中的任何一个的数据?

1 个答案:

答案 0 :(得分:1)

如果您不使用公式表示法,train的第一个参数为x,第二个参数为y。 因此,您需要切换参数:

res <- train(lr_scaled[,1:4], lr_scaled$Class, method='glm',metric = 'Accuracy', trControl= trainControl(method='boot', savePredictions = TRUE))

此外,使用train(...)的结果覆盖您的数据集并不是一个好主意。