I am a green hand to Stan and have a question about my first Stan code. The following is my Stan code:
data {
int n_s;
int s;
int N;
int x_p;
int M;
real<lower=0> Y[N];
matrix[N, x_p] X;
matrix[s, s] W_orignal;
real eigenvalue_min;
real eigenvalue_max;
}
transformed data {
vector[s] zeros;
zeros = rep_vector(0, s);
matrix[s, s] T;
vector[s] W_rowsums;
for (i in 1:s) {
W_rowsums[i] = sum(W_orignal[i, ]);
}
T = diag_matrix(W_rowsums);
}
parameters {
//tweedie
real<lower=0> mu[N];
real<lower=0> phi;
real<lower=1, upper=2> p;
vector[N] lambda_tweedie;
vector[N] alpha_tweedie;
vector[N] beta_tweedie;
//log link function
vector[s] alpha;
vector[x_p] beta;
//CAR model
real<lower=0> delta;
real rho;
}
transformed parameters {
real p_star = log((p-1)/(2-p));
}
Once complied, the console will return the error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
variable "matrix" does not exist.
error in 'model4d2c455a7f93_model' at line 19, column 10
-------------------------------------------------
17: zeros = rep_vector(0, s);
18:
19: matrix[s, s] T;
^
20:
-------------------------------------------------
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model 'model' due to the above error.
In addition: Warning message:
In readLines(file, warn = TRUE) :
incomplete final line found on 'C:\Users\Qiangsuper\Dropbox\Spatial Tweedie\CAR code\Simplified Model\model.stan'
I try to solve it couple times and all failed. I think it might due to this part:
vector[s] zeros;
zeros = rep_vector(0, s);
I appreciate any suggestion and help. Besides, any suggestion for other part of my code is welcome.
This is due to declaring some variable (T) after another variable has been declared and separately defined (zeros). All declarations have to come at the top of a block “before” any definitions, although a simultaneous declaration and definition is acceptable and allows you to declare and define variables subsequently. In addition, it is the preferred syntax. So, your transformed data block should be
transformed data {
vector[s] zeros = rep_vector(0, s);
matrix[s, s] T;
vector[s] W_rowsums;
for (i in 1:s) W_rowsums[i] = sum(W_original[i, ]);
T = diag_matrix(W_rowsums);
}
All that said, using the Tweedie distribution will probably not work well in Stan unless you are prepared to go Beast Mode.
I appreciate your help. Thank you very much. Our approach to Tweedie requires too much computation. Other than code by myself, I would like to try to code in Stan. Would you please tell more about the Beast Mode?
Beast Mode is sadly not a configuration option for Stan, but rather a state a mind. When in Beast Mode, you can read through papers such as
and think to yourself, “that might actually work in Stan”, despite the many reasons why it might not actually work in Stan. One of the many reasons it might not actually work in Stan is that you would need numerical integration to do the Fourier inversion when p \approx 2, but that will be exposed in the next version of Stan.
I see, Ben. Thank you very much for your help. Right now I am working on Compound Poisson Gamma distribution, which means p is within 1 and 2. Hopefully Stan can do a good job in this situation.
By the way, might I ask you to help with my modeling part? The link of question is here:modeling question
This is a little strange. I haven’t faced this problem when using CmdStan. But need to put all the declarations at the top of the block when using rStan.
I think that’s because CmdStan is several versions of Stan ahead of RStan at the moment. Right now to get the latest Stan features in R you can use the new CmdStanR interface.