Menu

T(L,) ignored in JAGS 4.1

Help
Marc Kéry
2016-02-09
2023-07-23
  • Marc Kéry

    Marc Kéry - 2016-02-09

    Dear all,

    I want to fit a normal distribution that is truncated at some value L, but the T(L,) bit in my JAGS code is simply ignored. Using simulated data I've been able to replicate this behaviour on three different Windows machines, using JAGS 4.1 run from R >3.2 via the jagsUI package. In contrast, two colleagues who're running JAGS 3.4 get the right answer.

    The code is here below, where I attempt to fit both the normal and the truncated normal model to the same truncated data set, but then get essentially the same estimates from both.

    Thanks a lot for your help.

    Best regards --- Marc

    n <- 1000             # Number of values drawn
    mean <- 600           # Average body mass of male peregrines
    sd <- 30              # Inter-individual variability (SD)
    y <- rnorm(n,mean, sd) # draw n values
    
    trunc.limit <- 560              # Truncation limit
    ytrunc <- y[y>trunc.limit]      # left-truncate
    
    # Fit model with and without truncation in same BUGS model
    # Package data
    str(bugs.data <- list(y1 = ytrunc, y2 = ytrunc, n = length(ytrunc), trunc.limit = trunc.limit) )
    
    # Write model in BUGS language
    cat(file = "model.txt","
    model {
    
    # Submodel using the original model that ignores the truncation
    # Priors
    mu1 ~ dnorm(0, 1.0E-06)
    tau1 <- pow(sd1, -2)
    sd1 ~ dunif(0, 1000)
    
    # Likelihood for data assumed without truncation
    for (i in 1:n){
       y1[i] ~ dnorm(mu1, tau1)
    }
    
    # Submodel for truncated data
    # Priors
    mu2 ~ dnorm(0, 1.0E-06)
    tau2 <- pow(sd2, -2)
    sd2 ~ dunif(0, 1000)
    
    # Likelihood for truncated data
    for (i in 1:n){
       y2[i] ~ dnorm(mu2, tau2)T(trunc.limit,)
    }
    }"
    )
    
    inits <- function() list(mu1 = rnorm(1))  # inits
    params <- c("mu1", "sd1", "mu2", "sd2")
    ni <- 2000   ;   nt <- 1   ;   nb <- 1000   ;  nc <- 3
    
    # Call JAGS from R, check convergence and look at estimates
    library(jagsUI)
    out <- jags(bugs.data, inits, params, "model.txt", n.chains = nc, n.thin = nt, n.iter = ni, n.burnin = nb)
    print(out3, 3)
    
     
  • Martyn Plummer

    Martyn Plummer - 2016-02-09

    Confirmed. I can't see anything wrong with the code for the truncated likelihood so I'm going to have to step through your example to see where it is going wrong.

     
  • Marc Kéry

    Marc Kéry - 2016-02-09

    Great, thank you, Martyn. This almost drove me mad yesterday !

    Best regards --- Marc

     
  • Martyn Plummer

    Martyn Plummer - 2016-02-09

    Yes there is a bug. It's one of those tiny ones that is virtually impossible to see unless you pay attention with a "<" instead of a ">". The truncated likelihood evaluation is now working.

                  mean    sd      2.5%       50%     97.5% overlap0 f  Rhat n.eff
    mu1        613.759 0.807   612.222   613.761   615.313    FALSE 1 1.000  3000
    sd1         22.306 0.562    21.219    22.292    23.450    FALSE 1 1.001  3000
    mu2        601.129 2.515   595.700   601.269   605.573    FALSE 1 1.002   838
    sd2         30.392 1.543    27.583    30.311    33.582    FALSE 1 1.002  1381
    deviance 13436.515 2.692 13433.145 13435.904 13443.209    FALSE 1 1.003   939
    

    A bug that just gives you the wrong answer without any warning is the worst kind of bug. I'm going to publish a new version of JAGS with the bug fix.

     
  • Marc Kéry

    Marc Kéry - 2016-02-10

    Great, thanks.

    Marc

     
  • Martyn Plummer

    Martyn Plummer - 2016-02-12

    I just need to add some unit tests to the test suite to make sure this does not come back.

     
  • German Vargas

    German Vargas - 2023-04-23

    Hi all,

    I recently stumbled upon this exact situation in JAGS 4.3.1 for Apple silicon (M1). I will test in a linux machine if T(,) is ignored as well. But I was wondering if the bug was fixed or maybe is an Apple M1 specific issue.

    Many thanks,

     
    • Martyn Plummer

      Martyn Plummer - 2023-04-23

      I would be surprised if this exact problem came back. Marc's example still works correctly for me. If you can share a reproducible example of your problem then that will help.

       
    • Matt Denwood

      Matt Denwood - 2023-04-24

      I can confirm that T(,) behaves (in general) as expected on Apple silicon (and the official binary passed the test suite before being released). But if you can share a reproducible example I can run it on the same hardware to double check your use case.

       
    • German Vargas

      German Vargas - 2023-04-26

      Hi all,

      Thank you for looking into this. I discovered I specified the truncation in the wrong way. After re-reading the manual, and fix it according to what is specified in page 35. My apologies for the lack of clarity in the post and also lack of a reproducible example in my previous post.

       
  • lisa302

    lisa302 - 2023-07-23

    .Much obliged to you for investigating this. I found I determined the truncation in the incorrect manner. After re-perusing the manual, and fix it as per what is determined in page. My conciliatory sentiments for the absence of lucidity in the post and furthermore absence of a reproducible model in my past post.

     

    Last edit: lisa302 2023-07-23

Log in to post a comment.