List structures in Stan


#41

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.

#42

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…


#43

Hello sir how are you?
Sir Is there is available the stan software freely. I have available
www.stan2web.net that one. I think this is not required or is
suitable for my work.
https://www.google.com.hk/search?q=stan+softwar+logo&rlz=1C1NHXL_enPK765PK765&oq=stan+softwar+logo&aqs=chrome..69i57.28544j1j4&sourceid=chrome&ie=UTF-8#

http://webcache.googleusercontent.com/search?q=cache:cfWZVxkQP-wJ:www.stan2web.net/+&cd=16&hl=en&ct=clnk&gl=hk
2.
https://www.google.com.hk/search?rlz=1C1NHXL_enPK765PK765&q=related:www.stan2web.net/+stan+software+logo&tbo=1&sa=X&ved=0ahUKEwj5nOjJkN_WAhVGRSYKHaeiBUcQHwhoMA8


#44

That is the wrong Stan. You need http://mc-stan.org/ . And to know what software you are talking about before you ask a bunch of questions.


#45

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


#46

That link literally says

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


#47

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;

//priors:
beta0~normal(0,100000);
beta1~normal(0,100000);
phi~uniform(0,100000);

for(i in 1:N){
yhat[1]= (beta0+beta1X1[2i-1]);
yhat[2]= (beta0+beta1X1[2i]);
sigmak[1]=phi;
sigmak[2]=phi;
sk[1]=(Y[2i-1]-yhat[1])^2;
sk[2]=(Y[2
i]-yhat[2])^2;
Fk[1]=Y[2i-1]-yhat[1];
Fk[2]=Y[2
i]-yhat[2];
Fk[3]=sk[1]-sigmak[1];
Fk[4]=sk[2]-sigmak[2];
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
phi^2;

//u constains the 3 estimating functions for beta0, beta1, and phi
u= D’*inverse(V)Fk;
utemp[1:3,i]=u;
u2temp = u2temp + u
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
U’*inverse(Sigma)*U;

library(rstan)
load(“GEE1data1.RData”)
data=list(Y=simdata$Y,X1=simdata$X1,N=length(table(simdata$famID)),N2=100.0)
fit = stan(file = “stan_GEE1_ind.stan”,data = data, cores = 1, chains = 1, iter = 10)
Result is following

SAMPLING FOR MODEL ‘stan_GEE1_ind’ NOW (CHAIN 1).
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…


#48

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.


#49

Hello,

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

go to https://github.com/stan-dev for all the interfaces (R, Python, etc…)


#50

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?.


#51

Hello,

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 (https://github.com/stan-dev/stan/releases/download/v2.16.0/stan-reference-2.16.0.pdf). 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.

Bw.


#52

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


#53

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?


#54

I am using only Rstudio sir …


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

  2. execute all lines of code here https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started

  3. please do everything I said in the previous message

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


#56

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

library(rstan)
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
"C:\Users\Muhammad\Desktop\8schools.stan\8schools.stan"


#57

You need to provide a path to the file.


#58

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

library(rstan)

load(“GEE1data1.RData”)

data=list(Y=simdata$Y,X1=simdata$X1,N=length(table(simdata$famID)),N2=100.0)

fit = stan(file = “stan_GEE1_ind.stan”,data = data, cores = 1, chains =
1, iter = 10)
In file included from
C:/Users/Muhammad/Documents/R/win-library/3.3/BH/include/boost/config.hpp:39:0,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/BH/include/boost/math/tools/config.hpp:13,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/core.hpp:12,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/StanHeaders/include/stan/math/rev/mat.hpp:4,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/StanHeaders/include/stan/math.hpp:4,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file9fc2ef267d7.cpp:8:
C:/Users/Muhammad/Documents/R/win-library/3.3/BH/include/boost/config/compiler/gcc.hpp:186:0:
warning: “BOOST_NO_CXX11_RVALUE_REFERENCES” redefined

define BOOST_NO_CXX11_RVALUE_REFERENCES

^
:0:0: note: this is the location of the previous definition
In file included from
C:/Users/Muhammad/Documents/R/win-library/3.3/BH/include/boost/multi_array/base.hpp:28:0,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/BH/include/boost/multi_array.hpp:21,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/BH/include/boost/numeric/odeint/util/multi_array_adaption.hpp:29,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/BH/include/boost/numeric/odeint.hpp:61,
from
C:/Users/Muhammad/Documents/R/win-library/3.3/StanHeaders/include/


#59

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

library(rstan)
load(“GEE1data1.RData”)

data=list(Y=simdata$Y,X1=simdata$X1,N=length(table(simdata$famID)),N2=100.0)

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
coding\stan_GEE1_ind.stan’


#60

this is the coding of model fitting