0

I am trying to run logistic regression model and getting below error.

model1 <- glm(state~. -LanID, data = deliq, family = "binomial", maxit = 50)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

I know this error normally appears when you have one level in a factor variable or only one level has some value(and others are does not have any value).

I have checked all factor variable, none of them seems to have one level only. Please see the summary and str() output on data.

> str(deliq)
'data.frame':   56730 obs. of  46 variables:
 $ LanID                    : chr  "ADV1611581752568472" "ADV1611949561233693" "ADV1612029494951053" "ADV1612029915281654" ...
 $ Region                   : Factor w/ 4 levels "EAST","NORTH",..: 4 3 3 3 3 3 3 3 3 3 ...
 $ Loan.City                : Factor w/ 7 levels "Bangalore","Chennai",..: 7 1 1 4 4 1 2 2 6 6 ...
 $ Loan.Type                : Factor w/ 6 levels "EMI Free Loan",..: 2 5 5 5 2 5 5 5 5 5 ...
 $ Loan.Scheme              : Factor w/ 26 levels "Advance Salary Loan",..: 21 21 21 21 21 21 21 21 21 21 ...
 $ Loan.Status              : Factor w/ 2 levels "active","closed": 1 2 1 2 2 1 1 1 1 2 ...
 $ Principal.Outstanding    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Last.EMI                 : num  8776 6609 11409 12287 13164 ...
 $ Sum.Instalment.Posted    : int  52656 39654 68454 73722 13164 78984 89520 84252 52656 73722 ...
 $ Dues.Receipts            : num  53248 39654 57753 73722 13216 ...
 $ EMI.Due                  : num  -592 0 10701 0 -52 ...
 $ All.Dues                 : num  2859 0 17164 0 0 ...
 $ Instalment.Dues          : num  1776 0 11409 0 0 ...
 $ Bullets.Overdue          : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Loan.Quality             : Factor w/ 7 levels "Closed","Doubtful",..: 6 6 5 6 6 7 6 6 7 6 ...
 $ Sanctioned.Amount        : int  50000 225000 65000 70000 75000 75000 85000 80000 50000 70000 ...
 $ Loan.Amount              : int  50000 225000 65000 70000 75000 75000 85000 80000 50000 70000 ...
 $ Tenure                   : Factor w/ 24 levels "3","5","6","8",..: 3 23 3 3 3 3 3 3 3 3 ...
 $ Completed.Tenure         : num  6 6 6 6 1 6 6 6 6 6 ...
 $ Tenure.Left              : num  0 42 0 0 5 0 0 0 0 0 ...
 $ No..Of.Late.Payments     : int  4 0 6 0 0 6 0 0 5 0 ...
 $ Gender                   : Factor w/ 2 levels "female","male": 1 2 2 2 1 2 2 2 2 2 ...
 $ Educational.Qualification: Factor w/ 6 levels "graduate","Not Available",..: 1 1 1 1 1 1 1 4 1 1 ...
 $ Marital.Status           : Factor w/ 3 levels "married","single",..: 2 1 1 2 1 1 1 2 2 1 ...
 $ Job.Type                 : Factor w/ 9 levels "delivery-boy/driver",..: 6 6 6 6 6 6 6 6 6 6 ...
 $ Employment.Year          : Factor w/ 4 levels "less-than-2-years",..: 3 3 3 2 3 1 1 3 2 3 ...
 $ Age                      : num  29 37 37 28 31 31 44 25 29 43 ...
 $ relevant_pos             : num  0 0 11241 0 0 ...
 $ crif_active_accounts     : int  6 19 1 9 11 4 4 20 5 5 ...
 $ crif_overdue_amt         : int  0 0 23653 0 0 0 0 0 10486 0 ...
 $ crif_current_outstanding : num  849716 1095088 23000 170614 621655 ...
 $ cibil_active_accounts    : int  0 0 0 0 0 0 0 0 0 0 ...
 $ cibil_overdue_amt        : int  0 0 0 0 0 0 0 0 0 0 ...
 $ cibil_current_outstanding: int  0 0 0 0 0 0 0 0 0 0 ...
 $ NACH.status              : Factor w/ 6 levels "active","nach-registration-initiated",..: 4 5 4 5 5 4 4 4 4 5 ...
 $ Awarenss.Allocation      : Factor w/ 17 levels "Cheque","Chq Banked ",..: 12 4 12 4 4 12 12 12 12 4 ...
 $ Tenure.End               : Factor w/ 6 levels "Closed","No",..: 3 1 3 1 1 3 3 3 3 1 ...
 $ LastMonthBkt             : Factor w/ 28 levels "-","#N/A","0",..: 1 1 4 1 1 24 1 1 15 1 ...
 $ LastMonthBnc             : Factor w/ 4 levels "-","1st EMI",..: 1 3 1 3 3 1 1 1 1 3 ...
 $ PAID.Unpaid              : Factor w/ 2 levels "Closed","Running": 2 1 2 1 1 2 2 2 2 1 ...
 $ Last.Month.delinq        : Factor w/ 7 levels "1","2","3","4",..: 6 6 6 6 6 6 6 6 6 6 ...
 $ CIBIL.Bracket            : Factor w/ 11 levels "-1","490-600",..: 3 4 6 3 6 3 6 3 6 6 ...
 $ Salary.Bracket           : Factor w/ 10 levels "125K-200K","30k-40k",..: 3 3 4 3 4 5 5 4 3 4 ...
 $ DELINQ.NON.DELINQ        : Factor w/ 2 levels "DELINQ","NON DELINQ": 2 2 1 2 2 1 2 2 1 2 ...
 $ Month                    : int  6 6 6 6 6 6 6 6 6 6 ...
 $ state                    : Factor w/ 3 levels "No Change","To DELINQ",..: 1 1 1 1 1 1 1 1 1 1 ...
> summary(deliq)
    LanID             Region          Loan.City                          Loan.Type    
 Length:56730       EAST :   10   Bangalore:14146   EMI Free Loan             : 5948  
 Class :character   NORTH:16224   Chennai  : 3766   Others                    :11488  
 Mode  :character   SOUTH:23085   Delhi NCR:15081   Overdraft Loan            : 2127  
                    WEST :17411   Hyderabad: 4544   Paytm Term Loan           :17100  
                                  Mumbai   : 4995   Personal Term Loan        :18143  
                                  Others   : 3737   Upward Term Loan - Loantap: 1924  
                                  Pune     :10461                                     
             Loan.Scheme    Loan.Status    Principal.Outstanding    Last.EMI     
 Paytm Term Loan   :17100   active:47009   Min.   :      0       Min.   :     2  
 SALARIED          :16792   closed: 9721   1st Qu.:  17629       1st Qu.:  3809  
 SALARIED          :13465                  Median : 100000       Median :  7344  
 Personal Term Loan: 5115                  Mean   : 147043       Mean   :  7335  
 EMI Free Loan     : 1523                  3rd Qu.: 199821       3rd Qu.:  9456  
 HF LOAN           :  729                  Max.   :2413127       Max.   :138450  
 (Other)           : 2006                                                        
 Sum.Instalment.Posted Dues.Receipts        EMI.Due          All.Dues      Instalment.Dues 
 Min.   :      0       Min.   :      0   Min.   :-51827   Min.   :-50787   Min.   :-50787  
 1st Qu.:  20316       1st Qu.:  18000   1st Qu.:     0   1st Qu.:     0   1st Qu.:     0  
 Median :  47280       Median :  41125   Median :     0   Median :     0   Median :     0  
 Mean   :  57615       Mean   :  52421   Mean   :  5194   Mean   :  6662   Mean   :  4558  
 3rd Qu.:  81264       3rd Qu.:  75152   3rd Qu.:  2189   3rd Qu.:  3154   3rd Qu.:  1529  
 Max.   :1456140       Max.   :1456140   Max.   :501664   Max.   :763635   Max.   :361524  

 Bullets.Overdue         Loan.Quality   Sanctioned.Amount  Loan.Amount          Tenure     
 Min.   :     0   Closed       : 8075   Min.   :  15000   Min.   :  15000   60     :24079  
 1st Qu.:     0   Doubtful     :  389   1st Qu.: 100000   1st Qu.: 100000   12     :15189  
 Median :     0   Not Available:  111   Median : 150000   Median : 150000   24     : 5239  
 Mean   :  1596   SMA1         : 2361   Mean   : 209688   Mean   : 209688   36     : 4859  
 3rd Qu.:     0   SMA2         : 1398   3rd Qu.: 250000   3rd Qu.: 250000   48     : 3503  
 Max.   :333334   Standard     :41393   Max.   :2500000   Max.   :2500000   18     : 2550  
                  SubStandard  : 3003                                       (Other): 1311  
 Completed.Tenure  Tenure.Left    No..Of.Late.Payments    Gender     
 Min.   : 0.000   Min.   : 0.00   Min.   : 0.000       female: 5258  
 1st Qu.: 4.000   1st Qu.: 7.00   1st Qu.: 0.000       male  :51472  
 Median : 7.000   Median :32.00   Median : 0.000                     
 Mean   : 8.028   Mean   :29.98   Mean   : 1.691                     
 3rd Qu.:11.000   3rd Qu.:52.00   3rd Qu.: 2.000                     
 Max.   :37.000   Max.   :60.00   Max.   :27.000                     
                  NA's   :21                                         
  Educational.Qualification   Marital.Status                  Job.Type    
 graduate      :32823       married  :40947   salaried            :37990  
 Not Available : 2861       single   :15439   self-employed       :17959  
 others        : 1757       undefined:  344   others              :  248  
 post-graduate :11670                         sales & marketing   :  186  
 professional  : 1746                         operations          :  154  
 under-graduate: 5873                         finance & accounting:  104  
                                              (Other)             :   89  
          Employment.Year       Age         relevant_pos     crif_active_accounts
 less-than-2-years: 8374   Min.   : 1.00   Min.   :      0   Min.   :  0.000     
 less-than-5-years: 9238   1st Qu.:29.00   1st Qu.:  25709   1st Qu.:  2.000     
 more-than-5-years:25568   Median :33.00   Median : 100000   Median :  6.000     
 Not Available    :13550   Mean   :33.57   Mean   : 150620   Mean   :  6.678     
                           3rd Qu.:37.00   3rd Qu.: 200000   3rd Qu.: 10.000     
                           Max.   :74.00   Max.   :2413127   Max.   :533.000     

 crif_overdue_amt   crif_current_outstanding cibil_active_accounts cibil_overdue_amt
 Min.   :     -12   Min.   :-1.526e+08       Min.   : 0.00         Min.   :      0  
 1st Qu.:       0   1st Qu.: 1.492e+04       1st Qu.: 0.00         1st Qu.:      0  
 Median :       0   Median : 4.392e+05       Median : 0.00         Median :      0  
 Mean   :   12791   Mean   : 4.583e+06       Mean   : 3.41         Mean   :   4761  
 3rd Qu.:       0   3rd Qu.: 1.538e+06       3rd Qu.: 6.00         3rd Qu.:      0  
 Max.   :40236040   Max.   : 9.000e+10       Max.   :78.00         Max.   :4409530  

 cibil_current_outstanding                      NACH.status         Awarenss.Allocation
 Min.   :-152975611        active                     :31716   None           :23641   
 1st Qu.:         0        nach-registration-initiated: 1070   Nach           :14511   
 Median :         0        nach-sent-for-registration :  644   NACH PRESENTED : 7428   
 Mean   :    685390        Not Available              :10770   Closed         : 1640   
 3rd Qu.:    537990        Others                     : 9701   RCSPL-WEST     : 1544   
 Max.   : 276999154        rejected                   : 2829   NACH NOT REG   : 1281   
                                                               (Other)        : 6685   
       Tenure.End     LastMonthBkt    LastMonthBnc    PAID.Unpaid      Last.Month.delinq
 Closed     : 1640   0      :18694   -      :53553   Closed : 1640   1          :  571  
 No         :51132   -      :16576   1st EMI:  296   Running: 8444   2          :  320  
 Tenure End :  384   #N/A   : 9058   Closed : 1640   NA's   :46646   3          :  295  
 Tenure End : 1018   X      : 4561   Yes    : 1241                   4          :  110  
 Yes        : 1815   1      : 2550                                   5          :   89  
 YES        :  741   2      : 1500                                   More-than-5:53888  
                     (Other): 3791                                   X          : 1457  
       CIBIL.Bracket         Salary.Bracket   DELINQ.NON.DELINQ     Month       
 Not Available:20932   50K-70K      :12334   DELINQ    : 7841   Min.   : 6.000  
 701-750      :10293   Less than 30K: 8943   NON DELINQ:48889   1st Qu.: 7.000  
 676-700      : 8259   40K-50K      : 7098                      Median : 8.000  
 601-650      : 5566   70K-90K      : 7062                      Mean   : 8.126  
 651-675      : 5502   90K-125K     : 5801                      3rd Qu.: 9.000  
 -1           : 1978   30K-40K      : 3831                      Max.   :10.000  
 (Other)      : 4200   (Other)      :11661                                      
           state      
 No Change    :54860  
 To DELINQ    : 1374  
 To NON-DELINQ:  496  

I tried model1 <- glm(deliq[,'state']~deliq[,2:45]) as well as suggested in this stackoverflow post

Please suggest what is going wrong here?

SKB
  • 189
  • 1
  • 13

1 Answers1

0

I am able to figure out another reason for this error.

Along with just 1 level to a factor field(which is not the case in above data), above error appears when you have NA's available in data-set.

In above case, i removed NA's and error disappeared.

SKB
  • 189
  • 1
  • 13