hello all,
I am trying to create a hierarchical second-order polynomial model on the logit scale, using cmdstanr. I thought that this would be fairly straightforward if I adapted the example code in the user guide for hierarchical logistic regression to this slightly more complex model structure. But it won’t compile because of a data type incompatibility error that I can’t figure out the cause of.
For reference, the non-hierarchical version of the model I am trying to fit looks like this in R’s stats package:
glm( y ~ poly(x, 2), data=df, family=binomial)
The following code for a hierarchical logistic regression compiles without a problem:
data {
int<lower=1> D;
int<lower=0> N;
int<lower=1> L;
array[N] int<lower=0, upper=1> y;
array[N] int<lower=1, upper=L> ll;
array[N] row_vector[D] x;
}
parameters {
array[D] real mu;
array[D] real<lower=0> sigma;
array[L] vector[D] beta;
}
model {
for (d in 1:D) {
mu[d] ~ normal(0, 100);
for (l in 1:L) {
beta[l, d] ~ normal(mu[d], sigma[d]);
}
}
for (n in 1:N) {
y[n] ~ bernoulli(inv_logit(x[n] * beta[ll[n]]));
}
}
but when I simply expand the model formula from beta * x
to A + B * x + C * x^2
, like I do below, I get an error telling me one of my parameters is in an inappropriate data type (A, B, and C are coded as pA, pB, and pC).
data{
int <lower=1> D;
int <lower=0> N;
int <lower=1> L;
array[N] int<lower=0, upper=1> y;
array[N] int<lower=1, upper=L> ll;
array[N] row_vector[D] x;
}
parameters{
array [D] real prior_pA;
array [D] real prior_pB;
array [D] real prior_pC;
array [D] real <lower=0> sigma_pA;
array [D] real <lower=0> sigma_pB;
array [D] real <lower=0> sigma_pC;
array [L] vector[D] pA;
array [L] vector[D] pB;
array [L] vector[D] pC;
}
model {
for (d in 1:D) {
prior_pA[d] ~ normal(0, 100);
prior_pB[d] ~ normal(0, 100);
prior_pC[d] ~ normal(0, 100);
for (l in 1:L) {
pA[l, d] ~ normal(prior_pA[d], sigma_pA[d]);
pB[l, d] ~ normal(prior_pB[d], sigma_pB[d]);
pC[l, d] ~ normal(prior_pC[d], sigma_pC[d]);
}
}
for (n in 1:N) {
y[n] ~ bernoulli_logit(pA[n] + pB[ll[n]] * x[n] + pC[ll[n]] * x[n]^2);
}
}
The error:
Semantic error in '/var/folders/k7/2gf32r911sgfpf0pld60jb780000gn/T/RtmpVMTXH5/model-12d67652ed1e4.stan', line 35, column 28 to column 52:
-------------------------------------------------
33: }
34: for (n in 1:N) {
35: y[n] ~ bernoulli_logit(pA[n] + pB[ll[n]] * x[n] + pC[ll[n]] * x[n]^2);
^
36: }
37: }
-------------------------------------------------
Ill-typed arguments supplied to infix operator +. Available signatures:
(int, int) => int
(real, real) => real
(real, vector) => vector
(vector, real) => vector
(vector, vector) => vector
(complex, complex) => complex
(real, row_vector) => row_vector
(row_vector, real) => row_vector
(row_vector, row_vector) => row_vector
(real, matrix) => matrix
(matrix, real) => matrix
(matrix, matrix) => matrix
(complex, complex_vector) => complex_vector
(complex_vector, complex) => complex_vector
(complex_vector, complex_vector) => complex_vector
(complex, complex_row_vector) => complex_row_vector
(complex_row_vector, complex) => complex_row_vector
(complex_row_vector, complex_row_vector) => complex_row_vector
(complex, complex_matrix) => complex_matrix
(complex_matrix, complex) => complex_matrix
(complex_matrix, complex_matrix) => complex_matrix
Instead supplied arguments of incompatible type: vector, matrix.
I don’t think I defined any variables as a matrix, and I adapted the code as directly as possible. I’d be grateful for any help figuring out what exactly is going on here.
thanks in advance