minfconf=function(x,y){ #this will spit out the quantiles of the MI from the bootstrap.. library(sm) air3 = cbind(x,y) result.12 = sm.density(air3, eval.points = air3, display = "none",verbose=0) result.12 = diag(result.12$estimate) result.1 = sm.density(x, eval.points = x, display = "none",verbose=0)$estimate result.2 = sm.density(y, eval.points = y, display = "none",verbose=0)$estimate #Mutual Information from the hand out.. #tobs = mean(log(result.12$estimate /(result.1$estimate * result.2$estimate))) #Average Mutual Information .. Moon et al. (1995) tobs = sum(result.12 * log2(result.12 /(result.1 * result.2))) print(c(tobs)) n=length(x) nsim = 500 #500 bootstrap samples.. #vector of MI for the bootstrap samples.. simval = rep(0,nsim) #vector of correlations of the bootstrap samples.. simcor=rep(0,nsim) for (i in 1:nsim) { #sample y but not x. This disturbs the joint occurrence of x and y xy=sample(y,replace=TRUE) xx = sample(x,replace=TRUE) samp = cbind(xx,xy) #to obtain confidence interval of MI both x and y are undisturbed. #xn=sample(1:n, replace=TRUE) # samp = cbind(y1 = x[xn], y2 = y[xn]) result.12 = sm.density(samp, eval.points = samp, display = "none", verbose=0) result.12 = diag(result.12$estimate) result.1 = sm.density(xx, eval.points = xx, display = "none", verbose=0)$estimate result.2 = sm.density(xy, eval.points = xy, display = "none", verbose=0)$estimate #Mutual Information from the hand out.. #simval[i] = mean(log(result.12$estimate /(result.1$estimate * result.2$estimate))) #Average Mutual Information .. Moon et al. (1995) simval[i] = sum(result.12 * log2(result.12 /(result.1 * result.2))) simcor[i]=cor(samp[,1],samp[,2]) } print(c('MI range',quantile(simval,c(0.05,0.1,.50,.90,.95)))) print(c('Cor range ',quantile(simcor,c(0.05,0.1,.50,.90,.95)))) par(mfrow=c(1,2)) zz=myboxplot(simcor,ylim=range(simcor,cor(x,y)),xlab="",ylab="Cor",plot=F) zz$names=rep("",length(zz$names)) z1=bxp(zz,xlab="",ylab="Cor",ylim=range(simcor,cor(x,y)), cex=1.25) points(z1, cor(x,y),cex=1.5,pch=16) title(main="Boxplot of bootstrap correlations") zz=myboxplot(simval,ylim=range(simval,tobs),xlab="",ylab="MI",plot=F) zz$names=rep("",length(zz$names)) z1=bxp(zz,xlab="",ylab="MI",ylim=range(simval,tobs),cex=1.25) points(z1, tobs,cex=1.5,pch=16) title(main="Boxplot of bootstrap MI") }