在 R 中创建提升图
Creating a lift chart in R
假设我有以下数据框,其中包含与他们相关的某些分数的人:
Score | hasDefaulted
10 | 0
13 | 0
15 | 1
17 | 0
...
我想在 R 中制作一个提升图,方法是首先按分数对人口进行排序,然后在 X 轴上显示人口百分比,在 Y 轴上显示默认百分比。我找不到可以让我控制执行此操作的好包。我探索了 Package Lift as well as Package Gains 但我无法弄清楚如何对它们进行足够的控制以执行我上面描述的操作。例如,当我尝试使用 Package Lift 时,as
plotLift(sort(dataFrame$Score, decreasing=FALSE), dataFrame$hasDefaulted)
我得到一些奇怪的情节:
但根据我的意愿,该图最终应该看起来像一个累积密度函数。
有人可以告诉我如何正确使用这些包,或者指导我使用满足要求的包吗?提前致谢。
我总是尝试构建自己的代码,而不是尝试不太灵活的代码。
以下是我认为您可以解决问题的方法:
# 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")
这是你想要的吗?
我认为您正在搜索增益图,而不是提升图。我注意到他们之间有些混淆。您可以参考 Lift Charts 了解更多信息。
require(ROCR)
data(ROCR.simple)
pred <- prediction(ROCR.simple$predictions, ROCR.simple$labels)
gain <- performance(pred, "tpr", "rpp")
plot(gain, main = "Gain Chart")
即使这个问题是大约 5 年前提出的,我还是想分享一下我最近发现了一个很好的包,它可以帮助构建 GAIN 和 LIFT 图表,并显示增益和提升表:包名称是 CustomerScoringMetrics
.
函数:cumGainsChart()
、cumGainsChart()
、liftChart()
、liftTable()
等
假设我有以下数据框,其中包含与他们相关的某些分数的人:
Score | hasDefaulted
10 | 0
13 | 0
15 | 1
17 | 0
...
我想在 R 中制作一个提升图,方法是首先按分数对人口进行排序,然后在 X 轴上显示人口百分比,在 Y 轴上显示默认百分比。我找不到可以让我控制执行此操作的好包。我探索了 Package Lift as well as Package Gains 但我无法弄清楚如何对它们进行足够的控制以执行我上面描述的操作。例如,当我尝试使用 Package Lift 时,as
plotLift(sort(dataFrame$Score, decreasing=FALSE), dataFrame$hasDefaulted)
我得到一些奇怪的情节:
但根据我的意愿,该图最终应该看起来像一个累积密度函数。
有人可以告诉我如何正确使用这些包,或者指导我使用满足要求的包吗?提前致谢。
我总是尝试构建自己的代码,而不是尝试不太灵活的代码。
以下是我认为您可以解决问题的方法:
# 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")
这是你想要的吗?
我认为您正在搜索增益图,而不是提升图。我注意到他们之间有些混淆。您可以参考 Lift Charts 了解更多信息。
require(ROCR)
data(ROCR.simple)
pred <- prediction(ROCR.simple$predictions, ROCR.simple$labels)
gain <- performance(pred, "tpr", "rpp")
plot(gain, main = "Gain Chart")
即使这个问题是大约 5 年前提出的,我还是想分享一下我最近发现了一个很好的包,它可以帮助构建 GAIN 和 LIFT 图表,并显示增益和提升表:包名称是 CustomerScoringMetrics
.
函数:cumGainsChart()
、cumGainsChart()
、liftChart()
、liftTable()
等