Search code examples
rgraphvisualization

Creating a lift chart in R


Suppose I have the following data frame consisting of people with some score associated with them:

Score | hasDefaulted
10    | 0
13    | 0
15    | 1
17    | 0
...

I want to make a lift chart in R by first sorting the population by score, then having % of population on the X-axis, and % of Default's on the Y-axis. I cannot find a good package that gives me the control to do this. I have explored Package Lift as well as Package Gains but I cannot figure out how to get enough control over them to do what I described above. For example, when I try using Package Lift, as

plotLift(sort(dataFrame$Score, decreasing=FALSE), dataFrame$hasDefaulted)

I get some strange plot:

But given my desires, the plot should end up looking like a cumulative density function.

Could someone show me how to use such packages properly, or direct me to a package that does the required? Thanks in advance.


Solution

  • I always try to build my own code rather than trying something less flexible.

    Here's how I think you can tackle the problem:

    # Creating the data frame
    df <- data.frame("Score"=runif(100,1,100),
                     "hasDefaulted"=round(runif(100,0,1),0))
    
    # Ordering the dataset
    df <- df[order(df$Score),]
    
    # Creating the cumulative density
    df$cumden <- cumsum(df$hasDefaulted)/sum(df$hasDefaulted)
    
    # Creating the % of population
    df$perpop <- (seq(nrow(df))/nrow(df))*100
    
    # Ploting
    plot(df$perpop,df$cumden,type="l",xlab="% of Population",ylab="% of Default's")
    

    enter image description here

    Is that what you want?