Jeromy Anglim's Notes

Assorted notes on statistics, R, psychological research, LaTeX, computing, etc. See also my primary blog for more substantive posts: jeromyanglim.blogspot.com
  • rss
  • archive

Tags:
  • R
  • LaTeX
  • Linux / Ubuntu
  • OSX
  • Jags
  • Tumblr

Previous Notes:
Main Blog:
  • Jags error: glm does not does converge in random coefficient quadratic model

    I was fitting a random coefficient quadratic model in jags with all three regression coefficient parameters as varying. See the following extract of the JAGS/BUGS code:

    for (i in 1:length(y)) {
        mu[i] <- beta1[subject[i]] + beta2[subject[i]] * trial[i] +
            beta3[subject[i]] * pow(trial[i], 2);
        y[i]  ~ dnorm(mu[i], tau.c)
    }
    
    # Random coefficients
    for (i in 1:N) {    
        beta1[i] ~ dnorm(beta1.mu, beta1.tau);
        beta2[i] ~ dnorm(beta2.mu, beta2.tau);
        beta3[i] ~ dnorm(beta3.mu, beta3.tau);
    }
    

    I then ran the model

    mod1 <- jags.model("model.txt", data=jagsdata, n.chains=4, n.adapt=1000)
    update(mod1, 200) # burn in
    
    # monitor
    mod1.samples <- coda.samples(model=mod1,
                                 variable.names=c('beta1.mu', 'beta1.sigma', 
                                                  'beta2.mu', 'beta2.sigma',
                                                  'beta3.mu', 'beta3.sigma',
                                                  'sigma.c'),
                                 n.iter=1000)
    summary(mod1.samples) # print descriptive statistics of posterior densities for parameters
    

    and received the warning:

    Warning message:
    glm.fit: algorithm did not converge 
    

    Solution

    The warning is discussed here. Martyn Plummer suggests that the warning can indcate poor mixing, and that one strategy is “to do quite a long run with a large thinning interval”.

    When I increased the iterations and incorporated thinning, the warning went away:

    mod1.samples <- coda.samples(model=mod1,
                                 variable.names=c('beta1.mu', 'beta1.sigma', 
                                                  'beta2.mu', 'beta2.sigma',
                                                  'beta3.mu', 'beta3.sigma',
                                                  'sigma.c'),
                                 n.iter=10000, thin=10)
    
    • December 7, 2012 (10:30 am)
  • comments powered by Disqus