if (FALSE) {
## a synthetic data example
set.seed(123)
I <- 65 ## number of raters
J <- 50 ## number of items to be compared
## raters 1 to 5 put most weight on dimension 1
## raters 6 to 10 put most weight on dimension 2
## raters 11 to I put substantial weight on both dimensions
gamma.true <- c(runif(5, 0, 0.1),
runif(5, 1.47, 1.57),
runif(I-10, 0.58, 0.98) )
theta1.true <- rnorm(J, m=0, s=1)
theta2.true <- rnorm(J, m=0, s=1)
theta1.true[1] <- 2
theta2.true[1] <- 2
theta1.true[2] <- -2
theta2.true[2] <- -2
theta1.true[3] <- 2
theta2.true[3] <- -2
n.comparisons <- 125 ## number of pairwise comparisons for each rater
## generate synthetic data according to the assumed model
rater.id <- NULL
item.1.id <- NULL
item.2.id <- NULL
choice.id <- NULL
for (i in 1:I){
for (c in 1:n.comparisons){
rater.id <- c(rater.id, i+100)
item.numbers <- sample(1:J, size=2, replace=FALSE)
item.1 <- item.numbers[1]
item.2 <- item.numbers[2]
item.1.id <- c(item.1.id, item.1)
item.2.id <- c(item.2.id, item.2)
z <- c(cos(gamma.true[i]), sin(gamma.true[i]))
eta <- z[1] * (theta1.true[item.1] - theta1.true[item.2]) +
z[2] * (theta2.true[item.1] - theta2.true[item.2])
prob.item.1.chosen <- pnorm(eta)
u <- runif(1)
if (u <= prob.item.1.chosen){
choice.id <- c(choice.id, item.1)
}
else{
choice.id <- c(choice.id, item.2)
}
}
}
item.1.id <- paste("item", item.1.id+100, sep=".")
item.2.id <- paste("item", item.2.id+100, sep=".")
choice.id <- paste("item", choice.id+100, sep=".")
sim.data <- data.frame(rater.id, item.1.id, item.2.id, choice.id)
## fit the model
posterior <- MCMCpaircompare2d(pwc.data=sim.data,
theta.constraints=list(item.101=list(1,2),
item.101=list(2,2),
item.102=list(1,-2),
item.102=list(2,-2),
item.103=list(1,"+"),
item.103=list(2,"-")),
verbose=1000,
burnin=500, mcmc=20000, thin=10,
store.theta=TRUE, store.gamma=TRUE, tune=0.5)
theta1.draws <- posterior[, grep("theta1", colnames(posterior))]
theta2.draws <- posterior[, grep("theta2", colnames(posterior))]
gamma.draws <- posterior[, grep("gamma", colnames(posterior))]
theta1.post.med <- apply(theta1.draws, 2, median)
theta2.post.med <- apply(theta2.draws, 2, median)
gamma.post.med <- apply(gamma.draws, 2, median)
theta1.post.025 <- apply(theta1.draws, 2, quantile, prob=0.025)
theta1.post.975 <- apply(theta1.draws, 2, quantile, prob=0.975)
theta2.post.025 <- apply(theta2.draws, 2, quantile, prob=0.025)
theta2.post.975 <- apply(theta2.draws, 2, quantile, prob=0.975)
gamma.post.025 <- apply(gamma.draws, 2, quantile, prob=0.025)
gamma.post.975 <- apply(gamma.draws, 2, quantile, prob=0.975)
## compare estimates to truth
par(mfrow=c(2,2))
plot(theta1.true, theta1.post.med, xlim=c(-2.5, 2.5), ylim=c(-2.5, 2.5),
col=rgb(0,0,0,0.3))
segments(x0=theta1.true, x1=theta1.true,
y0=theta1.post.025, y1=theta1.post.975,
col=rgb(0,0,0,0.3))
abline(0, 1, col=rgb(1,0,0,0.5))
plot(theta2.true, theta2.post.med, xlim=c(-2.5, 2.5), ylim=c(-2.5, 2.5),
col=rgb(0,0,0,0.3))
segments(x0=theta2.true, x1=theta2.true,
y0=theta2.post.025, y1=theta2.post.975,
col=rgb(0,0,0,0.3))
abline(0, 1, col=rgb(1,0,0,0.5))
plot(gamma.true, gamma.post.med, xlim=c(0, 1.6), ylim=c(0, 1.6),
col=rgb(0,0,0,0.3))
segments(x0=gamma.true, x1=gamma.true,
y0=gamma.post.025, y1=gamma.post.975,
col=rgb(0,0,0,0.3))
abline(0, 1, col=rgb(1,0,0,0.5))
## plot point estimates
plot(theta1.post.med, theta2.post.med,
xlim=c(-2.5, 2.5), ylim=c(-2.5, 2.5),
col=rgb(0,0,0,0.3))
for (i in 1:length(gamma.post.med)){
arrows(x0=0, y0=0,
x1=cos(gamma.post.med[i]),
y1=sin(gamma.post.med[i]),
col=rgb(1,0,0,0.2), len=0.05, lwd=0.5)
}
}
Run the code above in your browser using DataLab