Why values are not changing using replacement function in R?

r

#1

I am currently trying to solve one classification problem in which I am trying to replace the level of the Dependent variable(Loan_Status) plyr packages in R, but my level is not changing.

'data.frame':   614 obs. of  11 variables:
** $ Loan_Status      : Factor w/ 2 levels "N","Y": 2 1 2 2 2 2 2 1 2 1 ...**
 $ Credit_History   : int  1 1 1 1 1 1 1 0 1 1 ...
 $ Property_Area    : Factor w/ 3 levels "Rural","Semiurban",..: 3 1 3 3 3 3 3 2 3 2 ...
 $ CoapplicantIncome: num  0 1508 0 2358 0 ...
 $ ApplicantIncome  : num  5849 4583 3000 2583 6000 ...
 $ LoanAmount       : num  142 128 66 120 141 ...
 $ Married          : Factor w/ 2 levels "No","Yes": 1 2 2 2 1 2 2 2 2 2 ...
 $ Education        : Factor w/ 2 levels "Graduate","Not Graduate": 1 1 1 2 1 1 2 1 1 1 ...
 $ Dependents       : Factor w/ 4 levels "0","1","2","3+": 1 2 1 1 1 3 1 4 3 2 ...
 $ Gender           : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
 $ Self_Employed    : Factor w/ 2 levels "No","Yes": 1 1 2 1 1 2 1 1 1 1 ...


library(plyr)
 
 train_final$Loan_Status<-as.numeric(revalue(train_final$Loan_Status,c("N"=0,"Y"=1)))
 str(train_final)
'data.frame':   614 obs. of  11 variables:
 $ Loan_Status      : num  2 1 2 2 2 2 2 1 2 1 ...
 $ Credit_History   : int  1 1 1 1 1 1 1 0 1 1 ...
 $ Property_Area    : Factor w/ 3 levels "Rural","Semiurban",..: 3 1 3 3 3 3 3 2 3 2 ...
 $ CoapplicantIncome: num  0 1508 0 2358 0 ...
 $ ApplicantIncome  : num  5849 4583 3000 2583 6000 ...
 $ LoanAmount       : num  142 128 66 120 141 ...
 $ Married          : Factor w/ 2 levels "No","Yes": 1 2 2 2 1 2 2 2 2 2 ...
 $ Education        : Factor w/ 2 levels "Graduate","Not Graduate": 1 1 1 2 1 1 2 1 1 1 ...
 $ Dependents       : Factor w/ 4 levels "0","1","2","3+": 1 2 1 1 1 3 1 4 3 2 ...
 $ Gender           : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
 $ Self_Employed    : Factor w/ 2 levels "No","Yes": 1 1 2 1 1 2 1 1 1 1 ...

#2

Hi
revalue(trainfinal…) will do change your factor levels references , for Y to 1 and N to 0 in your case, but it still return a factor !! then you a a numeric transformation and got 2 1 which is expected levels start at 1 in R not 0.
I think what you want to do is unclass(train_final$Loan_Status) - 1 and then you can put in your gbm model as mentioned in your other posting.
Hope this help.
Alain


#3

@sid100158, check if the menthods posted in this thread help.

http://discuss.analyticsvidhya.com/t/how-to-change-the-level-of-factor-variable-in-r/2876