Search code examples
rjags

Why this models return different sample


I am using Jags and I have defined two different models to estimate a parameter theta. Why does this two models return different sample of theta 1 and theta 2? Someone could help me?

#MODEL 1
model {
    for ( i in 1:nFlip)    {
        y[i] ~ dbern ( theta[ mdlI ] )
    }

    theta[1] <- 1/(1+exp( -nu ))
    theta[2] <- exp( -eta )
   nu ~ dnorm(0,.1)      #  0,.1  vs  1,1
   eta ~ dgamma(.1,.1)   # .1,.1  vs  1,1

    # Prob dos modelos
    mdlI ~ dcat ( mdlProb[] )
    mdlProb[1] <- .5
    mdlProb[2] <- .5

}

#MODEL 2
model {
   for ( i in 1:nFlip ) {
      # Likelihood:
      y[i] ~ dbern( theta )
   }
   # Prior
   theta <- ( (2-mdlIdx) * 1/(1+exp( -nu )) # theta from model index 1
            + (mdlIdx-1) * exp( -eta ) )    # theta from model index 2
   nu ~ dnorm(0,.1)      #  0,.1  vs  1,1
   eta ~ dgamma(.1,.1)   # .1,.1  vs  1,1
   # Hyperprior on model index:
   mdlIdx ~ dcat( modelProb[] )
   modelProb[1] <- .5
   modelProb[2] <- .5
}

Thanks in advance for any help. Diogo Ferrari


Solution

  • The samples are random, so they are supposed to be different, but their average values are comparable (and awfully biased, but that is a different issue).

    I used the following data.

    nFlip <- 100
    y <- ifelse( 
      sample(c(TRUE,FALSE), nFlip, replace=TRUE), # Choose a coin at random
      sample(0:1, nFlip, p=c(.5,.5), replace=TRUE), # Fair coin
      sample(0:1, nFlip, p=c(.1,.9), replace=TRUE) # Biased coin
    )
    # Models
    m1 <- "
    model {
        for ( i in 1:nFlip)    {
            y[i] ~ dbern ( theta[ mdlI ] )
        }
        theta[1] <- 1/(1+exp( -nu ))
        theta[2] <- exp( -eta )
        nu  ~ dnorm(0,.1)   
        eta ~ dgamma(.1,.1) 
        mdlI ~ dcat ( mdlProb[] )
        mdlProb[1] <- .5
        mdlProb[2] <- .5
    }
    "
    m2 <- "
    model {
       for ( i in 1:nFlip ) {
          y[i] ~ dbern( theta )
       }
       theta <- ( (2-mdlIdx) * 1/(1+exp( -nu ))
                + (mdlIdx-1) * exp( -eta ) )   
       theta1 <- 1/(1+exp( -nu ))
       theta2 <- exp( -eta )
       nu  ~ dnorm(0,.1)   
       eta ~ dgamma(.1,.1)
       mdlIdx ~ dcat( modelProb[] )
       modelProb[1] <- .5
       modelProb[2] <- .5
    }
    "
    

    and ran the computations as follows.

    library(rjags)
    m <- jags.model(textConnection(m1), n.chains=2)
    s <- coda.samples(m, "theta", n.iter=1e4, thin=100)
    plot(s) # One of the probabilities is around 1, the other around .7
    
    m <- jags.model(textConnection(m2), n.chains=2)
    s <- coda.samples(m, c("theta1", "theta2"), n.iter=1e4, thin=100)
    plot(s) # Similar estimates
    

    For advice on how to check that the chains have converged, and how to write your models so that they are identifiable, https://stats.stackexchange.com/ may be a better place.