我正在尝试将stat_ecdf()
累积成功绘制为预测模型创建的排名分数的函数。
#libraries
require(ggplot2)
require(scales)
# fake data for reproducibility
set.seed(123)
n <- 200
df <- data.frame(model_score= rexp(n=n,rate=1:n),
obs_set= sample(c("training","validation"),n,replace=TRUE))
df$model_rank <- rank(df$model_score)/n
df$target_outcome <- rbinom(n,1,1-df$model_rank)
# Plot Gain Chart using stat_ecdf()
ggplot(subset(df,target_outcome==1),aes(x = model_rank)) +
stat_ecdf(aes(colour = obs_set), size=1) +
scale_x_continuous(limits=c(0,1), labels=percent,breaks=seq(0,1,.1)) +
xlab("Model Percentile") + ylab("Percent of Target Outcome") +
scale_y_continuous(limits=c(0,1), labels=percent) +
geom_segment(aes(x=0,y=0,xend=1,yend=1),
colour = "gray", linetype="longdash", size=1) +
ggtitle("Gain Chart")
我要做的就是强制 ECDF 从 (0,0) 开始并在 (1,1) 结束,这样曲线的开头或结尾就没有间隙。如果可能的话,我想在 的语法中执行它ggplot2
,但我会满足于一个聪明的解决方法。
@Henrik 这不是这个问题的重复,因为我已经用 and 定义了我的限制scale_x_
,_y_continuous()
并且添加expand_limits()
不会做任何事情。需要修复的不是 PLOT 的起点,而是 stat_ecdf() 的端点。