0

I am trying to adjust a linear model with JAGS but I'm having trouble with the code. I'm writing:

library(R2jags)
library(BEST)

base<-data.table::data.table(read.csv("/Users/franco/Documents/Todo/UNAM/Facultad\ de\ Ciencias/Asignaturas\ Actuaría/Análisis\ Bayesiano\ de\ Datos/Tareas/Tarea-Examen\ 2/FootballLeague.csv"))
X <- cbind(1,as.matrix(base[,-c(1,2,12)]))
y <-as.matrix(base[,2])
n <- length(y)
m <- ncol(X)

model.jags <- function(){
  tau ~ dgamma(0.01, 0.01)
  for(i in 1:m){
    beta[i] ~ dnorm(0,0.001)
  }

  for (i in 1:n){

    y[i] ~ dnorm(x[i,]%*%beta,tau)

  } 

  sigma <- pow(tau,-1)
}
jags.params <- c("beta","sigma")
jags.modelo <- jags(model.file=model.jags,parameters.to.save=jags.params,
                    data = list('n' = n,
                                'y' = y,
                                'x' = X,
                                'm'=m),
                    n.chains = 2,
                    n.thin=1,
                    DIC=FALSE,
                    n.burnin = 10000,
                    n.iter = 20000)

And R throws this error:

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, : RUNTIME ERROR: Compilation error on line 8. Dimension mismatch in subset expression of y.

I don't know which is the error :/ Can someone help me, please.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
  • we almost certainly can't answer this without a [mcve] (we don't have your data file ...) – Ben Bolker Nov 30 '19 at 21:23
  • perhaps try defining `y` as `y <- base[,2]` i.e. remove its dimension?? But if you can do try to add a reproducible example please – user20650 Nov 30 '19 at 23:48
  • Yeah! Sorry for not uploading the data file. I've just solved it.JAGS needed a numeric object to work and when you extract a vector from a csv file it is not treated as one. Thanks¡¡ – Roberto Francotzin Dec 01 '19 at 22:59

0 Answers0