######################################################################
#### R-code for the paper: 
####
#### Modelling changes over time in a multivariate paired comparison: 
#### An application to window display design.
####
#### Alexandra Grand and Regina Dittrich
######################################################################


library(prefmod)

#### Design structure for the MPCC models saved in des
######################################################

# matrix H (8 possible response patterns)
patt <- prefmod:::gfac2(rep(2,3))
H <- ifelse(patt==2,-1,patt)

Y <- do.call("cbind", lapply(1:3, function(i) diag(1) %x% H[,i]))
B <- prefmod:::pcdesign(2)
B <- diag(3) %x% B
X <- Y %*% B

# y (counts for each of the 8 possible response patterns)
y<-c(1,0,5,7,3,2,10,10)

mu <- gl(1, length(H[,1]))


### two-way interactions between attributes

# focus (1) and optical balance (2)
Z.12 <- diag(1) %x% apply(H[,1:2],1,prod)

# optical balance (2) and triangle (3)
Z.23 <- diag(1) %x% apply(H[,2:3],1,prod)

# focus (1) and triangle (3)
Z.13 <- diag(1) %x% apply(H[,-2],1,prod)

Z<- cbind(Z.12,Z.23,Z.13)


### one common parameter for time 1 (t1) and time 2 (t2)

t1<-B[,1]+B[,3]+B[,5]
t2<-B[,2]+B[,4]+B[,6]
b<-cbind(t1,t2)
c<-Y%*%b



des <- data.frame(y, mu, X, Z,c)
   names(des)<- c("y","mu","focus.t1","focus.t2","optb.t1","optb.t2","triang.t1","triang.t2",
                   "i12","i23","i13","t1","t2")
attach(des)



#### MPCC models (model A, model B, model C, model D)
#####################################################

# model A
mA<-gnm(y~focus.t2+focus.t1+optb.t2+optb.t1+triang.t2+triang.t1+
          i12+i23+i13,eliminate=mu,family=poisson,data=des)

# model B
i1323<-i13+i23
mB<-gnm(y~focus.t2+focus.t1+optb.t2+optb.t1+triang.t2+triang.t1+
          i12+i1323,eliminate=mu,family=poisson,data=des)

# model C
mC<-gnm(y~focus.t2+focus.t1+optb.t2+optb.t1+triang.t2+triang.t1,
          eliminate=mu,family=poisson,data=des)

# model D
mD<- gnm(y~t2+t1, eliminate=mu,family=poisson,data=des)

