Умножение вектора параметров на матрицу независимых переменных в JAGS - PullRequest
0 голосов
/ 24 апреля 2018

Я подгоняю многомерную модель в JAGS, используя распределение dirlichet. У меня есть матрица 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))

Я соответствую модели JAGS, используя пакет runjags в R.

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.
"

1 Ответ

0 голосов
/ 25 апреля 2018

Решение этой проблемы - взять точечный продукт или внутренний продукт в 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.
"
...