I am returning to an old-ish project on a new computer, and I’m having problems compiling a model with PyStan. This is in the context of Mojave 10.14.3, (Anaconda) Python 3.7.2, PyStan 2.18.1, gcc version 4.8.5.
It’s been a while since I used (Py)Stan, so I don’t know if there is something wrong with my model, which is a multilevel logistic regression model with a bunch of intercepts:
data {
int nobs; // number of observations
int nreg; // number of regions
int npat; // number of patients
int nmat; // number of materials
int<lower=0,upper=1> y[nobs]; // present = 1, absent = 0
int p[nobs]; // patient index
int r[nobs]; // region index
int m[nobs]; // material index
}
parameters {
real a_o; // overall intercept
real a_p[npat]; // patient intercept
real a_m[nmat]; // material intercepts
real a_r[nreg]; // region intercept
real<lower=0.01> sd_p; // patient int sd
}
model {
// data model
for(i in 1:nobs)
y[i] ~ bernoulli(inv_logit(a_o + a_p[p[i]] + a_m[m[i]] + a_r[r[i]]));
// model model
a_o ~ normal(0,2);
a_m ~ normal(0,2);
a_r ~ normal(0,2);
a_p ~ normal(0,sd_p);
sd_p ~ gamma(3,2);
}
In case it’s relevant, the data gets processed with this code (all of the *idx columns are 1-indexed, not 0-indexed):
df = pd.read_csv('../data_by_region.csv').dropna()
y = df['pa'].astype(int).values
nobs = len(y)
r = df['ridx'].astype(int).values
nreg = len(df['ridx'].unique())
p = df['pidx'].astype(int).values
npat = len(df['pidx'].unique())
m = df['midx'].astype(int).values
nmat = len(df['midx'].unique())
dd = {'nobs':nobs, 'nreg':nreg, 'npat':npat, 'nmat':nmat, 'y':y, 'p':p, 'r':r, 'm':m}
When I try to run the script to process the data and fit the model, I get this:
INFO:pystan:COMPILING THE C++ CODE FOR MODEL lr_swallow_region_009d04776976ca2ad4be9a43ddaedb29 NOW.
---------------------------------------------------------------------------
DistutilsExecError Traceback (most recent call last)
/anaconda3/lib/python3.7/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
117 self.spawn(compiler_so + cc_args + [src, '-o', obj] +
--> 118 extra_postargs)
119 except DistutilsExecError as msg:
/anaconda3/lib/python3.7/distutils/ccompiler.py in spawn(self, cmd)
908 def spawn(self, cmd):
--> 909 spawn(cmd, dry_run=self.dry_run)
910
/anaconda3/lib/python3.7/distutils/spawn.py in spawn(cmd, search_path, verbose, dry_run)
35 if os.name == 'posix':
---> 36 _spawn_posix(cmd, search_path, dry_run=dry_run)
37 elif os.name == 'nt':
/anaconda3/lib/python3.7/distutils/spawn.py in _spawn_posix(cmd, search_path, verbose, dry_run)
158 "command %r failed with exit status %d"
--> 159 % (cmd, exit_status))
160 elif os.WIFSTOPPED(status):
DistutilsExecError: command 'gcc' failed with exit status 1
During handling of the above exception, another exception occurred:
CompileError Traceback (most recent call last)
~/Google Drive/analysis/mia/pystan/pystan_swallow_region.py in <module>()
28
29 if model_pkl not in pkl_files:
---> 30 model = ps.StanModel(file='lr_swallow_region.stan',model_name='lr_swallow_region')
31
32 with open(model_pkl,'wb') as f:
/anaconda3/lib/python3.7/site-packages/pystan/model.py in __init__(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args)
347
348 try:
--> 349 build_extension.run()
350 finally:
351 if redirect_stderr:
/anaconda3/lib/python3.7/distutils/command/build_ext.py in run(self)
337
338 # Now actually compile and link everything.
--> 339 self.build_extensions()
340
341 def check_extensions_list(self, extensions):
/anaconda3/lib/python3.7/distutils/command/build_ext.py in build_extensions(self)
446 self._build_extensions_parallel()
447 else:
--> 448 self._build_extensions_serial()
449
450 def _build_extensions_parallel(self):
/anaconda3/lib/python3.7/distutils/command/build_ext.py in _build_extensions_serial(self)
471 for ext in self.extensions:
472 with self._filter_build_errors(ext):
--> 473 self.build_extension(ext)
474
475 @contextlib.contextmanager
/anaconda3/lib/python3.7/distutils/command/build_ext.py in build_extension(self, ext)
531 debug=self.debug,
532 extra_postargs=extra_args,
--> 533 depends=ext.depends)
534
535 # XXX outdated variable, kept here in case third-part code
/anaconda3/lib/python3.7/distutils/ccompiler.py in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
572 except KeyError:
573 continue
--> 574 self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
575
576 # Return *all* object filenames, not just the ones we just built.
/anaconda3/lib/python3.7/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
118 extra_postargs)
119 except DistutilsExecError as msg:
--> 120 raise CompileError(msg)
121
122 def create_static_lib(self, objects, output_libname,
CompileError: command 'gcc' failed with exit status 1
I can’t just post the data publicly, but I could email it to someone along with the script if that would help diagnose the problem. And I’m happy to share any other relevant information, if I missed anything above. Thanks.