List structures in Stan


Your problem is that you are searching for software that has an implementation written for you instead of implementing the pseudo-likelihood function yourself. I would suggest that you:

  1. Figure out what are the moment restrictions you want to impose. These are the same as they would be with frequentist GMM, but in 20 posts on this thread you have managed to avoid articulating what they are.
  2. Learn the Stan language by reading the Stan User Manual
  3. Write a function in the Stan language called pseudolikelihood that inputs the parameters and returns -0.5 * quad_form(U, inverse_spd(Sigma)) where U and Sigma are from section 2.1 of the Yin paper and depend on the the moment restrictions you want to impose.
  4. Check that you have not made a mistake by exporting that function via the expose_stan_functions function in rstan.
  5. Add the line target += pseudolikelihood(...) to the model block of your Stan program, where ... is a shorthand for the necessary arguments to this function that you wrote
  6. Add informative priors on the unknown parameters in the model block of your Stan program
  7. Look carefully for warnings and problems with the results. Since the Yin paper utilizes something that is not quite a likelihood function, and Stan is very precise, there will likely be problems. In that case, you will probably have to specify a genuine likelihood function and your six month quest to do Bayesian GMM will have been a complete waste of time.


thank you very much for your suggestion sir.
I am going to follow your suggestion step by step, If in this process I
pace some difficulties then I will contact you. Bay…


Hello sir how are you?
Sir Is there is available the stan software freely. I have available that one. I think this is not required or is
suitable for my work.


That is the wrong Stan. You need . And to know what software you are talking about before you ask a bunch of questions.


Thank you sir for correction But I think that stan is
not freely available. If you have some link for downloading that one
software freely please share me I will be grateful.


That link literally says

Stan is freedom-respecting, open-source software (new BSD core, some interfaces GPLv3).


Help me to fitting the Bayesian Generalized Method of Moment the Stan coding is following

//Simple Bayesian Generalized Method of Moments Model
//Essentially this model is the Bayesian analog to a Frequentist GEE model with
//an indepdent working correlation matrix
//the data consists of N 2-person families, thus we do not have independent observations
//however, GEE can be used to conduct valid inference on the mean parameters even given
//the fact that our working correlation matrix (I) is incorrect.

data {
int<lower=0> N; // number of 2-person families
real Y[N2]; // response, ordered by family
real X1[N
2]; // 1 covariate
real N2;
parameters {
real beta0; //intercept
real beta1; //covariate effect
real<lower=0> phi; //variance
model {
real yhat[2];
real sigmak[2];
real sk[2];
vector[4] Fk;
matrix[4,3] D;
matrix[4,4] V;
matrix[3,N] utemp;
vector[3] u;
vector[3] U;
matrix[3,3] u2temp = rep_matrix(0, 3, 3);
matrix[3,3] Sigma;


for(i in 1:N){
yhat[1]= (beta0+beta1X1[2i-1]);
yhat[2]= (beta0+beta1X1[2i]);
D[1,1]=1.0; D[2,1]=1.0; D[3,1]=0.0; D[4,1]=0.0;
D[1,2]=X1[2i-1]; D[2,2]=X1[2i]; D[3,2]=0.0; D[4,2]=0.0;
D[1,3]=0.0; D[2,3]=0.0; D[3,3]=1.0; D[4,3]=1.0;
V[1,1]=phi; V[2,1]=0.0; V[3,1]=0.0; V[4,1]=0.0;
V[1,2]=0.0; V[2,2]=phi; V[3,2]=0.0; V[4,2]=0.0;
V[1,3]=0.0; V[2,3]=0.0; V[3,3]=2phi^2; V[4,3]=0.0;
V[1,4]=0.0; V[2,4]=0.0; V[3,4]=0; V[4,4]=2

//u constains the 3 estimating functions for beta0, beta1, and phi
u= D’*inverse(V)Fk;
u2temp = u2temp + u

//U is the sample mean of the N u’s
//U is asymptotically normal with mean 0 and variance consistently estimating by Sigma
//Bayesian GMM works by applying a normal likelihood to U, and then uses MCMC to estimate //the posterior of all parameters
U[1]=mean(utemp[1,]); U[2]=mean(utemp[2,]); U[3]=mean(utemp[3,]);
Sigma= 1/N2^2 * u2temp - 1/N2 * U*U’;

//pseudolikelihood for GMM (Yin 2009 Bayes GMM paper doesnt use normalizing constant):
//However, model will not fit with or without the normalizing constant…
//target+= -3.0/2.0log(23.14159265359)-0.5log_determinant(Sigma)-0.5U’*inverse(Sigma)U;
target += -0.5

fit = stan(file = “stan_GEE1_ind.stan”,data = data, cores = 1, chains = 1, iter = 10)
Result is following

Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.
Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.
Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.
Rejecting initial value:

Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[2] “In addition: Warning message:”
[3] “In readLines(file, warn = TRUE) :”
[4] " incomplete final line found on ‘C:\Users\Muhammad Zada\Desktop\New folder\stan_GEE1_ind.stan’"
[1] "error occurred during calling the sampler; sampling not done"
please help me…


Hello sir hope you will be fine,
How I download the Stan program language interface, In which I write the
function for pseudo likelihood which is alternative for likelihood please
sir give me the exact link for downloading the Stan interface.



I believe you asked the same question on Jun 10th 2017

go to for all the interfaces (R, Python, etc…)


Ok but I want to write the function for my model in Stan probability
language, and interface we can run the the function of the model only.
Actually here I am not clear that I think command function is written in
Stan language and run in R or Python interface let me clear about that.
Commonly the function have being made in Stan language and then run only in
the Interface of R or any other software. Are we can do the coding or
write the function in R interface?.



before proceeding:

  1. make sure you can compile and run the example exercise on this link

After you read the page carefully, and you can execute every single command on that page, you will be able to:

  1. start with the examples in the documentation manual ( After you have successfully run 2 or 3 models in the manual with dummy data, you can:

  2. go on with your model and ask more informed questions, for the community to support you.

Probably, it will take you 3 or 4 weeks before you can be proficient with your own model.



Hello sir I have study your mentions materiel, now I want to write the
function for Pseudo likelihood function estimation, but I don’t have the
Stan probability programming language in which I write the function please
mail me the link for downloading Stan programming language. thanks


Please follow my advise. Do not only study but,

  1. replicate all lines of code in the website given on your computer, and
  2. replicate 2, 3 models in the reference manual given, on your computer before even thinking about your model
  3. please paste here all command line of code you have done for exercise, here.
  4. then I will be keen to listen to other questions you might have

Did you download stan program? Are you using R or Python or some other platform to work?


I am using only Rstudio sir …

  1. Install RStan install.packages(“rstan”)

  2. execute all lines of code here

  3. please do everything I said in the previous message

  4. please do not touch your model until everything I asked is completed


Hello sir How are you I want to run the following stan example in r, For
that I follow the following steps but the example did not run it give the
following error please sir help me about it that where is the mistake
mention that line

rstan_options(auto_write = TRUE)

options(mc.cores = parallel::detectCores())

schools_dat <- list(J = 8,

  •                 y = c(28,  8, -3,  7, -1,  1, 18, 12),
  •                 sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

fit <- stan(file = ‘8schools.stan’, data = schools_dat,

  •         iter = 1000, chains = 4)

Error in file(fname, “rt”) : cannot open the connection
In addition: Warning messages:
1: In normalizePath(path.expand(path), winslash, mustWork) :
path[1]=“8schools.stan”: The system cannot find the file specified
2: In file(fname, “rt”) :
cannot open file ‘C:\Users\Muhammad\Desktop\8schools.stan\8schools.stan’:
No such file or directory
Error in get_model_strcode(file, model_code) :
cannot open model file


You need to provide a path to the file.


Sir my program give me the following result it did not run please help
point out that where is the mistake.




fit = stan(file = “stan_GEE1_ind.stan”,data = data, cores = 1, chains =
1, iter = 10)
In file included from
from file9fc2ef267d7.cpp:8:
warning: “BOOST_NO_CXX11_RVALUE_REFERENCES” redefined


:0:0: note: this is the location of the previous definition
In file included from


Hello sir with second attempt to fit my endogenietic model is fail to fir
the result is following please sir help me to fit the model mention the
mistake points



fit = stan(file = “stan_GEE1_ind.stan”,data = data, cores = 1, chains =
1, iter = 10)

Error in object@mk_cppmodule(object) :
could not find function "prep_call_sampler"
In addition: Warning message:
In readLines(file, warn = TRUE) :
incomplete final line found on ‘C:\Users\Muhammad\Desktop\stan


this is the coding of model fitting