A "parser expected:" warning on a code that works

Dear all,

I hope this message finds you well. I have been using Stan for sometime now and its quite fun. I have implemented an ICAR model inspired by Prof. Mitzi Morris’ tutorial [HERE]

functions {
	real icar_normal_lpdf(vector phi, int N, array[] int node1, array[] int node2) {
		return -0.5 * dot_self(phi[node1] - phi[node2]);
data {
	int<lower=0> N;
	int<lower=0> N_edges;
	array[N_edges] int<lower=1, upper=N> node1;
	array[N_edges] int<lower=1, upper=N> node2;
	array[N] int<lower=0> Y;
	vector<lower=0>[N] E;
transformed data {
	vector[N] log_exposure = log(E);
parameters {
	real alpha;
	real<lower=0> sigma;
	vector[N] phi;
model {
	phi ~ icar_normal(N, node1, node2);
	Y ~ poisson_log(log_exposure + alpha + phi*sigma);
	alpha ~ normal(0.0, 1.0);
	sigma ~ normal(0.0, 1.0);
	sum(phi) ~ normal(0, 0.001*N);
generated quantities {
	vector[N] eta = alpha + phi*sigma;
	vector[N] mu = exp(eta);

The above code works perfectly on one of my machines (i.e., Mac-mini). However, I am using the same code on a different computer (i.e., MacBook pro) and I get this confusing “parser expected” warning in the function line.

 error in 'model1443186a5e0b_icar_poisson_model' at line 2, column 43
     1: functions {
     2: 	real icar_normal_lpdf(vector phi, int N, array[] int node1, array[] int node2) {
     3: 		return -0.5 * dot_self(phi[node1] - phi[node2]);

PARSER EXPECTED: <argument declaration or close paren ) to end argument declarations>
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'icar_poisson_model' due to the above error.

I am having difficulties overcoming this issue on my MacBook Pro. Also, I don’t understand why don’t get this ‘parser’ problem on one of my computer, but it appearing in another machine. I am developing a practical session of an MSc module and I can imagine different students writing the same code but this parser warning appearing on different machines.

Any help or solutions will be appreciated. Thank you.


This is just a guess, but maybe replace the phi ~ icar_normal line with

target += icar_normal_lpdf(phi | N, node1, node2);

I seem to recall that functions ending in _lpdf have some special usage that is different than other functions. So besides above, you might also try renaming the function and keeping the phi ~ thing.

Also, N is not used in the function, so it seems you could remove that argument.

1 Like

@edm, thank you very much for the suggestions.

I have implemented the changes you suggested but I unfortunately I am still greeted with this error. This error message is quite baffling… the same above script runs nicely on my desktop with no problems flagged. But when I open the same script on my laptop this error appears. It’s indeed quite confusing.

Thank you again for the suggestions.

The Stan code in your post seems to work for me. What versions are you using on the system which produces this error?


Hello @WardBrian, thank you very much - your message alone has provided me the solution! I am now realising something… my desktop was using the latest development version of RStan (i.e., 2.26.13) which the script worked fine; however, my laptop had the 2.21.1 version of RStan which for some reason my script didn’t work on.

I have removed the 2.21.1 version of RStan and installed version 2.26.13 [see here]. The script works perfectly now. Thank you very much - your message has made things click!