Solved – Code Problem in Lognormal Bayesian Model in JAGS/WinBUGS

bayesianjagslognormal distributionrwinbugs

I have a data set with 853 units. Below, a small sample of it:

y

 [1]  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000
[10]  0.000000  6.751283  0.000000  0.000000 11.927481  0.000000  0.000000  0.000000  0.000000
[19]  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000
[28]  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000
[37]  0.000000  0.000000  0.000000  0.000000  5.342471  2.286760  0.000000  0.000000  3.464403
[46]  3.952309 16.030779 17.942967  0.000000  0.000000

head(x)

    gdp.cap     bf.cap
1  8.789771 0.03911118
2 10.204732 0.02341257
3  6.890112 0.04160150
4  9.178957 0.03892352
5  9.384171 0.03880186
6  9.562188 0.04165802

I'm calling JAGS from R to run the following code:

1) In JAGS

model {

    # Lognormal likelihood
    for(i in 1:N) { 
        y[i] ~ dlnorm(mu[i], tau)
        mu[i] <- beta0 + inprod(x[i,], beta[])
    }

    # Prior for betas
    beta0 ~ dnorm(mbeta0, precbeta0)

    for (j in 1:K) { # b are the number betas to be estimated
        beta[j] ~ dnorm(m[j], prec[j])
    }

    # Prior for precision of Y
    tau ~ dgamma(tau.a, tau.b)
    sigma <- 1/sqrt(tau)

}

2) In R:

dt <- read.csv("data.csv", header=T, sep=',')
dt <- na.omit(dt)

# Organize Data
y <- dt[,30]
x <- as.matrix(dt[,c(31,46)])

# Data:  
data <- list(N=length(y), K=2, y=y, x=x, m=c(-2,2), prec=c(.20, .20), tau.a=1, tau.b=2, mbeta0=0, precbeta0=.01)
inits <- rep(list(list(beta0=0, beta=c(1,1), tau=1)),5)
param <- c("beta0", "beta", "sigma")

# Sample Posterior
library(rjags)
sim <- jags.model(file="Bayesian/lognormal.bug", data=data, inits=inits, n.chains=5, n.adapt=1000)

I don't know why I get the following error:

Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph Size: 39212

Initializing model
Deleting model

Error in jags.model(file = "Bayesian/lognormal.bug", data = data, inits = inits,  : 
Error in node y[1]
Observed node inconsistent with unobserved parents at initialization

Any help, please?

Best Answer

You can't have zero values in a lognormal random variable--the support of the lognormal distribution is strictly positive.

If you explain in more detail what you're trying to do we may be able to help you write down a model that makes more sense.

Related Question