-
Notifications
You must be signed in to change notification settings - Fork 0
/
3) Custom Functions.R
74 lines (63 loc) · 2.13 KB
/
3) Custom Functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
##Step 2: Loading Functions to be Used
#Mode function for Imputation of categorical variables
Mode <- function (x, na.rm) {
xtab <- table(x)
xmode <- names(which(xtab == max(xtab)))
if (length(xmode) > 1)
xmode <- xmode[1]
return(xmode)
}
#using code in hand-outs for Confusion Statistics and ROC
confusion=function(truth,pred,conversion=c(1,1)){
a=conversion*table(truth,pred,dnn=c("Truth","Prediction"))
if(ncol(a)<2){ return( list(
Confusion=NA,
Misclassification=NA,
Precision=NA,
Sensitivity=NA,
Specificity=NA
)
)
}
list(
Confusion=addmargins(a, FUN = list(Total = sum), quiet = TRUE),
Misclassification=1-sum(diag(a))/sum(a),
Precision=a[2,2]/sum(a[,2]),
Sensitivity=a[2,2]/sum(a[2,]),
Specificity=a[1,1]/sum(a[1,])
)
}
roc=function(truth,p,k=100,plot=TRUE,lines=FALSE,...){
Curve=rbind(c(0,0),
t(sapply(quantile(p,(k:1)/(k+1)),function(th){
pred=as.numeric(p>th)
if(length(unique(pred))==2){
a=confusion(truth,pred)
return(c(1-a$Specificity,a$Sensitivity))
} else {
return(c(NA,NA))
}
}
)),
c(1,1)
)
Curve=Curve[complete.cases(Curve),]
if(plot&!lines) plot(Curve,xlab="1-Specificity",ylab="Sensitivity",main="ROC curve",xlim=0:1,ylim=0:1,type="l",...)
if(plot&lines) lines(Curve,...)
invisible(list(ROC=Curve,AUC=sum(diff(Curve[,1])*(Curve[-1,2]+Curve[-nrow(Curve),2])/2)))
}
#Wrapper function for wrapping long titles
wrapper <- function(x, ...)
{
paste(strwrap(x, ...), collapse = "\n")
}
#Return common legend
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
#Setting working directory using shortcut
set_wd_function <- function(directory) setwd( file.path(getwd(), directory))
#----------------------------------END OF CODE------------------------------------------------------