I have 4 demographic features: state, age , gender and urban. i want to perform a multi-level regression and poststratification. The codes is as follows,
mrp <- '
data {
int<lower=0> N;
array[N] int<lower=11, upper=65> state;
array[N] int<lower=1, upper=6> age;
array[N] int<lower=1, upper=2> gender;
array[N] int<lower=1, upper=2> urban;
array[N] int<lower=0> y;
array[31,6,2,2] int<lower=0> P;
}
parameters {
real alpha;
real<lower=0> sigma_beta;
vector<multiplier=sigma_beta>[31] beta;
real<lower=0> sigma_gamma;
vector<multiplier=sigma_gamma>[6] gamma;
real delta;
real epsilon;
}
model {
y ~ bernoulli_logit(alpha + beta[state] + gamma[age] + [delta,-delta][gender]' + [epsilon, -epsilon][urban]');
alpha ~ normal(0, 2);
beta ~ normal(0, sigma_beta);
gamma ~ normal(0, sigma_gamma);
{sigma_beta,sigma_gamma} ~ normal(0, 2);
delta ~ normal(0, 2);
epsilon ~ normal(0, 2);
}
generated quantities {
real expect_pos = 0;
int total = 0;
for (b in 1:31) {
for (c in 1:6) {
for (d in 1:2) {
for (e in 1:2) {
total += P[b, c, d, e];
expect_pos += P[b, c,d,e] * inv_logit(alpha + beta[b] + gamma[c] + delta[d] + epsilon[e] );
}
}
real<lower=0, upper=1> phi = expect_pos / total;
}
'
There is a problems. when adding ’ at the end of two binary groups,gender and urban, Rstudio give the message that there is unexpected [,] in the following line.
however,i think the discussion in the stan user is right. there should be only one coefficient for two groups.
how to settle the issue?
any help is appreciated.
If there are two groups, you can give both groups a coefficient, but you either want to pin one value to zero or you want to enforce that they sum to zero—either way you get the desired one degree of freedom out.
the result shows that the first line in the model block is wrong.
the message is that Ill-typed arguments supplied to infix operator *.
what’s wrong with my code?
thanks in advane.