# NOT RUN {
####################################
# A simulated Data Set
####################################
nsubject <- 200
nitem <- 40
y <- matrix(0,nrow=nsubject,ncol=nitem)
dimnames(y) <- list(paste("id",seq(1:nsubject)),
paste("item",seq(1,nitem)))
ind <- rbinom(nsubject,1,0.5)
theta <- ind*rnorm(nsubject,1,0.25)+(1-ind)*rnorm(nsubject,3,0.25)
beta <- c(0,seq(-1,3,length=nitem-1))
true.density <- function(grid)
{
0.5*dnorm(grid,1,0.25)+0.5*dnorm(grid,3,0.25)
}
for(i in 1:nsubject)
{
for(j in 1:nitem)
{
eta<-theta[i]-beta[j]
mean<-exp(eta)/(1+exp(eta))
y[i,j]<-rbinom(1,1,mean)
}
}
# Prior information
beta0 <- rep(0,nitem-1)
Sbeta0 <- diag(1000,nitem-1)
prior <- list(alpha=1,
tau1=6.01,
tau2=2.01,
mub=0,
Sb=100,
beta0=beta0,
Sbeta0=Sbeta0,
M=5)
# Initial state
state <- NULL
# MCMC parameters
nburn <- 5000
nsave <- 5000
nskip <- 0
ndisplay <- 100
mcmc <- list(nburn=nburn,
nsave=nsave,
nskip=nskip,
ndisplay=ndisplay)
# Fit the model
fit1 <- FPTrasch(y=y,prior=prior,mcmc=mcmc,
state=state,status=TRUE,grid=seq(-1,5,0.01),
compute.band=TRUE)
# Density estimate (along with HPD band) and truth
plot(fit1$grid,fit1$dens.u,lwd=2,col="blue",type="l",lty=2,
xlab=expression(theta),ylab="density")
lines(fit1$grid,fit1$dens,lwd=2,col="blue")
lines(fit1$grid,fit1$dens.l,lwd=2,col="blue",lty=2)
lines(fit1$grid,true.density(fit1$grid),col="red")
# Summary with HPD and Credibility intervals
summary(fit1)
summary(fit1,hpd=FALSE)
# Plot model parameters
# (to see the plots gradually set ask=TRUE)
plot(fit1,ask=FALSE)
plot(fit1,ask=FALSE,nfigr=2,nfigc=2)
# Extract random effects
DPrandom(fit1)
plot(DPrandom(fit1))
DPcaterpillar(DPrandom(fit1))
# }
Run the code above in your browser using DataLab