Created
July 7, 2020 01:22
-
-
Save tslumley/24a711688dee2945a4f0252ddbb2aa72 to your computer and use it in GitHub Desktop.
Profile likelihood for 'balanced accuracy'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nnot<-100 | |
ndis<-50 | |
tpr<-0.9 | |
fpr<-0.5 | |
rr<-replicate(100,{ | |
tpos <-rbinom(1,ndis,tpr) | |
fpos<-rbinom(1,nnot,fpr) | |
fneg <-ndis-tpos | |
tneg<-nnot-fpos | |
sens<-tpos/(tpos+fneg) | |
spec<-tneg/(tneg+fpos) | |
bla<-(sens+spec)/2 | |
diff<-(sens-spec)/2 | |
lldiff<-function(a,d){ | |
k<-length(a) | |
se<-a+d | |
sp<-a-d | |
rval <-dbinom(tpos,ndis,se,log=TRUE)+dbinom(tneg,nnot,sp,log=TRUE) | |
rval[is.nan(rval)]<- -Inf | |
rval | |
} | |
llprof<-function(a){ | |
limits<-c(-a, 1-a) | |
optimise(function(d) -2*lldiff(a,d),limits)$objective | |
} | |
xrange<-seq(0.5,.99,length=100) | |
llprofcurve<-sapply(xrange, llprof) | |
devmin<-min(llprofcurve,na.rm=TRUE) | |
range(xrange[llprofcurve<=devmin+qchisq(.95,1)]) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment