【发布时间】:2020-05-26 17:20:24
【问题描述】:
我有以下功能:
cost<-function(alpha) {
ypred<-lda.pred$posterior[,2] # prob of Y=1
ypred[lda.pred$posterior[,2]>=alpha]=1
ypred[lda.pred$posterior[,2]<alpha]=0
y<-dataframe_testsample[,y]
df<-data.frame(y,ypred)
row.names(df) <- NULL
FN <- sum(df$y == '1' & df$ypred == '0')
FP <- sum(df$y == '0' & df$ypred == '1')
tot_costs<-FN*10+FP*8
return(tot_costs)
}
这是一个在使用线性判别分析(R 中的 lda 命令)时计算错误分类的总成本的函数。 y 和 ypred 都是 137x1 向量。该函数计算误报 (FP) 和误报 (FN) 的数量以及总成本。
这工作无懈可击。但是,当尝试使用以下内容进行绘制时
alpha_grid<-seq(0,1,0.01)
plot(alpha_grid,cost(alpha_grid))
我收到以下错误消息:
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
Inoltre: Warning messages:
1: In lda.pred$posterior[, 2] >= alpha :
longer object length is not a multiple of shorter object length
2: In lda.pred$posterior[, 2] < alpha :
longer object length is not a multiple of shorter object length
这里发生了什么?
【问题讨论】:
-
您拥有的函数,例如在 lda.pred$postterior[, 2] >= alpha 处,仅适用于 1 个 alpha 值。您需要遍历 alpha 的所有值,例如 sapply((alpha_grid,cost)