I’m attempting to build a pystan model (multilevel ordered logit) using a .stan
script that previously compiled, with the only difference being the addition of whitespace (I need to make additional edits, and the issue is recurring, so I’m not able to simply revert). When I run stan.build(script, data = stan_data)
(OS: macOS Montrey 12.4, pystan 3.5.0, python 3.9.12, anaconda 4.12.0), I’m thrown:
Building...
Error handling request
Traceback (most recent call last):
File "/Users/bennett/opt/anaconda3/lib/python3.9/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
resp = await request_handler(request)
File "/Users/bennett/opt/anaconda3/lib/python3.9/site-packages/aiohttp/web_app.py", line 504, in _handle
resp = await handler(request)
File "/Users/bennett/opt/anaconda3/lib/python3.9/site-packages/httpstan/views.py", line 103, in handle_create_model
_, stanc_warnings = httpstan.compile.compile(program_code, stan_model_name)
File "/Users/bennett/opt/anaconda3/lib/python3.9/site-packages/httpstan/compile.py", line 37, in compile
completed_process = subprocess.run(run_args, capture_output=True, timeout=1)
File "/Users/bennett/opt/anaconda3/lib/python3.9/subprocess.py", line 507, in run
stdout, stderr = process.communicate(input, timeout=timeout)
File "/Users/bennett/opt/anaconda3/lib/python3.9/subprocess.py", line 1134, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/Users/bennett/opt/anaconda3/lib/python3.9/subprocess.py", line 1980, in _communicate
self._check_timeout(endtime, orig_timeout, stdout, stderr)
File "/Users/bennett/opt/anaconda3/lib/python3.9/subprocess.py", line 1178, in _check_timeout
raise TimeoutExpired(
subprocess.TimeoutExpired: Command '[PosixPath('/Users/bennett/opt/anaconda3/lib/python3.9/site-packages/httpstan/stanc'), '--name', 'model_ntce4pp2', '--warn-pedantic', '--print-cpp', '/var/folders/9t/53m4vvyn28d08r7hr7_h9xf80000gn/T/httpstan__n4i7362/model_ntce4pp2.stan']' timed out after 1 seconds
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 posterior = stan.build(script, data = stan_data)
File ~/opt/anaconda3/lib/python3.9/site-packages/stan/model.py:518, in build(program_code, data, random_seed)
515 return Model(model_name, program_code, data, param_names, constrained_param_names, dims, random_seed)
517 try:
--> 518 return asyncio.run(go())
519 except KeyboardInterrupt:
520 return
File ~/opt/anaconda3/lib/python3.9/site-packages/nest_asyncio.py:35, in _patch_asyncio.<locals>.run(main, debug)
33 task = asyncio.ensure_future(main)
34 try:
---> 35 return loop.run_until_complete(task)
36 finally:
37 if not task.done():
File ~/opt/anaconda3/lib/python3.9/site-packages/nest_asyncio.py:89, in _patch_loop.<locals>.run_until_complete(self, future)
86 if not f.done():
87 raise RuntimeError(
88 'Event loop stopped before Future completed.')
---> 89 return f.result()
File ~/opt/anaconda3/lib/python3.9/asyncio/futures.py:201, in Future.result(self)
199 self.__log_traceback = False
200 if self._exception is not None:
--> 201 raise self._exception
202 return self._result
File ~/opt/anaconda3/lib/python3.9/asyncio/tasks.py:256, in Task.__step(***failed resolving arguments***)
252 try:
253 if exc is None:
254 # We use the `send` method directly, because coroutines
255 # don't have `__iter__` and `__next__` methods.
--> 256 result = coro.send(None)
257 else:
258 result = coro.throw(exc)
File ~/opt/anaconda3/lib/python3.9/site-packages/stan/model.py:485, in build.<locals>.go()
482 resp = task.result()
484 if resp.status != 201:
--> 485 match = re.search(r"""ValueError\(['"](.*)['"]\)""", resp.json()["message"])
486 if not match: # unknown error, should not happen
487 raise RuntimeError(resp.json()["message"])
File ~/opt/anaconda3/lib/python3.9/site-packages/stan/common.py:25, in HTTPResponse.json(self)
23 def json(self) -> dict:
24 # mypy 0.961 complains that simdjson lacks a `loads`.
---> 25 return simdjson.loads(self.content)
File ~/opt/anaconda3/lib/python3.9/site-packages/simdjson/__init__.py:61, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kwargs)
58 s = s.encode('utf-8')
60 parser = Parser()
---> 61 return parser.parse(s, True)
ValueError: The JSON document has an improper structure: missing or superfluous commas, braces, missing keys, etc.
Any guidance on how to resolve? Thank you for your time and help.
P.S. The relevant .stan script is:
data {
// Training Data (Individual-Level)
/// Model Metadata
int<lower=0> N; // Number of interviews
int<lower=0> N_y; // Number of target classes
/// Number of Levels for Varying Intercepts
int<lower=0> N_gender;
int<lower=0> N_parental_status;
int<lower=0> N_marital_status;
int<lower=0> N_age;
int<lower=0> N_education;
int<lower=0> N_race;
/// Group-Level Indices
int<lower=1> N_state;
/// Interview Targets
array[N] int<lower=1,upper= N_y> y;
/// Varying Intercept Levels
array[N] int<lower=1,upper=N_gender> gender;
array[N] int<lower=1,upper=N_parental_status> parental_status;
array[N] int<lower=1,upper=N_marital_status> marital_status;
array[N] int<lower=1,upper=N_age> age;
array[N] int<lower=1,upper=N_education> education;
array[N] int<lower=1,upper=N_race> race;
array[N] int<lower=1,upper=N_state> state;
}
parameters {
// Target Cutpoints
ordered[N_y - 1] c;
// Prior Uncertainties for Demographic Varying Intercepts
real<lower=0> sigma_gender;
real<lower=0> sigma_parental_status;
real<lower=0> sigma_marital_status;
real<lower=0> sigma_age;
real<lower=0> sigma_education;
real<lower=0> sigma_race;
real<lower=0> sigma_state;
// Varying Intercepts
/// Real codings for dichotomous Varying Intercepts
real alpha_gender_raw;
real alpha_parental_status_raw;
real alpha_marital_status_raw;
/// Vector codings for multi-level Varying Intercepts
vector[N_age] alpha_age_raw;
vector[N_education] alpha_education_raw;
vector[N_race] alpha_race_raw;
vector[N_state] alpha_state_raw;
}
transformed parameters{
// Varying Intercepts
/// Real codings for dichotomous Varying Intercepts
real alpha_gender;
real alpha_parental_status;
real alpha_marital_status;
/// Vector codings for multilevel Varying Intercepts
vector[N_age] alpha_age;
vector[N_education] alpha_education;
vector[N_race] alpha_race;
vector[N_state] alpha_state;
// Non-Centered Parametrization for Varying Intercepts
/// Dichotomous Intercepts
alpha_gender = alpha_gender_raw * sigma_gender;
alpha_parental_status = alpha_parental_status_raw * sigma_parental_status;
alpha_marital_status = alpha_marital_status_raw * sigma_marital_status;
/// Multilevel Intercepts
for (n in 1:N_age){
alpha_age[n] = alpha_age_raw[n] * sigma_age;
}
for (n in 1:N_education){
alpha_education[n] = alpha_education_raw[n] * sigma_education;
}
for (n in 1:N_race){
alpha_race[n] = alpha_race_raw[n] * sigma_race;
}
for (n in 1:N_state){
alpha_state[n] = alpha_state_raw[n] * sigma_state;
}
}
model {
// Varying Intercept Prior SDs
sigma_gender ~ std_normal();
sigma_parental_status ~ std_normal();
sigma_marital_status ~ std_normal();
sigma_age ~ std_normal();
sigma_education ~ std_normal();
sigma_race ~ std_normal();
sigma_state ~ std_normal();
// Varying Intercepts
/// Dichotomous Intercepts
alpha_gender_raw ~ std_normal();
alpha_parental_status_raw ~ std_normal();
alpha_marital_status_raw ~ std_normal();
/// Multilevel Intercepts
for (n in 1:N_age){
alpha_age_raw[n] ~ std_normal();
}
for (n in 1:N_education){
alpha_education_raw[n] ~ std_normal();
}
for (n in 1:N_race){
alpha_race_raw[n] ~ std_normal();
}
for (n in 1:N_state){
alpha_state_raw[n] ~ std_normal();
}
// Training Loop
for (n in 1:N){
y[n] ~ ordered_logistic([alpha_gender, -alpha_gender][gender[n]] +[alpha_parental_status, -alpha_parental_status][parental_status[n]] + [alpha_marital_status, -alpha_marital_status][marital_status[n]] + alpha_age[age[n]] + alpha_education[education[n]] + alpha_race[race[n]] + alpha_state[state[n]], c);
}
}