################################################################## # ## updated revision on 2016/05/20 ## functions for computer information in Tsai and Gilmour (2010) ## ## cal.Q3L(d) -- Q-value for a three-level design ## cal.gwc(d) -- generalized word count for a three-level design ## cal.Qb3L(d, p) -- Qb-value for three-level designs coded with a given prior p ## cal.Db3L(d, p, tau) -- Q-value for three-level designs coded with a given ## prior p and tau ## ## details can be found in gwc_supp.pdf ## data files used in the supplimentary ## ## Col_F6R19.out for all six three-level designs in 18 runs ## pb16.out: 16-run two-level Placket-Burman design ## Qb18T6.out: the four six-factor designs studied ## ################################################################## dim2=function(mat) { dim(mat)[2] } myFun = function(arg){ prod(arg) } proj.calc = function(ind.col, Design) { sub.matrix=as.matrix(Design[, ind.col] ) prodV = apply(sub.matrix, 1, myFun) tmp=sum(prodV)^2 return(tmp) } matrix.calc.fin = function(data, noRun) { prodV = apply(data, 1, myFun) (sum(prodV))^2/(noRun*noRun) } num.mod2 = function(a.min, noCol, noRun) { #number of model for first order maximal model tmp=0 a.bound.min=min((noRun-1), noCol) if (a.min<=a.bound.min) { for (a in a.min:a.bound.min) { tmp = tmp + choose(noCol-a.min, a-a.min) } } } F.mod2.int = function(c.val, a, a.min, c.min, c.max, noCol, noRun) { if (c.val+a < noRun ) { ret = choose(noCol-a.min, a-a.min) * choose(c.max-c.min, c.val-c.min) } else { ret=0 } } delta2.int = function(ind, noCol, noRun) { #for two-level second-order maximal model #apply function F.mod to find delta for each combination of a,c a.bound.min=min(noRun-1, noCol) tmp=0 if (ind[1] <=noCol) { for(a in ind[1]:a.bound.min) { c.max=choose(a,2) c.ind=ind[2]:c.max tmp=tmp+sum(unlist(lapply(c.ind, F.mod2.int, a, ind[1], ind[2], c.max, noCol, noRun))) } } (tmp) } matrix.calc.real = function(nMatrix, combList, vec, myArray, noRun) { matrix.selected = c() for(i in 1:nMatrix) { combSlice = combList[[i]] matrix.selected = cbind(matrix.selected, myArray[, combSlice[vec[i],], i]) } myRes = matrix.calc.fin(matrix.selected, noRun) myRes } matrix.calc.rec = function(nMatrix, depth, combList, vec, colUnion, myArray, noRun) { ret = c() if(depth==nMatrix+1) { ret = matrix.calc.real(nMatrix, combList, vec, myArray, noRun) } else { combSlice = combList[[depth]] if(dim(combSlice)[1]==0) { ret = c(ret, matrix.calc.rec(nMatrix, depth+1, combList, c(vec,0), colUnion, myArray, noRun)) } else { for(i in 1:dim(combSlice)[1]) { if( length(colUnion)==0 || length(intersect(colUnion, combSlice[i,]))==0) ret = c(ret, matrix.calc.rec(nMatrix, depth+1, combList, c(vec, i), union(colUnion, combSlice[i,]), myArray, noRun)) } } } ret } matrix.choose.vector = function(nMatrix, maxCount, cv, myArray, noRun) { for(i in 1:nMatrix) { if(i==1) { if(cv[i]==0) combList = list(matrix(0,0,0)) else combList = list(t(combn(maxCount, cv[i]))) } else { if(cv[i]==0) combList = append(combList, list(matrix(0,0,0))) else combList = append(combList, list(t(combn(maxCount, cv[i])))) } } ret = matrix.calc.rec(nMatrix, 1, combList, c(), c(), myArray, noRun) ind = unlist(lapply(combList, dim2)) c(sum(ind), ind, sum(ret)) } matrix.choose.count = function(nMatrix, maxCount, totalCount, myArray, noRun) { ceiling = (maxCount + 1) ^ nMatrix cv = rep(0, nMatrix) ret = c() for(counter in 1:(ceiling-1)) { val = counter for(i in nMatrix:1) { cv[i] = val %% (maxCount + 1) val = val %/% (maxCount + 1) } if(sum(cv)==totalCount) { ret = rbind(ret, matrix.choose.vector(nMatrix, maxCount, cv, myArray, noRun)) } } ret } mod.num.calc=function(c.val, a.val, c.max, a.min, c.min, noF){ mod.num.calc=choose(noF-a.min, a.val-a.min)*choose(c.max-c.min, c.val-c.min) } mod.num=function(ac, a.min, c.min, noF){ #two-level design a.val=ac[1] c.max=ac[2] unlist(lapply(c(0:c.max), mod.num.calc, a.val, c.max, a.min, c.min, noF)) } F.mod = function(bc, a, a.min, b.min, c.min, c.max, noCol, noRun) { # number of models with a.min, b.min, c.min effects included and # whose number of parameters are less than run size if (a + sum(bc) < noRun) { ret = c(a, bc, choose(noCol-a.min, a-a.min) * choose(a-b.min, bc[1]-b.min) * choose(c.max-c.min, bc[2]-c.min)) } else { ret = numeric(4) } ret } mod.delta=function(abc, noCol, noRun){ #for second-order maximal model #apply function F.mod to find delta for each combination of a,b,c a.min=abc[1] b.min=abc[2] c.min=abc[3] a.bound.min=min(noRun-1,noCol) tmp=0 if (a.min<=noCol) { for(a in a.min:a.bound.min) { b.max=a c.max=choose(a,2) bc.max = noRun - a a1 = matrix( rep(b.min:b.max, c.max - c.min + 1), c.max - c.min + 1, b.max - b.min + 1, byrow=T) a2 = matrix( rep(c.min:c.max, b.max - b.min + 1), c.max - c.min + 1, b.max - b.min + 1) A = array(c(a1,a2), c(c.max - c.min + 1, b.max - b.min + 1, 2) ) R= apply(A, c(1,2), F.mod, a, a.min, b.min, c.min, c.max, noCol, noRun) tmp= tmp + sum(R[4,,]) } } else {tmp=0} tmp } delta.3.qual=function(a.min ,noCol, noRun) { a=1:noCol a.bound=length(a[(2*a+choose(a,2)*4)= noRun pr.gamma=sum(prmat[ind,6]) mod.gamma=sum(prmat[ind,4]) if (any(ind)==TRUE) adj=pr.gamma/mod.gamma else adj=0 pr=prmat[,5] ind2=apply(prmat[,c(1,2,3)],1, sum)==(noRun-1) pr[ind2]=pr[ind2]+adj pr[ind]=0 pradj=cbind(prmat[,c(1:4)], pr) list(pradj=pradj, adj=adj) } plot.hist=function(pp, noF=6, noRun=18) { prmat=pr.mat(pp, noF, noRun) pr.hist=prmat[,4]*prmat[,5] nopara=2*noF+noF*(noF-1)/2+1 pr=numeric(nopara-1) for (i in 1:(nopara-1)) { pr[i]=sum(pr.hist[apply(prmat[,c(1,2,3)],1, sum)==i]) } pr=c(0,pr) lab1=sum(pr[1:(noRun)]) barplot(pr,col="wheat", ylim=c(0,(max(pr)+.05)), xlim=c(0,nopara+1), xlab="No of parameters", ylab="Probability", main="Probability for different models being the true model, P(Size<18)") abline(v=(noRun+1), col=2,lwd=2) text(noRun-1, max(pr)+0.03, paste(round(100*lab1,2), "%",sep="")) } F.pr.mod=function(bc, a, a.min, b.min, c.min, c.max, adj, pp, noF, noRun) { p1=pp[1] p2=pp[2] p3=pp[3] b=bc[1] c=bc[2] if (a + sum(bc) < (noRun-1)) { tmp=p1^a*(1-p1)^(noF-a)*p2^b*(1-p2)^(a-b)*p3^c*(1-p3)^(c.max-c) nu.mod = choose(noF-a.min, a-a.min) * choose(a-b.min, bc[1]-b.min) * choose(c.max-c.min, bc[2]-c.min) ret=c(a, bc, nu.mod, tmp, tmp*nu.mod) } else if (a + sum(bc) == (noRun-1)) { tmp=p1^a*(1-p1)^(noF-a)*p2^b*(1-p2)^(a-b)*p3^c*(1-p3)^(c.max-c)+adj nu.mod = choose(noF-a.min, a-a.min) * choose(a-b.min, bc[1]-b.min) * choose(c.max-c.min, bc[2]-c.min) ret=c(a, bc, nu.mod, tmp, tmp*nu.mod) } else ret=numeric(6) ret } adj.xi=function(abc, adj, pp, noF, noRun) { a.min=abc[1] b.min=abc[2] c.min=abc[3] a.bound.min=min(noRun-1,noF) tmp=0 if (a.min<=noF) { for(a in a.min:a.bound.min) { b.max=a c.max=choose(a,2) bc.max = noRun - a b.max=a c.max=choose(a,2) bc.max = noRun - a a1 = matrix( rep(b.min:b.max, c.max - c.min + 1), c.max - c.min + 1, b.max - b.min + 1, byrow=T) a2 = matrix( rep(c.min:c.max, b.max - b.min + 1), c.max - c.min + 1, b.max - b.min + 1) A = array(c(a1,a2), c(c.max - c.min + 1, b.max - b.min + 1, 2) ) R=apply(A, c(1,2), F.pr.mod, a, a.min, b.min, c.min, c.max, adj, pp, noF, noRun) tmp= tmp + sum(R[6,,]) } } else {tmp=0} tmp # print(tmp) } cal.Wmat=function(xi.s, noF, noRun) { s=rep(0,14) s[1]=xi.s[1] s[2]=xi.s[2] s[3]=xi.s[3] s[4]=xi.s[3] s[5]=xi.s[4] s[6]=xi.s[6] s[7]=xi.s[5] s[8]=xi.s[6] s[9]=xi.s[7] s[10]=xi.s[8] s[11]=xi.s[9] s[12]=xi.s[10] s[13]=xi.s[11] s[14]=xi.s[12] n=noF sz2=(n+1)*(n+2)/2 W.mat=matrix(0, nrow=sz2, ncol=sz2) out=combn(noF, 2) ind_a=out[1,] ind_b=out[2,] # i1=1 # ind_a=c() # ind_b=c() # while (i1=noRun) { tmp.prob=tmp.prob+A[[i+1]][j+1] } if ((i+j)==(noRun-1)) { tmp.num=tmp.num+mod[[i+1]][j+1]} } } prob.adj=tmp.prob/tmp.num for (i in 1:noF) { for (j in 0:c.ind[i+1]) { if ((i+j)>=noRun) { prob[[i+1]][j+1]=0 } if ((i+j)==(noRun-1)) { prob[[i+1]][j+1]= prob[[i+1]][j+1]+prob.adj } } } #adjust gamma to model of a size smaller. A=matrix(0,0,0) for (i in a.ind) { prb.m=mod[[i+1]]*prob[[i+1]] A=append(A, list(prb.m)) } for (i.ind in 1:xi.size){ a.min=xi.ind[1,i.ind] c.min=xi.ind[2,i.ind] mod.ad=apply(rbind(a.ind, c.ind), 2, mod.num, a.min, c.min, noF) A.ad=matrix(0,0,0) for (i in a.ind) { prb.m=mod.ad[[i+1]]*prob[[i+1]] A.ad=append(A.ad, list(prb.m)) } pij=rbind(pij, sum(unlist(A.ad),na.rm=TRUE)) } return(pij) } cal.Qb2L=function(p1,p2,d){ noCol=ncol(d) noRun=nrow(d) b.word=numeric(noCol) for( TC in 1:noCol) { out=t(combn(noCol,TC)) b.word[TC]=sum(apply(out, 1, proj.calc, d))/(noRun*noRun) } pij=prob.lev2(p1, p2, noCol, noRun) if (noCol==3) { Q.B=( b.word[1]*( pij[2] + 2*(noF-1)*pij[4] )+ b.word[2] * ( 2*pij[3] + pij[4]+ 2*(noF-2)*pij[6] ) + b.word[3] * 6 * pij[5] ) / pij[1] } else { Q.B =( b.word[1]*( pij[2] + 2*(noF-1)*pij[4] )+ b.word[2] * ( 2*pij[3] + pij[4]+ 2*(noF-2)*pij[6] ) + b.word[3] * 6 * pij[5] + b.word[4] * 6 * pij[7] ) / pij[1] } return(list(Q.b=Q.B, b.word=b.word, pij=pij)) }