Monday, May 2, 2016

How to use the for loop with function needing for a string field?

Leave a Comment

I am using the smbinning R package to compute the variables information value included in my dataset.

The function smbinning() is pretty simple and it has to be used as follows:

result = smbinning(df= dataframe, y= "target_variable", x="characteristic_variable", p = 0.05) 

So, df is the dataset you want to analyse, y the target variable and x is the variable of which you want to compute the information value statistics; I enumerate all the characteristic variables as z1, z2, ... z417 to be able to use a for loop to mechanize all the analysis process.

I tried to use the following for loop:

for (i in 1:417) {  result = smbinning(df=DATA, y = "FLAG", x = "DATA[,i]", p=0.05)   } 

in order to be able to compute the information value for each variable corresponding to i column of the dataframe.

The DATA class is "data.frame" while the resultone is "character".

So, my question is how to compute the information value of each variable and store that in the object denominated result?

Thanks! Any help will be appreciated!

2 Answers

Answers 1

No sample data is provided I can only hazard a guess that the following will work:

results_list = list()     for (i in 1:417) {     current_var = paste0('z', i)     current_result = smbinning(df=DATA, y = "FLAG", x = current_var, p=0.05)     results_list[i] = current_result$iv } 

Answers 2

You could try to use one of the apply methods, iterating over the z-counts. The x value to smbinning should be the column name not the column.

results = sapply(paste0("z",1:147), function(foo) {    smbinning(df=DATA, y = "FLAG", x = foo, p=0.05) }) class(results) # should be "list" length(results) # should be 147 names(results) # should be z1,... results[[1]] # should be the first result, so you can also iterate by indexing 

I tried the following, since you had not provided any data

> XX=c("IncomeLevel","TOB","RevAccts01") > res = sapply(XX, function(z) smbinning(df=chileancredit.train,y="FlagGB",x=z,p=0.05)) Warning message: NAs introduced by coercion  > class(res) [1] "list" > names(res) [1] "IncomeLevel" "TOB"         "RevAccts01" > res$TOB ... 

HTH

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment