Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:43:33+00:00 2026-06-05T12:43:33+00:00

I often run JAGS models on simulated data with known parameters. I like the

  • 0

I often run JAGS models on simulated data with known parameters. I like the default plot method for mcmc objects. However, I would like to add an abline(v=TRUE_VALUE) for each parameter that is modelled. This would give me a quick check for whether the posterior is reasonable.

Of course I could do this manually, or presumably reinvent the wheel and write my own function. But I was wondering if there is an elegant way that builds on the existing plot method.

Here’s a worked example:

require(rjags)
require(coda)

# simulatee data
set.seed(4444)
N <- 100 
Mu <- 100
Sigma <- 15
y <- rnorm(n=N, mean=Mu, sd=Sigma)
jagsdata <- list(y=y)

jags.script <- "
model {
    for (i in 1:length(y)) {
        y[i]  ~ dnorm(mu, tau)
    }
    mu  ~ dnorm(0, 0.001)    
    sigma ~ dunif(0, 1000)
    tau <- 1/sigma^2
}"


mod1 <- jags.model(textConnection(jags.script), data=jagsdata, n.chains=4, 
                   n.adapt=1000)
update(mod1, 200) # burn in
mod1.samples <- coda.samples(model=mod1,
                             variable.names=c('mu', 'sigma'),
                             n.iter=1000)                  
plot(mod1.samples)

enter image description here

I just want to run something like abline(v=100) for mu and abline(v=15) for sigma. Of course in many other examples, I would have 5, 10, 20 or more parameters of interest. Thus, I’m interested in being able to supply a vector of true values for named parameters.

I’ve had a look at getAnywhere(plot.mcmc). Would modifying that be a good way to go?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:43:35+00:00Added an answer on June 5, 2026 at 12:43 pm

    Okay. So I modified plot.mcmc to look like this:

    my.plot.mcmc <- function (x, trace = TRUE, density = TRUE, smooth = FALSE, bwf, 
        auto.layout = TRUE, ask = FALSE, parameters, ...) 
    {
        oldpar <- NULL
        on.exit(par(oldpar))
        if (auto.layout) {
            mfrow <- coda:::set.mfrow(Nchains = nchain(x), Nparms = nvar(x), 
                nplots = trace + density)
            oldpar <- par(mfrow = mfrow)
        }
        for (i in 1:nvar(x)) {
            y <- mcmc(as.matrix(x)[, i, drop = FALSE], start(x), 
                end(x), thin(x))
            if (trace) 
                traceplot(y, smooth = smooth, ...)
            if (density) {
                if (missing(bwf)) {
                    densplot(y, ...); abline(v=parameters[i])
                } else densplot(y, bwf = bwf, ...)
            }
            if (i == 1) 
                oldpar <- c(oldpar, par(ask = ask))
        }
    }
    

    Then running the command

    my.plot.mcmc(mod1.samples, parameters=c(Mu, Sigma))
    

    produces this

    enter image description here

    Note that parameters must be a vector of values in the same sort order as JAGS sorts variables, which seems to be alphabetically and then numerically for vectors.

    Lessons learnt

    • Simply writing a new plot.mcmc didn’t work by default presumably because of namespaces. So I just created a new function
    • I had to change set.mfrow to coda:::set.mfrow presumably also because of namespaces.
    • I changed ask to ask=FALSE, because RStudio permits browsing through figures.

    I’d be happy to hear any suggestions about better ways of overriding or adapting existing S3 methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am experimenting with parser combinators and I often run into what seems like
I often run the various test groups like: rake test:units rake test:functionals I also
I often run into the problem that I have one stream full of data
We have a very data centric application, which is often run across networks with
I often run into an issue where I have a data frame that has
I'm familiar with Java JDBC and use it often to run simple SQLs. However,
I often run into the situation where I want to disable some code while
When you start messing around with Spring's auto-proxy stuff, you often run into this
Is it posible to make logging to the console synchronous? I often run into
Lately, I've often found myself wanting to run some command on the output of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.