Hi there,
I got some syntax errors for a user defined function in Stan, but couldn’t figure out the reason. Sorry I am not familiar with Stan’s syntax especially for defining the function. Here is what I am trying,
There is a vector parameter vector[N_evt] evt_amp
in which each element follows evt_amp ~ gamma(2,2);
I’d like to generate a matrix in function generate_evt()
to
- assign
vector[N_evt] evt_amp
to a longer vectorvector[N_obs] event_temp;
(initiated as an all 0s vector) according to a known indexint evt_idx[N_evt];
. - then toeplitz
event_temp
and an all zeros vectorappend_row(event_temp[1], rep_vector(0, N_f-1))'
functions {
matrix generate_evt(int evt_idx, vector evt_amp, int N_f, int N_obs) {
// Assign evt_amp to event_temp according to index evt_idx
matrix[N_obs, N_f] evt;
vector[N_obs] event_temp = rep_vector(0, N_obs);
event_temp[evt_idx] = evt_amp;
// Toeplitz event_temp
evt[1, ] = append_row(event_temp[1], rep_vector(0, N_f-1))';
evt[, 1] = event_temp;
for (i in 2:N_obs) {
evt[i, 2:N_f] = evt[i - 1, 1:(N_f)];
}
return evt;
}
}
}
data{
...
int<lower=1> N_f;
int<lower=1> N_obs;
int evt_idx[N_evt];
}
parameters { ...
vector[N_evt] evt_amp;
}
transformed parameters { ...
evt = generate_evt(evt_idx, evt_amp, N_f, N_obs);
}
model { ...
evt_amp ~ gamma(2,2);
}
I got error for this line event_temp[evt_idx] = evt_amp;
, saying left hand type = real, right hand type = vector. So I changed the type of evt_amp to real like this matrix generate_evt(int evt_idx, real evt_amp, int N_f, int N_obs)
, then got the following error for this line real event_empt[N_obs] = rep_vector(0, N_obs);
saying
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable definition base type mismatch, variable declared as base type real variable definition has base type vector
Could you please help to fix this? I am basically circulating on how to assign values to a vector :) Thanks.