Simple model Parse Errors

Can I just clarify that in the situation where we define x = a + b in the transformed parameters block and then put a likelihood (I’m not sure if that’s what it should be called) on x in the parameters block as x ~ baz(), we do not have to add a Jacobian or similar adjustment to the model likelihood?

In my case I would like to constrain a derived parameter x = a * b / (c * d). In this case, is what is described in this thread still relevant?

Yes. If you have a,b,c, and d as parameters and x = f(a,b,c,d) ~ baz() in model then you’ll face exactly the same problem, namely there’s no Jacobian adjustment for f and the likelihood is not sufficient to identify the model.

1 Like

Hi there,

I am pretty new to R/RStan, so apologies in advance if this doesn’t make much sense. I have uploaded the RStan file I have been using to this post. When I run the following R code:

library(readr)
library(“rstan”)
options(mc.cores=parallel::detectCores())
rstan_options(auto_write = TRUE)

setwd(“c:/Users/jlevy/Dropbox (Sydney Uni)/Twins reference point/Analysis_XW/twin data/BHM”)

library(readxl)
sample ← read_csv(“/Users/jlevy/Dropbox (Sydney Uni)/Twins reference point/Analysis_XW/twin data/BHM/for_rstan_JL.csv”)

N=nrow(sample)
nsubj=max(sample[‘sid’])
subjs=unlist(sample[‘sid’])
choices=unlist(sample[‘chosea’])
x1a=unlist(sample[‘x1a’])
x2a=unlist(sample[‘x2a’])
x3a=unlist(sample[‘x3a’])
x4a=unlist(sample[‘x4a’])
x1b=unlist(sample[‘x1b’])
x2b=unlist(sample[‘x2b’])
x3b=unlist(sample[‘x3b’])
x4b=unlist(sample[‘x4b’])
p1a=unlist(sample[‘p1a’])
p2a=unlist(sample[‘p2a’])
p3a=unlist(sample[‘p3a’])
p4a=unlist(sample[‘p4a’])
p1b=unlist(sample[‘p1b’])
p2b=unlist(sample[‘p2b’])
p3b=unlist(sample[‘p3b’])
p4b=unlist(sample[‘p4b’])
sid=unlist(sample[‘sid’])
sq=unlist(sample[‘sq’])
eva=unlist(sample[‘eva’])
evb=unlist(sample[‘evb’])
maxmin=unlist(sample[‘maxmin’])
minmax=unlist(sample[‘minmax’])
xatmaxp=unlist(sample[‘xatmaxp’])

fit<- stan(file=‘bhm_5rp_JL.stan’,
data=list(N=N,nsubj=nsubj,subjs=subjs,choices=choices,x1a=x1a,x2a=x2a,x3a=x3a,x4a=x4a,
x1b=x1b,x2b=x2b,x3b=x3b,x4b=x4b,p1a=p1a,p2a=p2a,p3a=p3a,p4a=p4a,p1b=p1b,p2b=p2b,p3b=p3b,p4b=p4b,sid=sid,sq=sq,eva=eva,evb=evb,maxmin=maxmin,minmax=minmax,xatmaxp=xatmaxp),
warmup=2500,
chains=4,
iter=10000,
init_r=1,
verbose=FALSE)
print(fit)
fit_ss ← extract(fit,permuted=TRUE)

I get the following error message:

Error in stanc(file = file, model_code = model_code, model_name = model_name, : **
** parser failed badly

Can someone please advise?

Thanks in advance!

bhm_5rp_JL.stan (18.3 KB)

The stanc parser can sometimes fail if you have a # in any of your comments, which you have at the start:

 // # of subjects

After removing that, there are also two parsing errors:

  • eua_com and eub_com are used without first declaring them

  • Missing a + after eub_minmax in the construction of eub_com

I’ve attached the stan file with those things fixed, which parses for me in RStan:
bhm_5rp_JL_fixed.stan (17.8 KB)

Thanks so much for your help! Really appreciate it!

@andrjohns is there any plan to fix that bug in RStan? It really shouldn’t be failing at # symbols

@hsbadr I believe you fixed this in the experimental branch right? Would you mind back-porting it to 2.26?

The latest experimental is better, but a model like this still trips it up:

functions {
  /*
  #include to load in common functions
  */
  #include funs.stan
}

In general doing includes requires at least one bit of state about whether or not you’re in a comment, you can’t do it with pure string replacement.