jokesOnMe
jokesOnMe

Reputation: 31

Intro to JAGS analysis

I am a student studying bayesian statistics and have just begun to use JAGS using a intro script written by my lecturer, with us (the students) having to only enter the data and the number of iterations. The following is the script with my data added into it:

setwd("C:\\Users\\JohnSmith\\Downloads")

rawdata = read.table("bwt.txt",header=TRUE)
Birthweight = rawdata$Birthweight
Age = rawdata$Age

model = "model
{
  beta0 ~ dnorm(0, 1/1000^2)
  beta1 ~  dnorm(0, 1/1000^2)

  log_sigma ~ dunif(-10, 10)
  sigma <- exp(log_sigma)

 for(i in 1:N)
 {
   mu[i] <- beta0 + beta1 * Age[i]
   Birthweight[i] ~ dnorm(mu[i], 1/sigma^2) 
 } 
}
"

data = list(x=Birthweight, y=Age, N=24)

# Variables to monitor
variable_names = c('beta0','beta1')

# How many burn-in steps?
burn_in = 1000

# How many proper steps?
steps = 100000

# Thinning?
thin = 10

# Random number seed
seed = 2693795


    # NO NEED TO EDIT PAST HERE!!!
    # Just run it all and use the results list.

library('rjags')

# Write model out to file
fileConn=file("model.temp")
writeLines(model, fileConn)
close(fileConn)

if(all(is.na(data)))
{
    m = jags.model(file="model.temp", inits=list(.RNG.seed=seed,             .RNG.name="base::Mersenne-Twister"))
} else
{
m = jags.model(file="model.temp", data=data, inits=list(.RNG.seed=seed,     .RNG.name="base::Mersenne-Twister"))
}
update(m, burn_in)
draw = jags.samples(m, steps, thin=thin, variable.names = variable_names)
# Convert to a list
make_list <- function(draw)
{
    results = list()
    for(name in names(draw))
    {
        # Extract "chain 1"
        results[[name]] = as.array(draw[[name]][,,1])

        # Transpose 2D arrays
        if(length(dim(results[[name]])) == 2)
            results[[name]] = t(results[[name]])
    }
    return(results)
}
results = make_list(draw)

However, when I run the following code I get the following error message:

Error in jags.model(file = "model.temp", data = data, inits = list(.RNG.seed = seed,  : 
  RUNTIME ERROR:
Compilation error on line 11.
Unknown parameter Age

In addition: Warning messages:
1: In jags.model(file = "model.temp", data = data, inits = list(.RNG.seed =     seed,  :
  Unused variable "x" in data
2: In jags.model(file = "model.temp", data = data, inits = list(.RNG.seed = seed,  :
  Unused variable "y" in data

But as far as I can see, line 11 is blank, which leaves me stumped as to where the error is coming from. If anyone can give me some tips as to solve this, it will be greatly appreciated.

Upvotes: 2

Views: 239

Answers (1)

jbaums
jbaums

Reputation: 27388

The names of the elements of your list of data (data) should match the names of the variables in your model.

You have:

data = list(x=Birthweight, y=Age, N=24)

so JAGS is looking for variables called x and y in your model. However, in your model, you have:

mu[i] <- beta0 + beta1 * Age[i]
Birthweight[i] ~ dnorm(mu[i], 1/sigma^2) 

That is, your variables are called Age and Birthweight.

So, either change your list to:

data <- list(Birthweight=Birthweight, Age=Age, N=24)

or change your model to:

mu[i] <- beta0 + beta1 * y[i]
x[i] ~ dnorm(mu[i], 1/sigma^2)

Had you done readLines('model.temp') (or opened model.temp in a text editor), you would have seen that line 11 of that file refers to the line that contains mu[i] <- beta0 + beta1 * Age[i], which is the first error that JAGS encountered due to the reference to Age, for which neither data nor a prior was provided.

Upvotes: 2

Related Questions