Mixed effects model

Hello!
I get the following error:
Error : int variable contained non-int values; processing stage=data initialization; variable name=Y; base type=int
failed to create the sampler; sampling not done

Could anyone explain me its meaning please?
Thank you!

Below my R code and the stan file:

library(MASS)
library(LearnBayes)
library(rstan)
set.seed(123)

N = number of users

N <- 15

M = number of books

M <- 10

Q = dimensions of the user covariates

Q <- 3

K = dimensions of the feature vector

K <- 2

J = number of rating levels

J <- 5

generate coefficients beta

mu0 <- rep(0, Q)
sigma0 <- matrix(1, nrow = Q, ncol = Q)
b <- mvrnorm(n = K, mu0, sigma0)
dim(b)

generate coefficients alpha

mu0 <- rep(0, M)
sigma0 <- matrix(1, nrow = M, ncol = M)
a <- mvrnorm(n = K, mu0, sigma0)
dim(a)

generate user covariates

x <- rbinom(Q, 1, 0.5)
for (n in seq(N-1)){
x <- rbind(x, rbinom(Q, 1, 0.5))
}
dim(x)

generate ratings

r <- sample(0:J, M, replace = TRUE)
for (n in seq(N-1)){
r <- rbind(r, sample(0:J, M, replace = TRUE))
}
dim®

generate incidence matrix

F <- diag(rbinom(M, 1, 0.5))
dim(F)

generate the response

psi <- b %% t(x) # linear combination
dim(psi)
zeta <- F %
% t®
dim(zeta)
phi <- a%*%zeta # linear combination
dim(phi)

y = psi + phi
dim(y)

########### FULL CHAIN ############

Data passed as a list

data_stan <- list(N = N,
M = M,
Q = Q,
K = K,
Y = y,
X = t(as.matrix(x)),
Z = as.matrix(zeta))

A list of initial value for the MCMC algorithm

inits <- function()
{
list( beta = matrix(0.1, nrow = K, ncol = Q),
alpha = matrix(0.1, nrow = K, ncol = M))
}

Fit the model

model = stan(file = “linearmodelmixedeffects.stan”,
data = data_stan,
chains = 1,
iter = 200,
warmup = 100,
thin = 4,
seed = 199,
init = inits,
control = list(max_treedepth = 20, adapt_delta = 0.95))

///////////////////////// DATA /////////////////////////////////////
data {
int<lower = 0> N; // number of users
int<lower = 0> M; // numbers of books
int<lower = 0> K; // number of features
int<lower = 0> Q; // number of user covariates
int<lower = 0> J; // number of rating levels
int<lower = 0, upper = 1> Y[K,N];
//matrix[K,N] Y; // response matrix
matrix[Q,N] X; // design matrix of user covariates
matrix[M,N] Z; // design matrix of books

}

//////////////////// PARAMETERS /////////////////////////////////
parameters {
matrix[K, Q] beta; // coefficients related to users
matrix[K, M] alpha; // coefficients related to books

}

transformed parameters {
// Probability trasformation from linear predictor
real<lower=0> odds[K,N];
real<lower=0, upper=1> prob[K,N];

for (i in 1:N)
{
for (k in 1:K)
{
odds[k,i] <- exp(1.0 + beta[k,]*X[,i] + alpha[k,]*Z[,i]);
//odds[k,i] <- exp(1.0 + beta[k]*X[,i] + alpha[k]*Z[,i]);
prob[k,i] <- odds[k,i] / (odds[k,i] + 1.0);
}
}

}

////////////////// MODEL ////////////////////////
model {

// Likelihood
for (i in 1:N)
{
  for (k in 1:K)
  {
    Y[k,i] ~ bernoulli(prob[k,i]);
  }
} 


// Prior
// beta
for (k in 1:K) 
{
  beta[k,] ~ normal( 0, 5);
  alpha[k,] ~ normal( 0, 5);
}

}

You told Stan to expect integers for Y and passed numbers with decimal points. My guess is that you should change the declaration back to matrix[K,N] Y;.

Thank you for the reply!

I think I am actually passing integers to Y, since it is a bernoulli, so it should be 0 or 1!

That is not what your R code is doing:

You are right!

Thank you very much!!