runtime error

Help
Chris
2011-07-18
2012-09-01
  • Chris
    Chris
    2011-07-18

    I am trying to fit a relatively simple model, but keep getting an error. Any
    insight would be appreciated.

    jags.inits<-function(){

    list(a=rnorm(M), b=rnorm(1), mu.a=rnorm(1),

    sigma.y=runif(1), sigma.a=runif(1))

    }

    jags.data<-list("n", "M", "market", "x", "y")

    jags.params<-c("a", "b", "mu.a", "sigma.y", "sigma.a")

    jags.fit<-jags(jags.data, inits=jags.inits, jags.params, n.iter=5000,
    model.file="mlm.bug")

    model{

    for (i in 1:n){

    y_~dnorm(y.hat_, tau.y)

    y.hat_<-a[market_]+b*x_

    }

    b~dnorm(0,0.0001)

    tau.y<-pow(sigma.y, -2)

    sigma.y~dunif(0,100)

    for (m in 1:M){

    a~dnorm(mu.a, tau.a)

    }

    mu.a~dnorm(0,0.001)

    tau.a<-pow(sigma.a, -2)

    sigma.a~dunif(0,100)

    }

    Compiling model graph

    Resolving undeclared variables

    Allocating nodes

    Deleting model

    Error in jags.model(model.file, data = data, inits = inits, n.chains =
    n.chains, :

    RUNTIME ERROR:

    Compilation error on line 3.

    Unable to resolve node y.hat

    This may be due to an undefined ancestor node or a directed cycle in the graph


     
  • The error points to a unresolved node at y.hat.

    So go back and check y.hat.

    My guess is that y must be something as y_, and y.hat as y.hat_. And you want
    probably to loop over different values of x, so use x_.

    So the idea is that y.hat is unresolved because it is not possible to
    calculate a + b*x.


     
  • Martyn Plummer
    Martyn Plummer
    2011-07-18

    That's just the forum software mangling the BUGS code. I got a pristine copy
    of the code by email and it compiles for me with the following fake data

    x <- c(1,2,3,4,5)
    y <- c(-1.073, 0.432, -0.823,  0.554, -0.874)
    n <- 5
    M <- 2
    market <- c(1,2,1,2,1)
    

    Check this and then, if it works, check your data.

     
  • Chris
    Chris
    2011-07-18

    Thanks. This worked and you are correct, the problem was in the data, not the
    model. Many thanks.