错误:glm.fit:出现数字为0或1的拟合概率

时间:2019-04-28 15:54:18

标签: r logistic-regression

我对R很陌生。我正在尝试训练我的数据以进行逻辑回归。尝试构建逻辑回归模型时,我仅使用数值,但不断出现以下错误。

警告信息: glm.fit:发生了数值为0或1的拟合概率

就像我说的那样,很新。

这是我迄今为止尝试过的。

AllMortgage$default <- as.factor(AllMortgage$default)
smp_size <- floor(0.70 * nrow(AllMortgage))
set.seed(12345)
trainindex <- sample(seq_len(nrow(AllMortgage)), size = smp_size)
training <- AllMortgage[trainindex,]
validation <- AllMortgage[-trainindex,]

str(training)

mylogit<-glm(default ~ credit_score + cltv + dti_ratio + upb + ltv + interest_rate + loan_term + num_borrowers,
             data=training, family=binomial)
summary(mylogit)

如果我继续构建,则会出现以下错误:

mylogit.probs<-predict(mylogit,validation,type="response")

警告信息: 'newdata'有62537行,但找到的变量有208454行

以下一些示例数据。大约有208563行。

ID  loan_seq_number delincuency_status  credit_score    fthb    mi_perc units   occupancy   cltv    dti_ratio   upb ltv interest_rate   channel ppm state   property_type   postal_code loan_purpose    loan_term   num_borrowers   super_conforming    default
1   F108Q4000001    0   736 N   0   1   P   80  20  172000  80  6.25    R   N   MI  SF  49700   C   360 2   N   0
2   F108Q4000002    0   797 N   0   1   P   62  31  215000  62  5.625   C   N   ME  SF  4000    N   360 1   N   0
3   F108Q4000003    0   804 N   0   1   P   91  53  220000  80  5.5 R   N   MN  SF  55000   N   360 2   N   0
4   F108Q4000004    0   803 N   0   1   P   86  46  288000  76  5.5 R   N   IL  SF  61300   N   360 1   N   0
5   F108Q4000005    0   803 N   0   1   P   36  17  58000   36  4.875   R   N   WI  SF  54800   N   360 1   N   0
6   F108Q4000006    4   666 N   0   1   P   53  64  125000  53  6.625   R   N   GA  SF  30200   N   360 2   N   0
7   F108Q4000007    1   677 N   0   1   P   34  19  55000   34  5.25    R   N   KY  SF  42300   N   360 2   N   0
8   F108Q4000008    0   755 N   0   1   P   73  35  410000  73  5.875   R   N   CO  SF  81200   N   360 2   N   0
9   F108Q4000009    0   768 N   0   1   P   80  32  214000  80  5.5 R   N   MI  SF  49700   N   360 2   N   0
10  F108Q4000010    0   776 N   0   1   P   49  44  164000  49  6   R   N   MO  SF  63300   N   360 2   N   0
11  F108Q4000011    1   660 N   0   1   P   80  27  53000   80  7.125   R   N   MI  SF  49700   N   360 2   N   0
12  F108Q4000012    0   813 N   25  1   P   86  27  417000  86  5.875   R   N   IL  SF  62400   N   360 2   N   0
13  F108Q4000013    0   773 N   0   1   P   75  30  98000   75  5.875   R   N   MN  SF  56000   N   360 1   N   0
14  F108Q4000014    0   735 N   0   1   P   80  15  240000  80  6.5 R   N   IL  SF  61400   C   360 2   N   0
15  F108Q4000015    2   610 N   0   1   P   80  48  220000  80  8.25    R   Y   IL  SF  62600   C   360 2   N   0
16  F108Q4000016    0   688 N   30  1   P   95  42  71000   95  6.25    R   N   IA  SF  50500   P   360 1   N   0
17  F108Q4000017    0   737 N   0   1   P   79  28  357000  79  6.125   R   N   KS  SF  66500   N   360 2   N   0
18  F108Q4000018    0   672 N   0   1   P   73  40  60000   73  6.875   R   N   KS  SF  66500   N   360 1   N   0
19  F108Q4000019    0   748 Y   0   1   P   80  28  140000  80  6.375   R   N   MI  SF  48400   P   360 2   N   0
20  F108Q4000020    0   801 N   0   1   P   67  34  417000  67  6.25    R   N   MD  SF  20600   C   360 1   N   0
21  F108Q4000021    0   785 N   0   1   P   79  25  55000   79  6.75    R   N   NY  SF  14800   C   360 1   N   0
22  F108Q4000022    2   674 N   0   1   P   80  38  300000  80  6.875   R   N   CO  SF  81200   N   360 2   N   0
23  F108Q4000023    0   745 N   0   1   P   60  33  160000  60  5.5 R   N   WY  SF  82400   N   360 2   N   0
24  F108Q4000024    0   753 N   0   1   I   68  58  68000   68  7.375   R   N   MO  PU  64100   C   360 1   N   0
25  F108Q4000025    0   669 N   0   1   P   72  15  80000   72  5.875   R   Y   GA  SF  30200   C   360 1   N   0
26  F108Q4000026    0   772 9   0   1   P   79  38  417000  79  5.875   R   N   IL  SF  62400   N   360 2   N   0
27  F108Q4000027    0   706 N   0   1   P   56  40  70000   56  7.75    R   N   IL  MH  62800   P   360 1   N   0
28  F108Q4000028    0   754 N   0   1   P   42  43  61000   42  6.625   R   N   KY  SF  42100   N   360 2   N   0
29  F108Q4000029    0   751 N   0   1   P   31  39  23000   31  6.625   R   N   KY  SF  42100   P   360 1   N   0
30  F108Q4000030    0   771 N   30  1   P   92  37  210000  92  6.5 R   N   TX  SF  77600   N   360 2   N   0
31  F108Q4000031    0   749 N   0   1   P   76  30  140000  76  6.75    R   N   KS  SF  66700   N   360 2   N   0
32  F108Q4000032    0   791 N   12  1   S   84  29  100000  84  6.625   R   N   MI  SF  49600   P   360 1   N   0
33  F108Q4000033    0   743 Y   30  1   P   95  43  118000  95  6.125   R   N   GA  SF  30200   P   360 1   N   0
34  F108Q4000034    0   794 N   0   1   P   80  28  106000  80  6.625   R   N   NC  SF  27200   C   360 1   N   0
35  F108Q4000035    3   771 N   0   4   I   65  50  285000  65  6.625   R   Y   CT  SF  6600    N   360 1   N   0
36  F108Q4000036    0   799 N   0   2   P   29  57  417000  29  6.125   R   N   CA  SF  94100   N   360 2   N   0
37  F108Q4000037    29  706 Y   0   1   P   80  48  73000   80  6.625   C   N   ME  SF  4400    P   360 1   N   1
38  F108Q4000038    0   806 N   0   1   P   60  22  80000   60  6.375   R   N   WA  CO  98800   C   360 1   N   0
39  F108Q4000039    0   657 N   0   1   P   78  15  132000  78  6.5 R   N   WA  SF  98800   P   360 2   N   0
40  F108Q4000040    0   756 N   0   1   P   80  21  176000  80  6.25    R   N   MN  SF  56000   P   360 2   N   0
41  F108Q4000041    0   754 N   30  1   P   95  44  285000  95  6.625   R   N   NY  SF  12100   P   360 2   N   0
42  F108Q4000042    0   785 N   0   1   P   47  6   150000  47  6.5 R   N   CO  SF  81200   C   360 1   N   0
43  F108Q4000043    0   694 N   0   1   P   67  38  138000  67  6.75    R   N   MO  SF  63700   N   360 1   N   0
44  F108Q4000044    0   688 N   30  1   P   95  42  297000  95  6.5 R   Y   AK  SF  99900   P   360 1   N   0
45  F108Q4000045    0   708 N   0   1   P   80  34  116000  80  6.875   R   N   KS  SF  66500   C   360 2   N   0
46  F108Q4000046    0   775 N   0   1   P   74  39  251000  74  6.625   R   Y   NC  SF  27800   C   360 1   N   0
47  F108Q4000047    1   687 N   0   1   P   80  57  186000  80  6.75    R   Y   OH  SF  43500   N   360 1   N   0
48  F108Q4000048    0   804 N   0   1   S   49  11  209000  49  6.75    R   N   NJ  CO  8200    P   360 2   N   0
49  F108Q4000049    0   809 N   0   1   P   37  30  100000  37  6.625   C   N   OR  SF  97200   C   360 1   N   0
50  F108Q4000050    1   734 N   0   1   P   80  20  34000   80  6.625   R   N   SC  SF  29300   P   360 1   N   0
51  F108Q4000051    0   649 Y   30  1   P   95  27  87000   95  6.625   R   N   PA  SF  16900   P   360 2   N   0
52  F108Q4000052    0   772 N   0   1   P   73  16  78000   73  6.5 R   N   KY  SF  42500   C   360 1   N   0
53  F108Q4000053    0   799 N   0   1   P   54  21  105000  54  6.875   R   N   PA  SF  18000   C   360 1   N   0
54  F108Q4000054    0   677 N   0   1   P   80  35  191000  80  6.5 R   N   NY  SF  14000   P   360 2   N   0
55  F108Q4000055    0   775 N   0   1   I   80  31  186000  80  6.875   R   Y   CA  SF  93200   C   360 2   N   0
56  F108Q4000056    0   768 N   30  1   P   92  20  290000  92  6.25    R   N   KS  SF  66500   N   360 2   N   0
57  F108Q4000057    19  685 N   0   1   P   80  35  144000  80  6.5 R   N   NV  SF  89100   C   360 1   N   1
58  F108Q4000058    0   623 N   0   1   P   59  38  56000   59  6.5 R   N   KY  SF  42100   C   360 1   N   0
59  F108Q4000059    0   628 N   0   1   P   75  43  338000  75  7   C   Y   KS  SF  66400   C   360 2   N   0
60  F108Q4000060    0   710 N   0   1   P   58  25  107000  58  6.5 R   N   ND  SF  58800   C   360 1   N   0
61  F108Q4000061    0   740 Y   30  1   P   95  47  349000  95  6.625   R   N   NY  SF  12500   P   360 1   N   0
62  F108Q4000062    0   767 N   0   1   P   66  44  294000  66  6.25    R   N   WA  SF  98900   N   360 2   N   0
63  F108Q4000063    0   745 N   0   1   P   79  20  65000   79  7.25    R   N   MO  SF  64000   C   360 2   N   0
64  F108Q4000064    0   773 N   25  1   P   87  50  154000  87  6.25    R   N   ND  SF  58400   P   360 1   N   0
65  F108Q4000065    0   732 N   30  1   P   90  28  149000  90  6.75    R   N   ME  SF  4600    P   360 1   N   0
66  F108Q4000066    0   702 N   0   1   P   73  38  133000  73  6.375   R   N   MI  SF  49300   C   360 2   N   0
67  F108Q4000067    0   780 N   0   1   P   80  39  135000  80  6.375   R   N   ND  SF  58800   P   360 2   N   0
68  F108Q4000068    0   780 Y   0   1   P   76  22  125000  76  6.75    R   N   IL  SF  61500   P   360 2   N   0
69  F108Q4000069    0   746 N   0   1   P   21  17  60000   21  6.625   R   N   NC  SF  28600   C   360 2   N   0
70  F108Q4000070    0   678 N   0   1   P   94  46  168000  80  6.375   R   N   AK  SF  99500   N   360 1   N   0
71  F108Q4000071    0   746 N   30  1   S   95  25  76000   95  6.625   R   N   MI  SF  49400   P   360 2   N   0
72  F108Q4000072    0   807 N   0   1   P   73  49  145000  73  6.625   R   N   MO  SF  65700   C   360 1   N   0
73  F108Q4000073    4   746 N   0   1   P   74  29  150000  74  6.5 R   N   CA  SF  96000   C   360 1   N   0
74  F108Q4000074    0   734 Y   30  1   P   95  18  109000  95  6.25    R   N   GA  SF  30200   P   360 2   N   0
75  F108Q4000075    0   692 N   25  1   P   90  28  198000  90  6.5 C   N   WI  SF  53900   P   360 2   N   0
76  F108Q4000076    0   788 Y   0   1   P   80  27  30000   80  6.375   R   N   KY  SF  42100   P   360 2   N   0
77  F108Q4000077    0   657 N   0   1   P   18  38  225000  18  6.125   R   N   CA  SF  93400   C   360 2   N   0
78  F108Q4000078    0   788 9   0   1   P   69  26  130000  69  6.625   R   N   NY  SF  12700   C   360 1   N   0
79  F108Q4000079    0   662 N   0   1   P   80  55  183000  80  6.75    R   Y   ME  SF  4900    P   360 1   N   0
80  F108Q4000080    0   717 Y   30  1   P   95  41  126000  95  6.5 R   N   TX  SF  75600   P   360 1   N   0
81  F108Q4000081    0   796 N   0   1   P   55  39  90000   55  6.25    R   N   NC  SF  27400   C   360 1   N   0
82  F108Q4000082    0   710 N   0   1   P   48  45  120000  48  6.625   R   N   WA  SF  98800   C   360 1   N   0
83  F108Q4000083    0   741 N   0   1   P   90  20  183000  80  5.875   C   N   VT  SF  5400    P   360 2   N   0
84  F108Q4000084    0   807 N   0   1   P   33  8   200000  17  6.125   R   N   IL  SF  60500   N   360 2   N   0
85  F108Q4000085    0   727 N   0   1   S   80  39  88000   80  6.375   R   N   KY  SF  42500   P   360 2   N   0
86  F108Q4000086    0   768 N   0   1   P   69  31  197000  69  5.75    R   N   CO  PU  81200   N   360 2   N   0
87  F108Q4000087    15  626 Y   30  1   P   95  21  84000   95  8.25    R   Y   IA  SF  52600   P   360 1   N   1
88  F108Q4000088    0   793 N   30  1   P   92  29  218000  92  6.25    R   N   TX  SF  77600   N   360 2   N   0
89  F108Q4000089    0   684 N   0   1   P   70  30  118000  70  6.5 R   N   MI  SF  49300   C   360 2   N   0
90  F108Q4000090    0   756 N   0   1   P   80  29  73000   80  6.375   R   N   MI  SF  48600   C   360 2   N   0
91  F108Q4000091    2   652 N   0   1   P   79  40  251000  79  6.75    R   N   TX  SF  77600   N   360 2   N   0
92  F108Q4000092    0   688 N   0   1   P   72  48  150000  72  6.5 R   N   KY  SF  42100   P   360 1   N   0
93  F108Q4000093    0   759 N   12  1   P   85  43  86000   85  6.375   R   N   NY  SF  14800   P   360 1   N   0
94  F108Q4000094    0   756 N   0   1   P   80  35  260000  80  5.875   R   N   ND  SF  58500   N   360 2   N   0
95  F108Q4000095    0   746 N   0   2   P   33  51  363000  33  6.5 R   N   NY  SF  11200   C   360 2   N   0
96  F108Q4000096    0   719 N   0   1   P   80  22  152000  80  5.875   R   N   OK  SF  74800   P   360 2   N   0
97  F108Q4000097    0   758 N   30  1   P   94  32  264000  94  6.375   R   N   IL  SF  62200   P   360 2   N   0
98  F108Q4000098    1   743 N   0   1   P   80  48  154000  80  6.25    R   N   NC  SF  27300   C   360 2   N   0
99  F108Q4000099    0   678 N   0   1   P   80  31  109000  80  6.375   R   N   MI  SF  49600   N   360 2   N   0
100 F108Q4000100    0   718 N   0   1   P   80  38  120000  80  6.25    R   N   NC  SF  27500   N   360 1   N   0
101 F108Q4000101    1   665 N   0   1   P   47  23  243000  47  6.375   R   Y   IL  SF  60400   C   360 1   N   0
102 F108Q4000102    0   761 Y   0   1   P   83  12  139000  80  6   R   N   MN  SF  56500   P   360 2   N   0
103 F108Q4000103    0   794 N   0   1   I   80  28  85000   80  6.375   R   N   IA  SF  52600   P   360 2   N   0
104 F108Q4000104    0   747 N   30  1   P   95  50  206000  95  6   R   N   ND  SF  58500   P   360 2   N   0
105 F108Q4000105    1   702 N   0   1   P   80  33  348000  80  6.375   R   N   NY  SF  12200   P   360 1   N   0
106 F108Q4000106    0   780 N   0   1   P   44  23  417000  44  6.25    R   N   MN  SF  55100   N   360 2   N   0
107 F108Q4000107    1   648 N   0   1   P   55  24  355000  55  6.125   R   N   WA  SF  98800   C   360 2   N   0
108 F108Q4000108    0   732 N   0   1   P   58  11  300000  58  5.75    R   N   NY  SF  14200   P   360 2   N   0
109 F108Q4000109    0   788 N   0   1   P   65  24  169000  65  5.875   R   Y   FL  PU  32300   P   360 1   N   0
110 F108Q4000110    0   787 N   0   1   S   49  26  130000  49  6   R   N   IA  SF  52100   C   360 2   N   0
111 F108Q4000111    2   696 N   0   1   P   80  32  343000  80  6.375   R   Y   UT  SF  84000   N   360 1   N   0
112 F108Q4000112    0   789 N   0   1   P   20  47  61000   20  6   R   Y   OH  SF  45000   N   360 2   N   0
113 F108Q4000113    0   774 N   25  1   P   90  48  284000  90  7   R   N   FL  PU  33000   P   360 1   N   0
114 F108Q4000114    0   645 N   0   1   P   58  38  220000  58  6.375   R   Y   AK  SF  99900   N   360 2   N   0
115 F108Q4000115    0   766 N   0   1   P   71  63  88000   71  6.25    R   N   MI  SF  48000   N   360 2   N   0
116 F108Q4000116    0   792 Y   0   1   P   77  22  130000  77  5.875   R   N   ND  SF  58800   P   360 2   N   0
117 F108Q4000117    0   812 N   0   1   P   78  42  255000  78  5.625   R   N   KY  SF  40300   C   360 2   N   0
118 F108Q4000118    0   697 N   0   1   P   75  19  94000   75  6   R   N   MI  SF  49000   C   360 2   N   0
119 F108Q4000119    1   605 N   0   1   S   70  35  243000  70  7.875   R   N   FL  CO  33000   C   360 1   N   0
120 F108Q4000120    0   709 Y   12  1   P   85  61  378000  85  6.625   R   N   NY  CO  10000   P   360 1   N   0
121 F108Q4000121    0   766 N   0   1   P   80  29  236000  80  6   R   N   CA  SF  96000   P   360 2   N   0
122 F108Q4000122    0   801 N   30  1   P   95  44  99000   95  6   R   N   OK  SF  73100   P   360 1   N   0
123 F108Q4000123    0   753 N   0   1   P   69  24  183000  69  6   R   N   NE  SF  68000   P   360 2   N   0
124 F108Q4000124    0   749 9   0   1   P   80  18  416000  80  6   R   N   IL  SF  61700   N   360 2   N   0
125 F108Q4000125    0   689 N   0   1   P   61  53  350000  61  6   R   N   IN  SF  46800   N   360 1   N   0
126 F108Q4000126    0   763 N   0   1   P   57  36  136000  57  5.875   R   N   IA  SF  52100   C   360 2   N   0
127 F108Q4000127    0   816 N   0   1   P   57  28  162000  57  5.5 R   N   CT  SF  6400    C   360 1   N   0
128 F108Q4000128    0   789 N   0   1   P   89  23  356000  80  5.875   R   N   IL  SF  60500   N   360 2   N   0
129 F108Q4000129    0   720 N   0   1   P   80  35  248000  80  6   R   N   VT  PU  5400    P   360 2   N   0
130 F108Q4000130    0   723 N   0   1   P   25  49  95000   25  5.75    R   Y   CO  SF  81100   N   360 2   N   0
131 F108Q4000131    0   710 Y   25  1   P   90  25  361000  90  6   R   N   NJ  SF  7700    P   360 1   N   0
132 F108Q4000132    0   809 N   0   1   P   54  39  105000  54  6.125   C   N   OR  SF  97200   C   360 1   N   0
133 F108Q4000133    0   646 N   0   1   P   66  24  205000  66  6   R   N   MA  SF  2100    C   360 2   N   0
134 F108Q4000134    0   776 Y   0   1   P   77  43  400000  77  5.875   R   N   CA  SF  92600   P   360 1   N   0
135 F108Q4000135    0   734 Y   30  1   P   95  35  142000  95  6   R   N   MN  SF  55300   P   360 1   N   0
136 F108Q4000136    0   797 N   0   1   P   90  33  140000  80  5.875   R   N   WI  SF  53700   N   360 2   N   0
137 F108Q4000137    0   528 N   0   1   P   63  21  67000   63  6.375   R   N   NY  SF  14800   P   360 2   N   0
138 F108Q4000138    0   790 N   0   1   P   80  55  204000  80  5.5 R   N   NY  SF  14800   P   360 2   N   0
139 F108Q4000139    0   757 N   0   1   P   61  58  475000  50  5.5 R   N   HI  SF  96800   N   360 2   N   0
140 F108Q4000140    0   699 Y   0   2   P   60  50  150000  60  6.5 R   N   MA  SF  2100    P   360 1   N   0
141 F108Q4000141    0   812 N   0   1   P   22  9   70000   22  5.875   R   N   MO  SF  63100   N   360 2   N   0
142 F108Q4000142    0   803 N   0   1   S   42  15  417000  42  5.875   R   N   MN  SF  55700   N   360 2   N   0
143 F108Q4000143    0   788 N   0   1   P   81  55  417000  44  6   R   N   KY  SF  40300   N   360 2   N   0
144 F108Q4000144    1   664 N   0   1   P   58  46  385000  58  6.125   R   N   NJ  SF  7600    C   360 1   N   0
145 F108Q4000145    0   768 N   0   1   P   80  47  319000  80  5.875   R   N   WI  SF  54000   N   360 2   N   0
146 F108Q4000146    0   808 N   0   1   P   44  29  83000   44  6.25    R   N   NY  SF  12500   C   360 1   N   0
147 F108Q4000147    0   764 N   0   1   P   80  22  154000  80  6   R   N   MI  SF  48600   P   360 2   N   0
148 F108Q4000148    0   798 N   0   1   P   73  20  255000  73  5.875   R   N   MN  SF  56600   N   360 2   N   0
149 F108Q4000149    0   816 N   0   1   P   33  15  48000   33  6   R   Y   IA  SF  50000   P   360 2   N   0
150 F108Q4000150    0   801 N   0   1   P   80  19  156000  80  6   R   N   NC  SF  28600   C   360 2   N   0
151 F108Q4000151    0   747 N   0   1   P   62  35  309000  62  5.5 R   N   WA  SF  98800   N   360 2   N   0
152 F108Q4000152    0   701 N   0   1   P   22  38  30000   22  6   R   N   IN  SF  46700   N   360 1   N   0
153 F108Q4000153    0   672 N   30  1   P   95  45  213000  95  6.25    R   Y   KY  SF  41200   P   360 1   N   0
154 F108Q4000154    0   691 N   0   1   P   71  51  165000  71  5.75    R   N   CO  SF  81200   N   360 2   N   0
155 F108Q4000155    0   766 N   0   1   P   69  47  175000  69  5.75    R   N   IL  SF  62400   N   360 1   N   0
156 F108Q4000156    0   789 N   0   1   P   80  45  120000  80  6   R   N   OH  SF  43300   P   360 2   N   0
157 F108Q4000157    0   742 N   0   1   P   93  27  299000  78  6.625   C   N   IN  SF  47900   N   360 2   N   0
158 F108Q4000158    0   733 N   0   1   P   79  40  173000  79  5.5 R   N   TN  SF  37100   N   360 2   N   0
159 F108Q4000159    0   756 N   0   1   P   66  28  150000  66  5.625   C   N   SC  SF  29000   C   360 2   N   0
160 F108Q4000160    0   653 N   0   1   P   80  48  142000  80  6   C   N   WI  SF  54400   N   360 2   N   0
161 F108Q4000161    0   707 N   0   1   P   80  31  156000  80  6.625   R   N   MD  SF  21900   P   360 2   N   0
162 F108Q4000162    0   773 9   0   1   P   63  33  179000  63  5.875   R   N   MI  SF  48000   N   324 2   N   0
163 F108Q4000163    0   696 N   0   1   P   80  33  92000   80  5.875   R   N   ME  SF  4600    P   360 2   N   0
164 F108Q4000164    0   678 N   0   1   P   90  30  344000  80  7   R   Y   MN  SF  55800   C   360 2   N   0
165 F108Q4000165    0   762 N   0   1   P   80  35  184000  80  5.875   R   N   KS  SF  67400   N   360 2   N   0
166 F108Q4000166    0   760 N   0   1   I   80  35  96000   80  6.875   R   N   KS  SF  67400   C   360 2   N   0
167 F108Q4000167    0   758 9   0   1   I   80  19  144000  80  5.875   R   N   OH  SF  45200   N   360 2   N   0
168 F108Q4000168    0   790 N   0   1   P   67  41  32000   67  6.25    R   N   KY  SF  42100   N   360 1   N   0
169 F108Q4000169    0   808 N   0   1   P   69  40  75000   69  5.875   R   N   MI  SF  49000   C   360 1   N   0
170 F108Q4000170    1   654 N   0   1   P   54  27  85000   54  6.375   R   N   MO  SF  65700   P   360 1   N   0
171 F108Q4000171    0   793 N   0   1   P   80  21  195000  80  6.375   R   N   IN  SF  47000   N   360 2   N   0
172 F108Q4000172    0   723 N   0   1   P   51  21  152000  51  6   R   N   IL  PU  60400   N   360 2   N   0
173 F108Q4000173    0   712 Y   0   1   P   80  33  92000   80  5.75    R   Y   PA  SF  17700   P   360 2   N   0
174 F108Q4000174    0   716 Y   0   1   P   75  44  415000  75  6   R   N   NJ  SF  8800    P   360 2   N   0
175 F108Q4000175    0   798 N   0   1   P   74  27  240000  74  6   R   N   KY  SF  42200   C   360 1   N   0
176 F108Q4000176    0   699 N   30  1   P   93  41  245000  93  5.75    R   N   ND  SF  58600   P   360 2   N   0
177 F108Q4000177    0   771 N   0   1   P   48  34  71000   48  6.125   R   N   MI  SF  49000   P   360 2   N   0
178 F108Q4000178    0   714 Y   30  1   P   95  39  125000  95  5.875   R   N   IA  SF  50300   P   360 2   N   0
179 F108Q4000179    0   816 Y   0   1   P   75  30  115000  75  5.875   R   N   WI  SF  53200   P   360 1   N   0
180 F108Q4000180    0   713 N   0   1   P   79  15  360000  79  6.125   R   N   IL  CO  60600   N   360 1   N   0
181 F108Q4000181    0   791 N   0   1   P   58  17  100000  58  5.625   R   N   ND  SF  58500   C   360 1   N   0
182 F108Q4000182    0   788 N   0   1   P   78  30  145000  78  5.875   R   N   WI  SF  53500   C   360 2   N   0
183 F108Q4000183    0   747 N   0   1   P   90  52  232000  80  5.625   R   N   ND  SF  58700   N   360 2   N   0
184 F108Q4000184    0   756 N   0   1   P   76  35  276000  76  6   R   N   IL  SF  61000   N   360 2   N   0
185 F108Q4000185    0   757 N   0   1   P   65  10  143000  65  6   C   N   WI  SF  54500   N   360 1   N   0
186 F108Q4000186    0   784 N   0   1   P   48  29  200000  48  5.75    R   N   WA  SF  98800   C   360 2   N   0
187 F108Q4000187    0   785 N   0   1   P   54  17  140000  54  5.875   C   N   KY  SF  40600   N   360 2   N   0
188 F108Q4000188    0   802 N   0   1   P   56  22  83000   56  6.5 R   N   KY  SF  42200   N   360 1   N   0
189 F108Q4000189    0   728 N   0   1   P   95  39  196000  80  5.75    R   N   ND  SF  58100   P   360 2   N   0
190 F108Q4000190    0   728 N   0   1   P   80  23  256000  80  5.875   C   N   IL  SF  60000   C   360 1   N   0
191 F108Q4000191    0   781 N   0   1   S   68  56  230000  68  6   R   N   NC  SF  28100   P   360 2   N   0
192 F108Q4000192    0   775 N   0   1   P   44  45  193000  44  5.75    R   N   CA  SF  96000   C   360 2   N   0
193 F108Q4000193    0   661 N   0   1   P   37  19  91000   37  5.625   R   N   NC  SF  27500   C   360 2   N   0
194 F108Q4000194    0   731 N   0   1   P   77  55  276000  77  5.625   R   N   ND  SF  58000   N   360 1   N   0
195 F108Q4000195    0   768 Y   0   1   P   80  18  76000   80  6   R   Y   MI  SF  48700   P   360 1   N   0
196 F108Q4000196    0   758 N   12  1   P   85  24  81000   85  5.875   R   Y   KS  SF  66600   C   360 2   N   0
197 F108Q4000197    0   760 9   0   1   P   62  32  235000  62  5.75    R   N   MN  SF  56300   C   360 2   N   0
198 F108Q4000198    0   812 N   0   1   S   42  31  100000  42  5.875   R   N   NJ  SF  8200    P   360 2   N   0
199 F108Q4000199    0   752 N   0   1   P   70  31  295000  70  6.25    R   N   NY  SF  12500   C   360 2   N   0
200 F108Q4000200    0   818 N   25  1   P   90  35  65000   90  5.875   R   N   NY  SF  14900   P   360 1   N   0
201 F108Q4000201    0   697 N   0   1   P   68  18  400000  68  6.375   C   N   MT  SF  59900   C   360 2   N   0
202 F108Q4000202    0   689 N   0   1   P   80  44  317000  80  6.125   R   N   NE  PU  68100   N   360 2   N   0
203 F108Q4000203    0   771 N   0   1   P   63  65  280000  63  5.875   R   N   CA  SF  96100   C   360 1   N   0
204 F108Q4000204    0   721 N   0   1   P   53  44  192000  53  5.75    R   N   WA  SF  98800   C   360 2   N   0
205 F108Q4000205    0   783 N   30  1   P   92  30  179000  92  6.375   R   N   MI  SF  49700   N   360 1   N   0
206 F108Q4000206    0   774 N   25  1   P   90  24  144000  90  6.375   R   N   NY  CO  10900   P   360 1   N   0
207 F108Q4000207    0   737 9   0   1   P   80  54  212000  80  5.875   R   N   OH  SF  45000   N   360 2   N   0
208 F108Q4000208    0   794 N   0   1   P   74  17  121000  74  5.875   R   N   AK  SF  99600   C   360 2   N   0
209 F108Q4000209    0   813 N   0   1   P   63  30  315000  63  5.75    R   N   CA  SF  95300   P   360 1   N   0
210 F108Q4000210    0   704 N   0   1   P   20  12  60000   20  5.75    R   Y   OH  SF  45300   N   360 2   N   0
211 F108Q4000211    0   732 N   17  1   P   90  48  200000  90  6   R   N   IL  SF  60400   P   360 2   N   0
212 F108Q4000212    0   779 N   0   1   P   80  22  276000  80  5.625   C   N   MN  SF  55900   P   360 2   N   0
213 F108Q4000213    0   735 N   0   1   P   67  40  60000   67  5.75    R   Y   KY  SF  40300   N   360 1   N   0
214 F108Q4000214    0   719 N   12  1   P   84  48  189000  84  5.875   R   N   KS  SF  66600   C   360 2   N   0
215 F108Q4000215    0   727 Y   0   1   P   47  47  115000  47  6.375   R   N   VA  SF  23100   P   360 2   N   0
216 F108Q4000216    0   708 N   0   1   P   80  40  199000  80  6   R   N   IN  SF  46700   N   360 2   N   0
217 F108Q4000217    0   790 N   0   1   P   77  29  249000  77  5.875   R   N   WI  SF  53100   N   360 2   N   0
218 F108Q4000218    0   731 N   0   1   P   80  25  126000  80  5.625   R   N   NC  SF  27800   N   360 2   N   0
219 F108Q4000219    0   742 N   0   1   P   63  31  137000  63  6.25    R   N   NM  SF  87100   N   360 1   N   0
220 F108Q4000220    1   663 N   25  1   P   95  34  121000  95  6.75    R   N   TX  SF  77600   N   360 2   N   0
221 F108Q4000221    0   757 N   0   1   S   74  19  252000  74  6.125   R   N   IA  CO  51300   N   360 2   N   0
222 F108Q4000222    0   798 N   0   1   P   72  17  210000  72  5.875   R   N   WA  SF  99300   P   360 2   N   0
223 F108Q4000223    0   800 N   0   1   P   80  999 320000  80  5.875   R   N   ID  SF  83600   P   360 1   N   0
224 F108Q4000224    0   745 N   0   1   P   42  34  240000  42  5.875   C   N   MI  SF  49700   C   360 1   N   0
225 F108Q4000225    0   796 N   0   1   P   80  30  176000  80  5.875   R   N   TX  SF  77600   P   360 2   N   0
226 F108Q4000227    0   795 Y   0   1   P   70  27  328000  70  5.875   R   N   AK  SF  99500   P   360 2   N   0
227 F108Q4000228    0   752 N   0   1   P   77  47  165000  77  5.875   R   N   KY  SF  42100   N   360 1   N   0
228 F108Q4000229    0   761 N   0   1   P   76  49  406000  76  5.625   R   N   CA  SF  95900   C   360 2   N   0
229 F108Q4000230    0   747 N   0   1   I   49  23  102000  49  6.125   R   N   CO  SF  81200   C   360 2   N   0
230 F108Q4000231    0   683 Y   0   1   P   74  14  69000   74  6   R   Y   MI  SF  48700   P   360 2   N   0
231 F108Q4000232    0   804 N   0   1   P   80  60  160000  80  6.25    R   N   IL  SF  62900   P   360 2   N   0
232 F108Q4000233    0   783 N   0   1   P   80  16  194000  80  5.875   R   N   IL  SF  62800   N   360 2   N   0
233 F108Q4000234    0   800 N   0   1   P   39  28  175000  39  6   R   N   CT  SF  6400    N   360 2   N   0
234 F108Q4000235    0   693 N   0   1   P   69  40  00  69  5.875   R   Y   KY  SF  42100   N   360 2   N   0
235 F108Q4000236    0   751 N   25  1   P   90  49  248000  90  5.625   C   N   MN  SF  56000   N   360 2   N   0
236 F108Q4000237    0   790 N   0   1   P   77  22  311000  77  5.875   R   N   RI  SF  2900    N   36

我只想使用训练有素的数据构建一个简单的逻辑回归模型并显示混淆矩阵。

1 个答案:

答案 0 :(得分:0)

我完全同意您对Logistic回归所做的工作,从概念上讲完全正确,我无权访问您的数据集,因此我无法测试Logistic。我建议您注意两件事(这是我一直想念的事情)

  1. 转换为响应因子

将数据集加载到R环境中时,请确保充当“响应”的变量是因数,以便它们仅采用1或0值。 您可以像这样手动进行操作(否则,您可以直接在模型中的响应变量前面使用 as.factor 函数)

How you auto convert to factor

  1. 请勿使用缩写 尝试指定所有属性:在GLM建筑物中:

    mylogit<-glm(AllMortgage$default ~ AllMortgage$credit_score + AllMortgage$cltv + AllMortgage$dti_ratio + AllMortgage$upb + AllMortgage$ltv + AllMortgage$interest_rate + AllMortgage$loan_term + AllMortgage$num_borrowers, data= your.data, subset = training, family=binomial)

尝试在预测函数中指定所有属性:

mylogit.probs<-predict(mylogit,newdata = validation,type="response")