bridgeBIRTno=function(object, niter,tol,silent){

fit=object$mcmc
z=object$z
model=object$model
z=as.matrix(z)
item.out=object$output
inflated=object$inflated

tau.samples=rstan::extract(fit,"theta")$theta
ti.samples=rstan::extract(fit,"beta")$beta
ln_phi.samples=log(rstan::extract(fit,"alpha")$alpha)
ln_sigma2.samples=log(rstan::extract(fit,"phi")$phi)

tau.est=apply(tau.samples,2,mean)
ti.est=apply(ti.samples,2,mean)
ln_phi.est=apply(ln_phi.samples,2,mean)
ln_sigma2.est=apply(ln_sigma2.samples,2,mean)

N=length(tau.est)
nit=length(ti.est)
no_iter=nrow(tau.samples)

###
n1=n2=floor(no_iter/2)

full.samples=cbind(tau.samples,ti.samples,ln_phi.samples,ln_sigma2.samples)
tau.ind=1:N
ti.ind=1:nit+N
ln_phi.ind=1:nit+N+nit
ln_sigma2.ind=1:nit+N+nit*2

post.batch1=full.samples[1:n1,]
post.batch2=full.samples[(n1+1):(2*n1),]

propos.me.p=apply(post.batch1[,tau.ind],2,mean)
propos.me.i=apply(post.batch1[,(N+1):(N+nit*3)],2,mean)

propos.cov.p=diag(diag(stats::cov(post.batch1[,tau.ind])))
propos.cov.i=stats::cov(post.batch1[,(N+1):(N+nit*3)])

propos.samples.p=MASS::mvrnorm(n2,propos.me.p,propos.cov.p)
propos.samples.i=MASS::mvrnorm(n2,propos.me.i,propos.cov.i)

propos.samples=cbind(propos.samples.p,propos.samples.i)

propos.me=c(propos.me.p,propos.me.i)
propos.cov=rbind(cbind(propos.cov.p,propos.cov.p[,1:(3*nit)]*0),cbind(propos.cov.p[1:(3*nit),]*0,propos.cov.i))

debug1=matrix(,n1,5)

dsimplex=function (y, m, s2){  #from rmutil by Swihart & Lindsey (2022) but with minor adjustment
  tmp <- -((y - m)/(m * (1 - m)))^2/(2 * y * (1 - y) * s2) -
    (log(2 * pi * s2) + 3 * (log(y) + log(1 - y)))/2
  return(exp(tmp))
}


log.l2=c()
if(!silent) cat("Stage 1/2:\n")
for(i in 1:n2){
  if(!silent) cat("processing proposal sample",i,"\r")
  tau.tmp2=propos.samples[i,tau.ind]
  ti.tmp2=propos.samples[i,ti.ind]
  ln_phi.tmp2=propos.samples[i,ln_phi.ind]
  ln_sigma2.tmp2=propos.samples[i,ln_sigma2.ind]

  ou5=ou1=ou2=ou3=ou4=c()
  for(p in 1:N){
    if(model=="beta") ou1[p]=sum(stats::dbeta(z[p,],exp(.5*(exp(ln_phi.tmp2)*tau.tmp2[p]+ti.tmp2+ln_sigma2.tmp2)),exp(.5*(-exp(ln_phi.tmp2)*tau.tmp2[p]-ti.tmp2+ln_sigma2.tmp2)),log=T))
    if(model=="simplex"){
      tmp=log(dsimplex(z[p,],m=1/(1+exp(-(ti.tmp2+exp(ln_phi.tmp2)*tau.tmp2[p]))),s=exp(ln_sigma2.tmp2)))
      ou1[p]=sum(tmp[is.finite(tmp)])
    }
    if(model=="samejima") ou1[p]=sum(-log(z[p,])-log(1-z[p,])+stats::dnorm(stats::qlogis(z[p,]),ti.tmp2+exp(ln_phi.tmp2)*tau.tmp2[p],exp(.5*ln_sigma2.tmp2),log=T))
    if(model=="normal") ou1[p]=sum(stats::dnorm(z[p,],ti.tmp2+exp(ln_phi.tmp2)*tau.tmp2[p],exp(.5*ln_sigma2.tmp2),log=T))

    ou4[p]=stats::dnorm(tau.tmp2[p],log=T)
  }
  ou2=sum(log(stats::dnorm(ln_phi.tmp2,0,10))+log(stats::dnorm(ln_sigma2.tmp2,0,10))+log(stats::dnorm(ti.tmp2,0,10)))
  ou3=mvtnorm::dmvnorm(c(ti.tmp2,ln_phi.tmp2,ln_sigma2.tmp2),propos.me[-(1:N)][1:(3*nit)],propos.cov[-(1:N),-(1:N)][1:(3*nit),1:(3*nit)],log=T)
  ou5=mvtnorm::dmvnorm(tau.tmp2,propos.me[1:N],propos.cov[(1:N),(1:N)],log=T)

  log.l2[i]=sum(ou1)+sum(ou4)+ou2 - (ou5+ou3)
  debug1[i,1:5]=c(sum(ou1),sum((ou4)),ou2, (ou5), (ou3))
}

log.l1=mll1=c()
llb=matrix(,N,n1)
if(!silent) cat("\nStage 2/2:\n")
for(i in 1:n1){
  if(!silent) cat("processing posterior sample",i,"\r")
  tau.tmp1=post.batch2[i,tau.ind]
  ti.tmp1=post.batch2[i,ti.ind]
  ln_phi.tmp1=post.batch2[i,ln_phi.ind]
  ln_sigma2.tmp1=post.batch2[i,ln_sigma2.ind]

  ou5=ou1=ou2=ou3=ou4=c()
  for(p in 1:N){
    if(model=="beta") ou1[p]=sum(stats::dbeta(z[p,],exp(.5*(exp(ln_phi.tmp1)*tau.tmp1[p]+ti.tmp1+ln_sigma2.tmp1)),exp(.5*(-exp(ln_phi.tmp1)*tau.tmp1[p]-ti.tmp1+ln_sigma2.tmp1)),log=T))
    if(model=="simplex"){
      tmp=log(dsimplex(z[p,],m=1/(1+exp(-(ti.tmp1+exp(ln_phi.tmp1)*tau.tmp1[p]))),s=exp(ln_sigma2.tmp1)))
      ou1[p]=ou1[p]=sum(tmp[is.finite(tmp)])
    }
    if(model=="samejima") ou1[p]=sum(-log(z[p,])-log(1-z[p,])+stats::dnorm(stats::qlogis(z[p,]),ti.tmp1+exp(ln_phi.tmp1)*tau.tmp1[p],exp(.5*ln_sigma2.tmp1),log=T))
    if(model=="normal") ou1[p]=sum(stats::dnorm(z[p,],ti.tmp1+exp(ln_phi.tmp1)*tau.tmp1[p],exp(.5*ln_sigma2.tmp1),log=T))

    ou4[p]=stats::dnorm(tau.tmp1[p],log=T)
    llb[p,i]= ou1[p]
  }
  ou2=sum(log(stats::dnorm(ln_phi.tmp1,0,10))+log(stats::dnorm(ln_sigma2.tmp1,0,10))+log(stats::dnorm(ti.tmp1,0,10)))
  ou3=mvtnorm::dmvnorm(c(ti.tmp1,ln_phi.tmp1,ln_sigma2.tmp1),propos.me[-(1:N)][1:(3*nit)],propos.cov[-(1:N),-(1:N)][1:(3*nit),1:(3*nit)],log=T)
  ou5=mvtnorm::dmvnorm(tau.tmp1,propos.me[1:N],propos.cov[(1:N),(1:N)],log=T)
  log.l1[i]=sum(ou1) + sum(ou4)+ou2 - (ou5+ou3)
  mll1[i]= sum(ou1) + sum(ou4)
}

r0=.2
r1=c()
r1[1]=0.1
l_star1=stats::median(log.l1)
it=1
while(abs(log(rev(r1)[1])-log(r0))>tol & it<niter){
  r1[it+1]= mean(exp(log.l2-l_star1) / (.5 * exp(log.l2-l_star1) + .5 * r1[it])) /  mean(1 / (.5 * exp(log.l1-l_star1) + .5 * r1[it]))
  r0=r1[it]
  it=it+1
}
if(it==niter) stop("Iterative scheme did not converge, increase niter or tol")

# This code can be used to calculate the conditional DIC and WAIC. However, it is generally not recommended to use these for latent variable
# models like those in boundIRT. See ?loglikBIRT for alternatives (marginal WAIC and LOOIC)
#
# ###########
# #DEVIANCE #
# ###########
# if(!silent) cat("\nStage 3/3 (DIC and WAIC):\n")
#
# lla=matrix(,N,n1)
# for(i in 1:n1){
#   if(!silent) cat("processing sample: ",i,"\r")
#   tau.tmp1=post.batch1[i,tau.ind]
#   ti.tmp1=post.batch1[i,ti.ind]
#   ln_phi.tmp1=post.batch1[i,ln_phi.ind]
#   ln_sigma2.tmp1=post.batch1[i,ln_sigma2.ind]
#
#   ou5=ou1=ou2=ou3=ou4=c()
#   for(p in 1:N){
#     if(model=="beta") ou1[p]=sum(stats::dbeta(z[p,],exp(.5*(exp(ln_phi.tmp1)*tau.tmp1[p]+ti.tmp1+ln_sigma2.tmp1)),exp(.5*(-exp(ln_phi.tmp1)*tau.tmp1[p]-ti.tmp1+ln_sigma2.tmp1)),log=T))
#     if(model=="simplex"){
#       tmp=log(dsimplex(z[p,],m=1/(1+exp(-(ti.tmp1+exp(ln_phi.tmp1)*tau.tmp1[p]))),s=exp(ln_sigma2.tmp1)))
#       ou1[p]=sum(log(dsimplex(z[p,],m=1/(1+exp(-(ti.tmp1+exp(ln_phi.tmp1)*tau.tmp1[p]))),s=exp(ln_sigma2.tmp1))))
#     }
#     if(model=="samejima") ou1[p]=sum(-log(z[p,])-log(1-z[p,])+stats::dnorm(stats::qlogis(z[p,]),ti.tmp1+exp(ln_phi.tmp1)*tau.tmp1[p],exp(.5*ln_sigma2.tmp1),log=T))
#     if(model=="normal") ou1[p]=sum(stats::dnorm(z[p,],ti.tmp1+exp(ln_phi.tmp1)*tau.tmp1[p],exp(.5*ln_sigma2.tmp1),log=T))
#
#     lla[p,i]= ou1[p]
#   }
# }
# dev.mean=c()
# for(p in 1:N){
#   if(model=="beta") dev.mean[p]=sum(stats::dbeta(z[p,],exp(.5*(exp(ln_phi.est)*tau.est[p]+ti.est+ln_sigma2.est)),exp(.5*(-exp(ln_phi.est)*tau.est[p]-ti.est+ln_sigma2.est)),log=T))
#   if(model=="simplex"){
#     tmp=log(dsimplex(z[p,],m=1/(1+exp(-(ti.est+exp(ln_phi.est)*tau.est[p]))),s=exp(ln_sigma2.est)))
#     dev.mean[p]=sum(tmp[is.finite(tmp)])
#   }
#   if(model=="samejima") dev.mean[p]=sum(-log(z[p,])-log(1-z[p,])+stats::dnorm(stats::qlogis(z[p,]),ti.est+exp(ln_phi.est)*tau.est[p],exp(.5*ln_sigma2.est),log=T))
#   if(model=="normal") dev.mean[p]=sum(stats::dnorm(z[p,],ti.est+exp(ln_phi.est)*tau.est[p],exp(.5*ln_sigma2.est),log=T))
# }
#
 if(!silent) cat("\n")
# ll=cbind(lla,llb)
# DIC=2*mean(-2*apply(ll,2,sum))-sum(-2*dev.mean)
# WAIC = -2*sum(log(apply(exp(ll),1,mean))) + 2 * sum(apply(ll,1,stats::var))
return(list(r1=rev(r1)[1],l_star=l_star1))
}

