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)
}
It was worth a shot, but the same errors were still returned, except for the error with the arrange function. I fixed this. It was due to a missing object in the function call. Restarting and changing things so many times makes it easy to forget to run a segment every now and then.
Is there a way to see which code line is causing the warnings? This may help.