3

我正在使用 dirlichet 分布在 JAGS 中拟合多元模型。我有一个y包含 3 个物种比例丰度的矩阵。

#generate 3 columns of species proprotional abundance data
y    <- matrix(ncol = 3, nrow = 100)
y[,] <- abs(rnorm(length(y)))
for(i in 1:nrow(y)){
  y[i,] <- y[i,] / sum(y[i,])
}

我有一个x预测值矩阵,其中第一个是截距。

#generate 2 columns of predictors and an intercept
x <- matrix(ncol = 2, nrow = 100)
x[,] <- rnorm(length(x), mean = 20, sd = 4)
x <- cbind(rep(1,nrow(x)),x)

我指定了一个多元锯齿模型jags.model

jags.model = "
model {
    #setup parameter priors for each species * predictor combination.
    for(j in 1:N.spp){
      for(k in 1:N.preds){
        m[k,j] ~ dgamma(1.0E-3, 1.0E-3)
      }
    }

    #go ahead and fit means of species abundances as a linear combination of predictor and parameters.
    for(i in 1:N){
        for(j in 1:N.spp){
             log(a0[i,j]) <- m[,j] * x[i,]
           }
    y[i,1:N.spp] ~ ddirch(a0[i,1:N.spp]) 
    }

} #close model loop.
"

我设置了 JAGS 数据对象jags.data

jags.data <- list(y = as.matrix(y), x = as.matrix(x),
                  N.spp = ncol(y), N.preds = ncol(x), N = nrow(y))

我使用 R 中的 runjags 包拟合 JAGS 模型。

jags.out <- runjags::run.jags(jags.model,
                              data=jags.data,
                              adapt = 100,
                              burnin = 200,
                              sample = 400,
                              n.chains=3,
                              monitor=c('m'))

我收到以下错误:

Error: The following error occured when compiling and adapting the model using rjags:
 Error in rjags::jags.model(model, data = dataenv, n.chains = length(runjags.object$end.state),  : 
  RUNTIME ERROR:
Invalid vector argument to exp

我在这里做错了什么?作为参考,通过预测组合拼出每个参数仍然很合适:

jags.model = "
model {
  #setup parameter priors for each species * predictor combination.
    for(j in 1:N.spp){
      for(k in 1:N.preds){
        m[k,j] ~ dgamma(1.0E-3, 1.0E-3)
      }
    }

  #go ahead and fit means of species abundances as a linear combination of predictor and parameters.
  for(i in 1:N){
    for(j in 1:N.spp){
      log(a0[i,j]) <- m[1,j] * x[i,1] + m[2,j] * x[i,2] + m[3,j] * x[i,3]
    }
    y[i,1:N.spp] ~ ddirch(a0[i,1:N.spp]) 
  }

} #close model loop.
"
4

1 回答 1

3

解决这个问题的方法是在 JAGS 中取点积或内积。换行:

log(a0[i,j]) <- m[,j] * x[i,]

至:

log(a0[i,j]) <- inprod(m[,j] , x[i,])

一切都应该正常工作。完整模型如下。

jags.model = "
model {
    #setup parameter priors for each species * predictor combination.
    for(j in 1:N.spp){
      for(k in 1:N.preds){
        m[k,j] ~ dgamma(1.0E-3, 1.0E-3)
      }
    }

    #go ahead and fit means of species abundances as a linear combination of predictor and parameters.
    for(i in 1:N){
        for(j in 1:N.spp){
             log(a0[i,j]) <- inprod(m[,j] , x[i,])
           }
    y[i,1:N.spp] ~ ddirch(a0[i,1:N.spp]) 
    }

} #close model loop.
"
于 2018-04-24T21:32:53.860 回答