RIcompare <- function(x,l){
require(R2jags)
### Assemble data into list for JAGS====
y = x
itemID = colnames(y)
subjID = rownames(y)
Nitem = ncol(y)
Nsubj = nrow(y)
v = ncol(y)
n = nrow(y)
L = (l - 1)
dataList = list( y=y , Nsubj=Nsubj, Nitem=Nitem , L=L)
# Define the model====
modelString = "
model {
for ( i in 1:Nsubj ) {
for ( j in 1:Nitem ) {
y[i,j] ~ dbin( pCorr[i,j] , L)
pCorr[i,j] <- ilogit( T[i,j] )
T[i,j] <- ((2*log(subjAbil[i]) + 2*log(itemDiff[j])) * (1-m)) +
((log(subjAbil[i]) + log(itemDiff[j])) * (m))
}
}
### Latent estimates
for ( subjIdx in 1:Nsubj ) {
subjAbil[subjIdx] ~ dnorm( muAbil , sigmaAbil )T(0,)
}
for ( itemIdx in 1:Nitem ) {
itemDiff[itemIdx] ~ dnorm( muDiff , sigmaDiff )T(0,)
}
### Priors for latents
muAbil ~ dgamma(5,1)
sigmaAbil ~ dgamma(.001,.001)
muDiff ~ dgamma(5,1)
sigmaDiff ~ dgamma(.001,.001)
### Priors for model selection
BF <- pM/(1-pM)
m ~ dbern(pM)
pM ~ dbeta( omega*(kappa-2)+1 ,
(1-omega)*(kappa-2)+1 ) # prior probability for m=1
omega ~ dbeta(1,1)
kappa <- kappaMinusTwo + 2
kappaMinusTwo ~ dgamma(.01,.01)
}
" # close quote for modelString
model = textConnection(modelString)
# Run the chains====
# Name the parameters to be monitored
params <- c("BF")
# Random initial values
inits <- function(){list("omega"=stats::rbeta(Nsubj,1,1),
"kappaMinusTwo"=stats::rgamma(Nitem,1,1))}
# Define some MCMC parameters for JAGS
nthin = 1 # How Much Thinning?
nchains = 3 # How Many Chains?
nburnin = 100 # How Many Burn-in Samples?
nsamples = 1100 # How Many Recorded Samples?
### Calling JAGS to sample
startTime = proc.time()
samples <- R2jags::jags(dataList, NULL, params, model.file =model,
n.chains=nchains, n.iter=nsamples, n.burnin=nburnin,
n.thin=nthin, DIC=T, jags.seed=666)
stopTime = proc.time(); elapsedTime = stopTime - startTime; methods::show(elapsedTime)
### Inspect and diagnose the run
Test <- samples$BUGSoutput$sims.list$BF
getmode <- function(v) {
Dens <- density(v,bw="SJ",from=0)
return(Dens$x[which.max(Dens$y)])
}
Mode <- getmode(Test)
library(coda)
HDI <- HPDinterval(as.mcmc(Test), prob=0.95)
Result <- list("BFs"=Test,"HDI"=HDI,"Mode"=Mode)
return(Result)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.