Search code examples
rjagswinbugs

The pow() operator in JAGS/BUGS


This is probably unimportant, but something I've been curious about for a while.

When constructing a model in JAGS/BUGS, I was initially taught to handle power transformations using the pow() function (e.g. tau <- pow(sigma, -2) to convert from a standard deviation to precision parameter in a normal distribution) but very often, I'll end up using simple arithmetic operators instead.

My question is this: is there a programmatic or syntactic benefit to pow(), or is it just a matter of aesthetics?

By way of an initial exploration, here's a good, long run of a toy simple linear regression, specified both ways. I'm using JAGS, called in R using the R2jags package.

# first some fake data
N <- 1000
x <- 1:N
y <- x + rnorm(N)

# model 1
cat('model {
  for (i in 1:N) {
    y[i] ~ dnorm(y.hat[i], tau)
    y.hat[i] <- a + b * x[i]
  }
  a ~ dnorm(0, .0001)
  b ~ dnorm(0, .0001)
  tau <- pow(sigma, -2)   ### this is the only difference
  sigma ~ dunif(0, 100)
}', file="test1.jags")

# model 2
cat('model {
  for (i in 1:N) {
    y[i] ~ dnorm(y.hat[i], tau)
    y.hat[i] <- a + b * x[i]
  }
  a ~ dnorm(0, .0001)
  b ~ dnorm(0, .0001)
  tau <- 1/(sigma*sigma)   ### this is the only difference
  sigma ~ dunif(0, 100)
}', file="test2.jags")

Both yield essentially equivalent posteriors (not shown, you'll just have to trust me ;) ), and run in essentially equivalent amounts of time.

test.data <- list(x=x,y=y,N=N)

# give both a nice long run

system.time(test1.jags.out <- jags(model.file="test1.jags", data=test.data, 
                  parameters.to.save=c("a","b","tau","sigma"), n.chains=3, n.iter=100000))
   user  system elapsed 
 166.85    0.03  166.97 

system.time(test2.jags.out <- jags(model.file="test2.jags", data=test.data, 
                  parameters.to.save=c("a","b","tau","sigma"), n.chains=3, n.iter=100000))
   user  system elapsed 
 162.42    0.00  162.75 

Is there any difference I'm not seeing?


Solution

  • They are identical at this point since at least Jags version 4.0.0. See here

    The text that is important on that particular post to the jags forum is:

    "The Pow function has alias "pow" so it can be invoked either as "a^b" or as "pow(a,b)"

    Go ahead and use whichever one you feel more comfortable with. I believe that the use of the original pow function comes from winbugs.