PRESS = function(g) sum(rstandard(g, type="pred")^2)
R2pred = function(g) {y=g$model[,1]; 1- PRESS(g)/sum((y-mean(y))^2) }
cmp.g = function(g) {
pred = predict(g, dat.test)
Rfit = cor(fitted(g), g$model[,1])
Rtest = cor(pred, dat.test$y)
Rmix = cor(c(fitted(g), pred), c(g$model[,1], dat.test$y))
gs = summary(g)
c(g$rank, g$df, gs$r.squared, gs$adj.r.squared, R2pred(g), gs$sigma, AIC(g), BIC(g), Rmix, Rtest)
}
permutations.R=function(n,v=1:n)
{
if (n <= 0) NULL
else if (n == 1) matrix(v[1:n], 1, n)
else if (n == 2) matrix(c(v[1:n], v[n:1]), 2, n)
else {
xi=NULL
for(i in 1:n) xi= rbind(xi, cbind(v[i], Recall(n-1,v[-i])))
xi
}
}
perm.base1=function(perm, base=n, debug=F, ...)
{
n=length(perm)
x =matrix(0,n,n)
for(i in 1:n) x[i, perm[i]] = 1
if(debug)print(cbind(x))
if(base>0) z=c(x[-base,-base])
else z=c(x)
lab=outer(LETTERS[1:n], 1:n, paste, sep="")
lab=t(lab)
if(base>0) lab=c(lab[-base,-base])
names(z)=lab
z
}
perm.contr1=function(perm, base=n, debug=F, ...)
{
n=length(perm)
x =matrix(0,n,n);
for(i in 1:n) x[i, perm[i]] = 1
y = sweep(x, 1, x[,base])
z = sweep(y, 2, y[base,])
if(debug)print(cbind(x,y,z))
z=c(z[-base,-base])
lab=outer(LETTERS[1:n], 1:n, paste, sep="")
lab=t(lab)
lab=c(lab[-base,-base])
names(z)=lab
z
}
perm.contr=function(perms, contr=perm.contr1, debug=F, ...)
{
if(is.vector(perms)) contr(perms, debug=debug, ...)
else t(apply(perms, 1, contr, ...))
}
perm.pair2=function(perm, debug=F, ...)
{
n=length(perm)
x =matrix(0,n,n)
for(i in 1:(n-1)) for(j in (i+1):n)
{
if(perm[i]<perm[j]) x[perm[i], perm[j]] = (j-i)
else x[perm[j], perm[i]] = -(j-i)
}
if(debug)print(x)
z=x[row(x)<col(x)]
lab=outer(1:n, 1:n, paste, sep=".")
lab=c(lab[row(x)<col(x)])
lab=paste("I",lab, sep="")
names(z)=lab
z
}
perm.pairwise=function(perm, debug=F, ...)
{
sign(perm.pair2(perm, debug=debug, ...))
}
cp.plot=function(ybar, perms, xlab="Position", ylab="Mean Response", pch=LETTERS[1:m], ...)
{
m=ncol(perms)
tab=matrix(0,m,m)
for(i in 1:m) tab[i,] = xtabs(ybar~perms[,i], drop=T)
df=(tab/(nrow(perms)/m))
df.mean=mean(df)
df=as.data.frame(df)
matplot(1:m, df,type="o", xlab=xlab, ylab=ylab, pch=pch, ...)
abline(h=df.mean)
}
ex=function()
{
perm.contr1(c(2,1,3), base=3, debug=T)
perm.contr(c(2,1,3), contr=perm.contr1, debug=T)
perm.contr(c(2,1,3), contr=perm.base1, debug=T)
perm.contr(c(2,1,3), contr=perm.base1, base=0, debug=T)
perm.contr(c(2,1,3), contr=perm.pairwise, debug=T)
}
dat0=read.table("coa24x4c.txt", h=T); dat0
dat0[,2:5]=dat0[,2:5]+1
perms=as.matrix(dat0[,2:5])
dat0$batch=ifelse(dat0$coa12=="*", 1, 2)
sub1=(1:24)[dat0$batch==1]
sub2=(1:24)[dat0$batch==2]
par(mfrow=c(1,3))
pch=c("0","1", "2", "3")
cp.plot(dat0$y, perms, pch=pch, main="Both", ylim=c(35,55))
cp.plot(dat0$y[sub1], perms[sub1,], pch=pch, main="COA1", ylim=c(35,55))
cp.plot(dat0$y[-sub1], perms[-sub1,], pch=pch, main="COA2", ylim=c(35,55))
X0=perm.contr(perms, contr=perm.base1, base=0)
X1=perm.contr(perms, contr=perm.pairwise)
dat0=as.data.frame(cbind(dat0, X0, X1))
dat=dat0; dat.test=dat0
g.cpm=g=g1=lm(y ~ A1+B1+C1+A2+B2+C2+A3+B3+C3, dat); summary(g)
g=g1=lm(y ~ 1, dat);
g=step(g1, scope =list(upper = ~(A1+B1+C1+D1+A2+B2+C2+D2+A3+B3+C3+D3+A4+B4+C4+D4), lower = ~1), direction="both", k=log(nrow(dat)), trace=0); summary(g)
g.cpm1=g=lm(formula = y ~ C4 + B1 + D1 + B2 , data = dat); summary(g);
g.pwo=g1=g=lm(y ~ (I1.2 + I1.3 + I2.3 + I1.4 + I2.4 + I3.4), dat); summary(g1, cor=F)
g=g1=lm(y ~ 1, dat);
g=step(g1, scope =list(upper = ~(I1.2 + I1.3 + I2.3 + I1.4 + I2.4 + I3.4), lower = ~1), direction="both", k=log(nrow(dat)), trace=0); summary(g)
g.pwo1=g=lm(formula = y ~ I2.3 + I3.4 + I2.4, data = dat); summary(g);
out=rbind(cmp.g(g.cpm), cmp.g(g.cpm1), cmp.g(g.pwo), cmp.g(g.pwo1))
dimnames(out)[[1]]=c("CPM", "CPM1", "PWO","PWO1")
dimnames(out)[[2]]=c("p", "DF", "Rsquare", "R.Adj", "R2pred", "Sigma","AIC", "BIC", "Rmix", "Rtest")
round(out,2)
dat=dat0
par(mfrow=c(1,3))
plot(resid(g)~fitted(g), xlab="Fitted values", ylab="Residuals", ylim=c(-6,10.5))
g1=lm(y ~A1+B1+C1+A2+B2+C2+A3+B3+C3, dat, sub=sub1); summary(g1, cor=F)
pred=predict(g1, dat); res = dat$y - pred
plot(res ~ pred, xlab="Fitted values", ylab="Residuals", ylim=c(-6,10.5))
cor(dat$y, pred)
cor(dat$y[-sub1], pred[-sub1])
g2=lm(y ~A1+B1+C1+A2+B2+C2+A3+B3+C3, dat, sub=sub2); summary(g1, cor=F)
pred=predict(g2, dat); res = dat$y - pred
plot(res ~ pred, xlab="Fitted values", ylab="Residuals", ylim=c(-6,10.5))
cor(dat$y, pred)
cor(dat$y[-sub2], pred[-sub2])
dat0=read.table("coa40x5b.txt", h=T)[,-1];
dat0
dat0$batch=rep(0:1, c(20,20))
dimnames(dat0)[[2]][6]="y"
perms=as.matrix(dat0[,1:5]+1)
X0=perm.contr(perms, contr=perm.base1, base=0)
X1=perm.contr(perms, contr=perm.pairwise)
dat0=as.data.frame(cbind(dat0,X0, X1))
dat=dat0; dat.test=dat0
g.cpm=g1=g=lm(y ~batch+(A1+B1+C1+D1+A2+B2+C2+D2+A3+B3+C3+D3+A4+B4+C4+D4), dat); summary(g1, cor=F);
g=g1=lm(y ~ 1, dat);
g=step(g1, scope =list(upper = ~batch+(A1+B1+C1+D1+E1+A2+B2+C2+D2+E2
+A3+B3+C3+D3+E3+A4+B4+C4+D4+E4+A5+B5+C5+D5+E5)^2, lower = ~1),
direction="both", k=2, trace=0); summary(g)
g.cpm2=g=lm(formula = y ~ batch + C1 + E1 + E2 + C2 + D1 + E3 + E1:C2 +
E2:D1 , data = dat); summary(g)
g.pwo=g1=g=lm(y ~ batch+(I1.2 + I1.3 + I2.3 + I1.4 + I2.4 + I3.4 + I1.5 + I2.5 + I3.5 + I4.5), dat); summary(g1, cor=F)
g=g1=lm(y ~ 1, dat);
g=step(g1, scope =list(upper = ~batch+(I1.2 + I1.3 + I2.3 + I1.4 + I2.4 + I3.4 + I1.5 + I2.5 + I3.5 + I4.5)^2, lower = ~1), data=dat, direction="both", k=2, trace=0); summary(g)
g.pwo2=g=lm(formula = y ~ batch + I3.4 + I2.5 + I1.5 + I2.3 + I2.4 + I1.5:I2.3 + I2.5:I2.3 + I3.4:I2.4, data = dat); summary(g)
out=rbind(cmp.g(g.cpm), cmp.g(g.cpm2), cmp.g(g.pwo), cmp.g(g.pwo2))
dimnames(out)[[1]]=c("CPM", "CPM2", "PWO", "PWO2")
dimnames(out)[[2]]=c("p", "DF", "Rsquare", "R.Adj", "R2pred","Sigma", "AIC", "BIC", "Rmix", "Rtest")
round(out,2)
par(mfrow=c(1,2))
g=g.cpm
plot(resid(g) ~ fitted(g), xlab="Fitted values", ylab="Residuals")
cp.plot(dat$y, perms, pch=c("0","1", "2", "3", "4"))
perms5all=permutations.R(5)
Sequence5all=apply(perms5all-1,1, function(x) paste(x,sep="",collapse =""))
X0=perm.contr(perms5all, contr=perm.base1, base=0)
X1=perm.contr(perms5all, contr=perm.pairwise)
df.X1= as.data.frame(cbind(batch=0,X0, X1))
pred=cbind(cpm=predict(g.cpm,df.X1),cpm2=predict(g.cpm2,df.X1), pwo=predict(g.pwo,df.X1), pwo2=predict(g.pwo2,df.X1))
round(cor(pred),2)
res=as.data.frame(cbind(perms5all-1, Sequence5all, round(pred,2)));
ii=4
res1=res[rev(order(pred[,ii])),][1:20,]; dimnames(res1)[[1]]=1:nrow(res1); res1
vuongtest.adj <- function (object1, object2, nested = FALSE, adj = "none", ll1 = llcont, ll2 = llcont, score1 = NULL, score2 = NULL, vc1 = vcov, vc2 = vcov)
{
obinfo <- check.obj(object1, object2)
callA <- obinfo$callA
classA <- obinfo$classA
callB <- obinfo$callB
classB <- obinfo$classB
llA <- ll1(object1)
llB <- ll2(object2)
if (nested) {
if (sum(llB, na.rm = TRUE) > sum(llA, na.rm = TRUE)) {
tmp <- object1
object1 <- object2
object2 <- tmp
tmp <- llA
llA <- llB
llB <- tmp
tmp <- callA
callA <- callB
callB <- tmp
}
}
nmis <- sum(is.na(llA))
n <- NROW(llA) - nmis
omega.hat.2 <- (n - 1)/n * var(llA - llB, na.rm = TRUE)
lamstar <- calcLambda(object1, object2, n, score1, score2,
vc1, vc2)
pOmega <- imhof(n * omega.hat.2, lamstar^2)$Qq
lr <- sum(llA - llB, na.rm = TRUE)
teststat <- (1/sqrt(n)) * lr/sqrt(omega.hat.2)
testadj.factor = (1/sqrt(n)) /sqrt(omega.hat.2)
if (classA %in% c("SingleGroupClass", "MultipleGroupClass")) {
nparA <- mirt::extract.mirt(object1, "nest")
}
else {
nparA <- length(coef(object1))
}
if (classB %in% c("SingleGroupClass", "MultipleGroupClass")) {
nparB <- mirt::extract.mirt(object2, "nest")
}
else {
nparB <- length(coef(object2))
}
if (adj == "aic") {
teststat <- teststat - (nparA - nparB)*testadj.factor
}
if (adj == "bic") {
teststat <- teststat - (nparA - nparB) * log(n)/2 *testadj.factor
}
if (nested) {
teststat <- 2 * lr
pLRTA <- imhof(teststat, -lamstar)[[1]]
pLRTB <- NA
}
else {
pLRTA <- pnorm(teststat, lower.tail = FALSE)
pLRTB <- pnorm(teststat)
}
rval <- list(omega = omega.hat.2, p_omega = pOmega, LRTstat = teststat,
p_LRT = list(A = pLRTA, B = pLRTB), class = list(class1 = classA,
class2 = classB), call = list(call1 = callA, call2 = callB),
nested = nested)
class(rval) <- "vuongtest"
return(rval)
}
environment(vuongtest.adj) <- asNamespace("nonnest2")
vuongtest.adj(g.cpm1,g.pwo1, adj="bic")
vuongtest.adj(g.cpm2,g.pwo2, adj="bic")