

### Function which takes as input a probeset matrix (columns = samples, rows = probes)
fitLM<-function(ps) {
	ps<-log2(ps) # Input is non logarithmic
	ps<-t(apply(ps,1,rank)) # Ranked values, row-wise
	colmeans<-apply(ps,2,mean) # column means
	coleff<-vector(mode="numeric") # column effects
	for (i in 1:dim(ps)[2]) {
		coleff<-c(coleff,rep(colmeans[i],dim(ps)[1]))
	}
	vps=as.vector(ps) # Transform the matrix into a vector
	model=lm(vps~coleff) # Fit a model where the values are explained by column (sample) effects
	summary(model)$adj.r.squared
}

# Good probeset (all probes grow accordingly)
goodps<-cbind(c(10,9,7,11,9),c(12,13,14,15,16),c(13,14,15,16,17))
matplot(goodps,type="b")
fitLM(goodps) # 1

# Bad probeset (probes behave randomly)
badps<-cbind(c(10,7,13,6,12),c(9,10,11,12,13),c(13,12,11,10,9))
matplot(badps,type="b")
fitLM(badps) # -0.04
