我想按区间计算错误率,其中0
是好的,1
是坏的。如果我有100个观察样本作为级别按以下间隔划分如下:
X <- 10; q<-sample(c(0,1), replace=TRUE, size=X)
l <- sample(c(1:100),replace=T,size=10)
bornes<-seq(min(l),max(l),5)
v <- cut(l,breaks=bornes,include.lowest=T)
table(v)
如何获得一个表或函数来计算每个区间的默认率,不良观测数除以观测总数?
tx_erreur<-function(x){
t<-table(x,q)
return(sum(t[,2])/sum(t))
}
我已经尝试过上面的代码并进行了tapply。谢谢!
我想你想要这个:
tapply(q,# the variable to be summarized
v,# the variable that defines the bins
function(x) # the function to calculate the summary statistics within each bin
sum(x)/length(x))