|
@@ -233,3 +233,60 @@ simple.plotROC<-function(df,var,col="black",x=0.65,y=0.1,unit="ml",precise=FALSE
|
|
base::list(roc=roc,thr=roc_metrics$threshold,legend_text=legend_text)
|
|
base::list(roc=roc,thr=roc_metrics$threshold,legend_text=legend_text)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#'Plot an assembly of ROCs
|
|
|
|
+#'
|
|
|
|
+#'@param df data frame
|
|
|
|
+#'@param vars vector of variables
|
|
|
|
+#'@param cols vector of color names
|
|
|
|
+#'@param x coordinate for legend
|
|
|
|
+#'@param y coordinate for legend
|
|
|
|
+#'@param unit unit for threshold in labels
|
|
|
|
+#'@param precise number of decimal places to use when reporting opt threshold, TRUE:2, FALSE:0
|
|
|
|
+#'@param target column that holds binary outcomes
|
|
|
|
+#'
|
|
|
|
+#'@return ggplot2 graphical object
|
|
|
|
+#'
|
|
|
|
+#'@export
|
|
|
|
+
|
|
|
|
+simple.plotROCgg<-function(df,vars,cols,x=0.7,y=0.3,unit="ml",precise="FALSE",target="alive"){
|
|
|
|
+ if (!requireNamespace('ggplot2',quiet=TRUE)){
|
|
|
|
+ print('ggplot2 not available. Use simple.plotROC function')
|
|
|
|
+ return(NULL)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #ggplot alternative
|
|
|
|
+ cvalues<-base::c()
|
|
|
|
+ colors_used<-base::c()
|
|
|
|
+
|
|
|
|
+ i=0
|
|
|
|
+ g<-ggplot2::ggplot()
|
|
|
|
+ for (var in vars) {
|
|
|
|
+ if (var %in% base::names(df)) {
|
|
|
|
+ # Pred izračunom ROC odstranimo vrstice z NA vrednostmi
|
|
|
|
+ df<-mapNA(df,var,0)
|
|
|
|
+ roc=simple.roc(df[,target],df[,var])
|
|
|
|
+#!! defuses evaluation of variable. https://rlang.r-lib.org/reference/topic-inject.html
|
|
|
|
+ col=cols[i]
|
|
|
|
+ roc_metrics <- simple.compute_roc_metrics(roc)
|
|
|
|
+ auc=simple.getAUC(roc)
|
|
|
|
+ sAUC=simple.sAUC(roc)
|
|
|
|
+ lab <- base::sprintf("[%s] AUC: %.2f (+- %.2f), OPT THR: %.2f",
|
|
|
|
+ var, auc, sAUC, roc_metrics$threshold)
|
|
|
|
+
|
|
|
|
+ g<-g+ggplot2::geom_line(ggplot2::aes(x=!!roc$FPR,y=!!roc$TPR,color=!!lab))
|
|
|
|
+ aes_p=ggplot2::aes(x=!!roc_metrics$FPR,y=!!roc_metrics$TPR,color=!!lab)
|
|
|
|
+ g<-g+ggplot2::geom_point(aes_p,size=4,shape=1,show.legend=FALSE)
|
|
|
|
+ colors_used<-base::c(colors_used,col)
|
|
|
|
+ cvalues<-base::c(cvalues,lab)
|
|
|
|
+ i=i+1
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ base::names(colors_used)<-cvalues
|
|
|
|
+ g+ggplot2::xlab('1-specificity')+
|
|
|
|
+ ggplot2::ylab('sensitivity')+
|
|
|
|
+ ggplot2::scale_color_manual(name='Variables',values=colors_used)+
|
|
|
|
+ ggplot2::guides(color = ggplot2::guide_legend(position = "inside"))+
|
|
|
|
+ ggplot2::theme(legend.position.inside=base::c(x,y))
|
|
|
|
+
|
|
|
|
+}
|