Md. Zubab Ibne Moid
Md. Zubab Ibne Moid

Reputation: 1

Dimension mismatch taking subset in R/JAGS

I am trying to calculate transition probabilities from different states in R/JAGS. But I am getting the following error- Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, : RUNTIME ERROR: Compilation error on line 7. Dimension mismatch taking subset of alpha

setwd("C:/Users/nibir/OneDrive/MA Applied Economics/Phd courses/Dissertation/code")

library(rjags)
library(runjags)
library(coda)

tt <- matrix(c(1251, 350, 0, 17,
               0, 731, 0, 15,
               0, 0, 0, 0),
             ncol = 4, nrow = 3, byrow = TRUE)

modelstring="model{
  
  p[1, 1:S] ~ ddirch(alpha[1:S])
  p[2, 1:S] ~ ddirch(alpha[1:S])
  p[3, 1] <- 0
  p[3, 2] <- 0
  p[3, 3:S] ~ ddirch(alpha[3:S])
  
  
  tt[1, 1:S] ~ dmulti(p[1, 1:S], n[1])
  tt[2, 1:S] ~ dmulti(p[2, 1:S], n[2])
  tt[3, 3:S] ~ dmulti(p[3, 3:S], n[3])
}"

writeLines(modelstring,con = "TEMPmodel.txt")


S <- 4
n <- apply(tt, 1, sum)
alpha <- matrix(c(.88,.08,.02,.02,
                  .1,.7,.1,.1,
                  0,0,.7,.3),ncol = 4, nrow = 4, byrow = TRUE)
params <- c("p")
jags.data <- list("S", "n", "alpha", "tt") 

library("R2jags")
set.seed(100)
jagsfit <- jags(data = jags.data, parameters.to.save = params,
                model.file = "TEMPmodel.txt", n.chains = 3,
                n.iter = 10000, n.thin = 5, progress.bar = "none")

print(jagsfit)

How can I solve that?

Upvotes: 0

Views: 46

Answers (1)

Erfan Ghasemi
Erfan Ghasemi

Reputation: 417

You defined "alpha" as a matrix. If you want to select a subset of that you should use alpha[,]. So if you change ddirch(alpha[1:S]) to for example ddirch(alpha[1, 1:S]), this error may be solve.

Moreover, in defining alpha, you determined ncol = 4 & nrow = 4 (4 *4 = 16), meanwhile you have 12 elements.

Upvotes: 0

Related Questions