# TODO: Add comment
# 
# Author: E.Korsching  2016
###############################################################################



test.sampling.jitter <- function(x,z,number,rnd.after=T){
	# test how raw data vectors with low differences behave
	# after jitter construction and sampling in their cor values
	# x: data.frame with raw data, z: selection of two cols, number: number of sampling tries, rnd.after: do further random adjustment in height
	
	#ini
	nrx <- nrow(x)
	ncx <- ncol(x)
	
	var.tab <- apply(x,2,var)[order(apply(x,2,var))]	#calculate all ordered (0 ascending) variances for appropriate small jitter
	var.tab.lowest <- var.tab[var.tab>0][1]		#pick lowest value above 0
	
	res.mat <- matrix(0,number,7)
	res.mat2 <- matrix(0,number*2,nrx)
	
	#
	for(i in 1:number){
		z1 <- x[,z[1]]
		z2 <- x[,z[2]]
		res.mat[i, 1] <- i
		tmp <- cor(z1, z2)
		res.mat[i, 2] <- tmp
		if(sum(is.na(tmp))>=1){			## cor: check if NA: true , the case -> than apply some jitter
			## pay attention: default jitter() fn is not appropriate: variable results ! e.g. global ssq from 167 to 202
			res.mat[i, 3] <- var(z1)
			res.mat[i, 4] <- var(z2)
			if(res.mat[i, 3]==0){
				cat("\n A")
				if(length(z1)%%2==1){ tmp2 <- c(rep(c(0,var.tab.lowest/10),length(z1)%/%2),0) }else{ tmp2 <- rep(c(0,var.tab.lowest/10),length(z1)%/%2) }
				#avoid (foremost by sampling/bootstrap approaches) the effect that a x vec with low alterations might become identical to
				# a y vec with also less to no alterations (result: cor -> 1, which is wrong)
				tmp2 <- sample(tmp2)	# therefore sampling here ...
				# but not sufficient normal therefore some noise
				if(rnd.after){ tmp2 <- tmp2 * runif(n=length(tmp2),min=-0.1,max=0.1) }
				res.mat2[((i*2)-1), ] <- tmp2
				z1 <- z1 + tmp2	#regular lowest variance/n jitter added
			}
			if(res.mat[i, 4]==0){
				cat("\n B")
				if(length(z2)%%2==1){ tmp2 <- c(rep(c(0,var.tab.lowest/10),length(z2)%/%2),0) }else{ tmp2 <- rep(c(0,var.tab.lowest/10),length(z2)%/%2) }
				tmp2 <- sample(tmp2)	# ... and here
				# but not sufficient normal therefore some noise
				if(rnd.after){ tmp2 <- tmp2 * runif(n=length(tmp2),min=-0.1,max=0.1) }
				res.mat2[(i*2), ] <- tmp2
				z2 <- z2 + tmp2	#regular lowest variance/n jitter added
			}
			res.mat[i, 5] <- var(z1)
			res.mat[i, 6] <- var(z2)
			res.mat[i, 7] <- cor(z1,z2)
		}
	}
	
	return(list(res.mat,res.mat2))
}


#test.h.jit01 <- test.sampling.jitter(x=horstLCA.2[,c(1:11,14,17:20)],z=c(13,12),number=1000,rnd.after=T)		#4,5 12,13,14  CK1,CK10, AR,ER,PR


#hist.plot.simple(x=test.h.jit01[[1]][ ,7],bin.num=100,bin.size=F,xlab="",xlim=c(-0.4,0.4),y.max=NULL,x.at=c(-0.2,0,0.2),y.at=c(1,10,50,100),y.num.format=F,ylog=F,
#		bar.width=1,offset=0,digits=1,col=c("red","green","blue"),cex=1,norm=F,add=F,n.factor=1,curve=F,smooth.f=0,
#		subfolder.filename=NULL,h.title="test cor raw-jitter-sampling")

#descision -> before skipping certain raw data cols (asumption: by applying the cor measure) the best estimate is to set cor to nearly 0 in the case of var=0


####
#apply(horstLCA.2[,c(1:11,14,17:20)],2,var)[order(apply(horstLCA.2[,c(1:11,14,17:20)],2,var))]	#calculate all ordered (0 ascending) variances



