0

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?

1
  • 1
    the function you have, for example at lda.pred$posterior[, 2] >= alpha, works only for 1 value of alpha. You need to iterate through all values of alpha, for example sapply((alpha_grid,cost) Commented Feb 11, 2020 at 10:18

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.