| library("ggplot2") | |
| library("extraDistr") | |
| library("pracma") | |
| rep = "Your repository for saving plots" | |
| imb_coef = function(y, probfunc = 'density',bdw='SJ', n_map = 100000, distfunc = 'pdf', disttype = 'cont',plot=F, p=1,k=1, w = NULL,scale=T, quad=F, save=F){ | |
| if (disttype == 'cont'){ | |
| if (scale==T) {y=(y-min(y))/(max(y)-min(y))} | |
| min_y=min(y) | |
| max_y = max(y) | |
| map = seq(min_y, max_y,length.out=n_map) | |
| if (is.null(w)){ | |
| w = rep(1,n_map) | |
| } | |
| else{ | |
| w = approx(y,w,xout=map, yleft = min(w[y==min_y]), yright = min(w[y==max_y]))$y | |
| } | |
| if (distfunc == 'pdf'){ | |
| kde_y = density(y, from=min_y, to=max_y, bw=bdw,n=n_map) | |
| y_map = kde_y$x | |
| kde_map = kde_y$y | |
| kde_map[is.na(kde_map)]=0 | |
| d_best = dunif(map,min_y,max_y) | |
| kde_x = function(x){ | |
| return(approx(kde_y$x,kde_map,xout=x)$y) | |
| } | |
| weight = function(x){ | |
| return(approx(map,w,xout=x)$y) | |
| } | |
| if (is.null(w)){ | |
| imb_ratio = round(quad(function(x) pmax(0,1-kde_x(x)), min_y, max_y,tol=1e-5),4)*100 | |
| } | |
| else{ | |
| imb_ratio = round(quad(function(x) pmax(0,1-kde_x(x))*weight(x), min_y, max_y,tol=1e-5)/quad(function(x) weight(x), min_y, max_y,tol=1e-5),4)*100 | |
| } | |
| if (plot == TRUE) { | |
| df_dens <- data.frame(map = map, kde_map = kde_map, d_best = d_best) | |
| df_hist <- data.frame(y = y) | |
| print( | |
| ggplot() + | |
| geom_histogram(data = df_hist, aes(x = y, y = ..density..), | |
| bins = 100, fill = "gray", alpha = 0.8, col = "darkblue") + | |
| geom_line(data = df_dens, aes(x = map, y = kde_map), color = "darkred", linewidth = 1) + | |
| geom_line(data = df_dens, aes(x = map, y = d_best), color = "darkgreen", linewidth = 1) + | |
| ggtitle(paste(imb_ratio, "%")) + | |
| labs(x = "map", y = "Value") + | |
| theme(plot.title = element_text(color = "darkred", size = 20, face = "bold")) | |
| ) | |
| if (exists("save") && save == TRUE) { | |
| ggsave(paste0(rep, "imbMtric_dens_", k, ".png"), width = 7.29, height = 4.5) | |
| } | |
| } | |
| return(imb_ratio) | |
| } | |
| } | |
| else if (disttype == 'dis'){ | |
| min_y=min(y) | |
| max_y = max(y) | |
| map = seq(min_y,max_y,1) | |
| if (is.null(w)){ | |
| w = rep(1,max_y-min_y+1) | |
| } | |
| else{ | |
| w=data.frame(map=y,w1=w) | |
| w = aggregate(w1~map,w,mean) | |
| temp = data.frame(map=seq(min_y,max_y),w0=rep(0,max_y-min_y+1)) | |
| temp = merge(temp,w,by="map",all.x = T) | |
| temp[is.na(temp$w1),'w1']=0 | |
| temp['w']=temp['w0']+temp['w1'] | |
| w = aggregate(w~map,temp, mean)$w | |
| } | |
| if (distfunc == 'pdf'){ | |
| kde_map = data.frame(prop.table(table(y))) | |
| prop0 = data.frame(y=map, freq0=rep(0,length(map))) | |
| kde_map = merge(prop0, kde_map,all.x=T) | |
| kde_map[is.na(kde_map$Freq),'Freq']=0 | |
| kde_map$Freq = kde_map$Freq + kde_map$freq0 | |
| kde_map$freq0=NULL | |
| d_best = ddunif(map,min_y,max_y) | |
| if (plot==T){ | |
| df <- data.frame(map = map, freq = c(data.frame(kde_map)$Freq, d_best), dist=rep(c("Emp", "Uni"), each = length(map))) | |
| error = abs(kde_map$Freq - d_best)^p * (kde_map$Freq < d_best) * w | |
| imb_ratio <- round(sum(error[w>0])/sum(d_best*w),4)*100 | |
| if (is.na(imb_ratio)){imb_ratio=100} | |
| print(ggplot(df, aes(x = factor(map), y = freq, fill = dist)) + | |
| geom_bar(stat = "identity", position = "identity", alpha = 0.5) + | |
| scale_fill_manual(values = c("darkred", "darkgreen"))+ | |
| ggtitle(paste(imb_ratio, "%")) + | |
| labs(x = "y", y = "Fréquence", fill = "Type") + | |
| theme(plot.title = element_text(color="darkred", size=20, face = "bold"))) | |
| if (save==T) {ggsave(paste0(rep,"imbMtric_mass_",k,".png"),width=7.29, height=4.5)} | |
| } | |
| return(imb_ratio) | |
| } | |
| } | |
| } | |