Reputation: 357
I have the following function:
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)
}
This is a function to calculate the total costs of a wrong classification of a class while using the Linear Discriminant Analysis (lda command in R). y
and ypred
are both 137x1 vectors. The function calculates the number of false positive (FP) and false negative (FN) and also the total cost.
This works flwaless. However, when trying to plot it with the following
alpha_grid<-seq(0,1,0.01)
plot(alpha_grid,cost(alpha_grid))
I get the following error message:
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
What is it happening here?
Upvotes: 0
Views: 30
Reputation: 26
Your function deals with a single value of alpha at the moment. You could get the result you want by using sapply
alpha_grid<-seq(0,1,0.01)
plot(alpha_grid,sapply(alpha_grid, FUN=cost))
Upvotes: 1