R/visualise_roc.R

# ROC curve

# input a two factor variables

y_pred = as.factor(c(1,0,0,1,0,1,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,0,1,0,1,1,0,0,1))
y_obs = as.factor(c(1,1,0,1,0,0,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,1,0,1,0,0,0,1,0,0))

y_pred = rep(1, times = 100)
y_obs = rep(1, times = 100)

TPR = numeric(length(y_obs))
FPR = numeric(length(y_pred))

for (i in 1:length(y_obs)){

  # subset the vectors
  thres_pred = y_pred[1:i]
  thres_obs = y_obs[1:i]

  # calculate the TPR and FPR
  TPRi = unlist(evaluation_metrics(y_obs = thres_obs, 
                                      y_pred = thres_pred, 
                                      type = "classification")[8])
  
  FPRi = unlist(evaluation_metrics(y_obs = thres_obs, 
                                      y_pred = thres_pred, 
                                      type = "classification")[9])
  
  # append TPRi and FPRi to TPR and FPR
  TPR = c(TPR, TPRi)
  FPR = c(FPR, FPRi)
  
}

plot(y = TPR, x = FPR, type = "l")
oislen/BuenaVista documentation built on May 16, 2019, 8:12 p.m.