The code below throws the following error (a different number of times on each run, oddly enough, and even before the function has been run on other commands):
Unknown or uninitialised column: f
.
Unknown or uninitialised column: L
.
I did not write this code, it was pulled from an instructor's site and is not related to any homework. To me, it looks like all the variables are initialized. What am I missing here?
##ROC FUNCTION
#' ROC curve code
#'
#' Based on algo 1 page 866, Fawcett2005
#'
#' @param L observations
#' @param f, predicted prob.
#'
#' @return points in ROC space and score
get_roc <- function(L, f) {
# Calculate P and N
P <- sum(L==1)
N <- sum(L==0)
# Order the observations by prediction
df <- tibble(L, f)
df <- df %>% arrange(desc(f))
# Set TP and FP to zero
TP <- 0
FP <- 0
# Set up matrix for results
R <- NULL
# Set previous f
f_prev <- -Inf
# set counter
i <- 1
while(i <= length(df$L)){
if( df$f[i] != f_prev){
R <- rbind(R, c(FP/N, TP/P, df$f[i]))
f_prev <- df$f[i]
}
if(df$L[i] == 1){
TP <- TP + 1
} else {
FP <- FP + 1
}
i <- i + 1
}
R <- rbind(R, c(FP/N, TP/P, f_prev))
R <- data.frame(R)
colnames(R) <- c("FPR","TPR", "Score")
return(R)
}
ROC <- get_roc(L = df$y, f = df$predictions)