# TODO: Add comment
# 
# Author: E.Korsching Dec 7, 2012
###############################################################################



gliding.proportions <- function(x){
	# setting stepwise different thresholds in a vector structure
	#  assumed that every cell counts as a step
	# x: vector (min 2 entries)
	#  return values: a value below and above the threshold for each step
	
	#ini
	nx <- length(x)
	if(nx<2){ cat("\nMinimum vector length of two"); return() }
	
	#
	erg <- data.frame(matrix(0,nx-1,2))
	
	#fill proportions
	for(i in 2:nx){
		erg[i-1,1] <- sum(x[(1:(i-1))])		#pos1 part
		erg[i-1,2] <- sum(x[(i:nx)])		#end part
	}
	
	#plot
	par(mfrow=c(2,2))
	
	#proportions
	y.max <- max(erg)
	plot(0,0, xlim=c(0,nx), ylim=c(0,y.max), xlab="sequence of thresholds", ylab="counts", type="n", axes=T)
	leg.str <- c("pos1","end")
	lp.col <- palette()[c(2,3)]
	lp.pch <- c(24,25)
	for(i in 1:(nx-1)){
		points(x=i, y=erg[i,1], type="p", col=lp.col[1], cex=0.5, pch=lp.pch[1])		#pos1
		points(x=i, y=erg[i,2], type="p", col=lp.col[2], cex=0.5, pch=lp.pch[2])		#end
	}
	legend(x=nx*0.25, y=y.max*0.65, legend=leg.str,
			col=lp.col, border="black", pch=lp.pch,
			bty="o", title=NULL, cex=0.5)
	
	
	#
	return(erg)
}


# ac <- gliding.proportions(x=c(1,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9))



