我试图建立一个简单的统计模型来研究随机模型和PyStan。
这里,这是我在PyStan中的模型描述,它不能很好地工作。请帮帮我。
Age来预测Val。其公式是由NormDist.Val大小为N,Age的最大值为Age效应,a_age[i]的效应取决于a_age[i-1]的效应。在这个脚本中,it.a_age[]是a_age[N]的缩写。stanexec= """
data {
int N;
int N_age;
vector[N] Val;
vector[N] Age;
}
parameters {
real mu;
vector[N_age] a_age;
real<lower=0> s_age;
real<lower=0> s;
}
model {
a_age[1] ~ normal(-sum(a_age[2:N_age]), 0.001);
a_age[2:N_age] ~ normal(a_age[1:(N_age-1)],s_age);
for (i in 1:N){
Val[i] ~ normal(mu+a_age[Age[i]],s);
}
}
"""这个剧本出了问题。其他型号运行良好,没有问题。因此,我希望在这个模型描述中存在遗漏。
ValueError Traceback (most recent call last)
<ipython-input-4-2e995ebf95f8> in <module>
11 thin=1,
12 warmup=100,
---> 13 seed=1
14 )
15 mcmc_sample = fit.extract(permuted=True)
~\Anaconda3\lib\site-packages\pystan\api.py in stan(file, model_name, model_code, fit, data, pars, chains, iter, warmup, thin, init, seed, algorithm, control, sample_file, diagnostic_file, verbose, boost_lib, eigen_lib, include_paths, n_jobs, **kwargs)
426 boost_lib=boost_lib, eigen_lib=eigen_lib,
427 include_paths=include_paths,
--> 428 obfuscate_model_name=obfuscate_model_name, verbose=verbose)
429 # check that arguments in kwargs are valid
430 valid_args = {"chain_id", "init_r", "test_grad", "append_samples", "enable_random_init",
~\Anaconda3\lib\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)
221 verbose=verbose,
222 include_paths=include_paths,
--> 223 obfuscate_model_name=obfuscate_model_name)
224
225 if not isinstance(stanc_ret, dict):
~\Anaconda3\lib\site-packages\pystan\api.py in stanc(file, charset, model_code, model_name, include_paths, verbose, obfuscate_model_name)
165 msg = msg.encode('ascii', 'replace')
166 error_msg = "Failed to parse Stan model '{}'. Error message:\n{}".format(model_name, msg)
--> 167 raise ValueError(error_msg)
168 elif result['status'] == 0: # SUCCESS_RC is 0
169 logger.debug("Successfully parsed Stan model '{}'.".format(model_name))
ValueError: Failed to parse Stan model 'anon_model_57b2ca24f38f5d3aa2d8b8fac976c276'. Error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Container index must be integer; found type=real
error in 'unknown file name' at line 21, column 35
-------------------------------------------------
19: a_age[2:N_age] ~ normal(a_age[1:(N_age-1)],s_age);
20: for (i in 1:N){
21: Val[i] ~ normal(mu+a_age[Age[i]],s_age);
^
22: }
-------------------------------------------------
PARSER EXPECTED: <one or more container indexes followed by ']'>发布于 2021-01-07 09:25:33
我知道已经有一段时间了,但也许其他人还需要解决办法。您所面临的问题是您只能使用整数进行索引。STAN的向量是实的。如果您想使用Age的一个元素(它是一个向量)进行索引,则使用实数进行索引。这不可能。解决方案是将Age声明为整数数组,如下所示:
int Age[N];这应该能解决你的问题。
https://stackoverflow.com/questions/62850138
复制相似问题