简体   繁体   中英

Specifying parameter matrix cells in R Stan

I have a 6 by 3 (6 x 3) parameter matrix D (suppose). The specific cells of this matrix are zero. More specifically,

 D[2,2]=0,
 D[3,2]=0,
 D[3,3]=0,
 D[5,2]=0

Now how can I declare the matrix as well as specify these cells in the "parameter" block of the Stan code?

Declaring the matrix is easy as follows:

parameters{

   matrix[6,3] D;

}

But how can I specify those particular cells of the matrix as zeroes?

Here is a minimal example.

library(rstan)

model_code <- "
data {
    int<lower=0> N;
    vector[N] x;
    vector[N] y;
}

parameters {
    real alpha;
    real beta;
    real<lower=0> sigma;
}
model {
    matrix[2, 2] A;
    A[1, 2] = 1;      // Assign values to matrix
    A[2, 2] = 2;
    A[2, 1] = 3;
    A[2, 2] = 4;
    y ~ normal(alpha + beta * x, sigma);
}
"

model <- stan(
    model_code = model_code, 
    data = list(N = nrow(mtcars), x = mtcars$wt, y = mtcars$mpg))

You can also initialise values on declaration, ie in Stan

matrix[2, 2] A = [[1, 2], [3, 4]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM