0

I am trying to build a simple statistical model to study both stochastic model and PyStan.
Here, this is my model description in PyStan which does not work well. Please help me.

  • Model is to predict Val by person's Age. Its formulation is that Val is generated by NormDist.
  • Data size is N, and max value of the Age is N_age.
  • the effect of a_age[i] depends on the effect of a_age[i-1]. In this script, a_age[N] stands for it.
  • a_age[] is normalized.

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);
            } 
    }
    """

This script goes wrong. Other models work well without problems. So, I expect miss exists in this model description.

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 ']'>
4

1 回答 1

1

我知道已经有一段时间了,但也许其他人仍然需要解决方案。您面临的问题是您只能使用整数进行索引。STAN 中的向量是实数类型。如果要使用 的元素(Age即向量)进行索引,则使用实数进行索引。那是不可能的。解决方案是声明Age为整数数组,如下所示:

int Age[N];

那应该可以解决您的问题。

于 2021-01-07T09:25:33.983 回答