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



test.var.cov.cor <- function(x){
	# test properties of raw data vectors
	# x: raw data columns
	
	# ini
	nr <- nrow(x)
	nc <- ncol(x)
	res <- matrix(0,)
	
	x.c.var <- apply(x,2,var)
	x.c.var <- x.c.var[order(x.c.var)]	# calculate all ordered (0 -> ascending) column variances for appropriate small jitter
	x.cov <- cov(x)
	x.cor <- cor(x)
	return(list(rows=nr, cols=nc, x.c.var=x.c.var, x.cov=x.cov, x.cor=x.cor))
}




test.sample.noise.cor <- function(x, n.sample, t.num=0){
	# test how raw data vectors with low variation behave on cor() (even cor=NA)
	#  1) permutation of data vectors and 2) (after=T) adding some noise
	# avoid (foremost by sampling/bootstrap approaches) the effect that a x vec with low variation might become identical to
	# a y vec with identical/similar low variation (result: cor ~=> 1, which is biologically wrong)
	# x: two "raw data" columns
	# n.sample: number of sampling tries
	# t.num: noise vector created around this number
	nr <- nrow(x)
	res.mat <- matrix(0, n.sample, 7)
	dimnames(res.mat)[[2]] <- c("i","var-z1","var-z2","cor-z1-z2","s-var-z1","s-var-z2","s-cor-z1-z2")
	# n samples of the two columns
	for(i in 1:n.sample){
		z1 <- x[,1]
		z2 <- x[,2]
		res.mat[i, 1] <- i
		varz1 <- var(z1)
		res.mat[i, 2] <- varz1
		varz2 <- var(z2)
		res.mat[i, 3] <- varz2
		corz1z2 <- cor(z1, z2)
		res.mat[i, 4] <- corz1z2
		if(varz1<varz2){ var.lowest <- varz1 }else{ var.lowest <- varz2 }
		if(is.na(corz1z2)){			## cor: check if NA: true -> then create a noise vector around
			# note: default jitter() fn is not appropriate here
			if(varz1==0){
				# some noise
				z1 <- runif(n=nr, min=(t.num-0.1), max=(t.num+0.1))
			}
			if(varz2==0){
				# some noise
				z2 <- runif(n=nr, min=(t.num-0.1), max=(t.num+0.1))
			}
			res.mat[i, 5] <- var(z1)
			res.mat[i, 6] <- var(z2)
			res.mat[i, 7] <- cor(z1,z2)
		}
	}
	return(res.mat)
}


#bc8.j01 <- test.var.cov.cor(x=aa1)
#bc8.j02 <- test.sample.noise.cor(x=cbind(aa1[,"Ck10"], sim=rep(1,955)), n.sample=1000, t.num=0)

#hist.plot.color(x=bc8.j02[,7], bin.num=100, x.range=NULL, lty=3, lwd=1,
#		xlab="cor values", ylab="", xlim=c(-0.15,0.15), y.max=NULL, x.at=c(-0.1,-0.05,0,0.05,0.1), y.at=c(1,50,100,200), ylog=F, bar.width=0.8, offset=0, digits=1,
#		col.grad=c("red","blue"), col.f="blue", col.sh=NULL, col.b=NA, cex=1, h.title="test cor low variance", add=F, rC=F)
#abline(v=0, lty=2, col="red")

#descision -> before skipping certain raw data cols (cor=NA)
#				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


#cor(c(1,1,1,1), c(2,2,2,2))		# NA
#cor(c(1,1,1,1), c(2,3,2,2))		# NA
#cor(c(1,1,1,1), c(2,3,1,4))		# NA
#
#cor(c(1,2,1,1), c(2,3,2,2))		# 1
#cor(c(1,2,1,1), c(2,3,2,2))		# 1
#
#var(c(1,2,1,1), c(2,3,2,2)); var(c(1,2,1,1,2,3,2,2))
#var(c(1,2,1,1)); var(c(2,3,2,2))
#cov(c(1,2,1,1), c(2,3,2,2))
#cov(cbind(c(1,2,1,1), c(2,3,2,2)) )
#cov(cbind(c(1,2,1,1), c(2,3,2,2), c(3,4,3,3)) )



