I am back again for help. In my modeling part, I have \mu for each observation. Therefore, I set up a \mu vector at very beginning. After that, I would like to define other parameters related to \mu. But I am not quite sure about what should I do.

model {
// other modeling part
for (k in 1:s) {
for (j in 1:n_s){
mu[(k*s-s+j)] = alpha[k] + X[(k*s-s+j), 1]*beta
lambda_tweedie[(k*s-s+j)] = 1/phi*mu[(k*s-s+j)]^(2-p)/(2-p);
alpha_tweedie[(k*s-s+j)] = (2-p)/(p-1);
beta_tweedie[(k*s-s+j)] = 1/phi*mu[(k*s-s+j)]^(1-p)/(p-1);
}
}
}
}

Now I am encountering error for this part. Would you please figure it out what should I do?

Thank you very much for your reply. Here is my original code:

model {
for (k in 1:s) {
for (j in 1:n_s){
mu[(k*s-s+j)] = alpha[k] + X[(k*s-s+j), 1]*beta
lambda_tweedie[(k*s-s+j)] = 1/phi*mu[(k*s-s+j)]^(2-p)/(2-p);
alpha_tweedie[(k*s-s+j)] = (2-p)/(p-1);
beta_tweedie[(k*s-s+j)] = 1/phi*mu[(k*s-s+j)]^(1-p)/(p-1);
}
}
}
}

Then console will return the following error message:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
p_star ~ normal(...)
Cannot assign to variable outside of declaration block; left-hand-side variable origin=parameter
error in 'model4d2c215e6b7a_model' at line 57, column 27
-------------------------------------------------
55: for (k in 1:s) {
56: for (j in 1:n_s){
57: mu[(k*s-s+j)] = alpha[k] + X[(k*s-s+j), 1]*beta
^
58:
-------------------------------------------------
PARSER EXPECTED: <expression assignable to left-hand side>
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) :

The error is exactly what the parser says it is. I can’t tell if mu is a parameter or data, but you’re in trouble either way. Outside of a transformed parameters block, it doesn’t make any sense to assign values to a parameter. You’re supposed to be finding the values of those parameters through MCMC or optimization. How would an assignment even work? Assigning to a data variable outside of a transformed data block doesn’t make any sense either.

The parser is also warning that you are probably doing something wrong with p_star as well, but I’d focus on fixing the more blatant logical inconsistencies in your model first. (Fixing that might help you find out how you’re doing something dodgy with p_star.)

transformed parameters {
vector[N] mu;
vector[N] lambda_tweedie;
vector[N] alpha_tweedie;
vector[N] beta_tweedie;
real p_star = log((p-1)/(2-p));
for (k in 1:s) {
for (j in 1:n_s){
mu[(k*n_s-n_s+j)] = exp(alpha[k] + X[(k*s-s+j), 1]*beta);
lambda_tweedie[(k*n_s-n_s+j)] = 1/phi*mu[(k*n_s-n_s+j)]^(2-p)/(2-p);
alpha_tweedie[(k*n_s-n_s+j)] = (2-p)/(p-1);
beta_tweedie[(k*n_s-n_s+j)] = 1/phi*mu[(k*n_s-n_s+j)]^(1-p)/(p-1);
}
}
}
model {
// other prior definition
p_star ~ normal(0, 10);
alpha ~ multi_normal_prec(zeros, delta*(T - rho*W_orignal));
for (k in 1:s) {
for (j in 1:n_s){
//mu[(k*n_s-n_s+j)] = exp(alpha[k] + X[(k*s-s+j), 1]*beta);
//lambda_tweedie[(k*n_s-n_s+j)] = 1/phi*mu[(k*n_s-n_s+j)]^(2-p)/(2-p);
//alpha_tweedie[(k*n_s-n_s+j)] = (2-p)/(p-1);
//beta_tweedie[(k*n_s-n_s+j)] = 1/phi*mu[(k*n_s-n_s+j)]^(1-p)/(p-1);
}
}
}
}

Here, alpha, beta, p, phi, and rho are all unknown parameters I want to figure out. Eventually, I received the following error messages:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Base type mismatch in assignment; variable name = mu, type = real; right-hand side type=vector
error in 'model4d2c54af6689_model' at line 46, column 31
-------------------------------------------------
44: for (k in 1:s) {
45: for (j in 1:n_s){
46: mu[(k*n_s-n_s+j)] = exp(alpha[k] + X[(k*s-s+j), 1]*beta);
^
47: lambda_tweedie[(k*n_s-n_s+j)] = 1/phi*mu[(k*n_s-n_s+j)]^(2-p)/(2-p);
-------------------------------------------------
PARSER EXPECTED: <expression assignable to left-hand side>
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) :

No. Variables are either parameters or data. They cannot be both.

I can try to explain the meaning of the syntax error, but I don’t think that would solve your problem.

What you should do, really, is to take a step back, slow down, and try to get the internal logic of your model straight before you code it in Stan. Make sure that you can specify your model in mathematical statements first. Otherwise, you are either going to keep chasing down syntax errors or trying to figure out why your model is somehow misbehaving.

Thank you for your reply, James. You are right. I am too “hurry” to code in Stan, since I have been stuck in this part for quite a while. I already completed my mathematics part and the model involves Tweedie distribution. There is not any function related to Tweedie in Stan so that we need to compute the pdf based on Tweedie’s definition. Tweedie can be also defined as compound possion gamma distribution so we need to conduct the above transformations to figure out Tweedie’s pdf.

I think I should clam down first. I am too greedy to get the simulation results for my thesis now.

The reason that I said what I said is that if you have a mathematical definition of your model – especially if it’s in the form of sampling statements – then it’s usually straightforward to translate that into the Stan language, because its notation tends to mirror the mathematical notation of models. Therefore, when you make very basic mistakes (like assigning a value to a parameter whose value you seek or assigning a vector to a scalar variable), I suspect that those mistakes may reflect a problem in the logic of your model.

Now it may be that the logic of your model is fine, and it’s just that it’s complicated enough that expressing it in the Stan language is no longer trivial. In that case, you still need to slow down and read through the documentation (probably starting with tutorials), because, again, you are making some basic mistakes that indicate that you don’t have a good grasp of Stan.

Thank you very much for your help. I think the main reason that my code is not working is due to the “transformed parameters” part. I tried the simplified case for the Tweedie distribution at very beginning, and Stan is working. However, when I updated with more complicated scenario, the code started popping out error messages.

For now, I would recommend avoiding using the transformed parameters block. Debugging will probably be simpler that way. If you need to declare variables that are functions of parameters, declare them in the model block.

If I move these transformation to the model part, I will receive the following error messages:

transformed parameters {
real p_star = log((p-1)/(2-p));
}
model {
p_star ~ normal(0, 10);
for (k in 1:s) {
for (j in 1:n_s){
mu[(k*n_s-n_s+j)] = exp(alpha[k] + X[(k*s-s+j), 1]*beta);
lambda_tweedie[(k*n_s-n_s+j)] = 1/phi*mu[(k*n_s-n_s+j)]^(2-p)/(2-p);
alpha_tweedie[(k*n_s-n_s+j)] = (2-p)/(p-1);
beta_tweedie[(k*n_s-n_s+j)] = 1/phi*mu[(k*n_s-n_s+j)]^(1-p)/(p-1);
}
}
}
}

And in the console, it will have:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
p_star ~ normal(...)
Cannot assign to variable outside of declaration block; left-hand-side variable origin=parameter
error in 'model10a24c7a71e0_model' at line 57, column 31
-------------------------------------------------
55: for (k in 1:s) {
56: for (j in 1:n_s){
57: mu[(k*n_s-n_s+j)] = exp(alpha[k] + X[(k*s-s+j), 1]*beta);
^
58:
-------------------------------------------------
PARSER EXPECTED: <expression assignable to left-hand side>
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) :

Looks like you’re still treating mu as a parameter. Just declare it as a vector at the top of the model block.

Remember that parameters are the quantities for which you are trying to “solve” for in some sense. They aren’t just generic variables or temporary values.

I think I get your point. In my code, I consider mu, lambda_tweedie, alpha_tweedie, and beta_tweedie as latent variables, which are by-pass products of evaluating other parameters. Once I followed you suggestion and declare all these four parameters at very beginning of my code, I received the error message due to type mismatch in mu’s equation.

model {
vector[N] mu;
vector[N] lambda_tweedie;
vector[N] alpha_tweedie;
vector[N] beta_tweedie;
int number;
p_star ~ normal(0, 10);
for (k in 1:s) {
for (j in 1:n_s){
number = k*n_s-n_s+j;
mu[number] = exp(alpha[k] + X[number, 1]*beta);
lambda_tweedie[number] = 1/phi*mu[number]^(2-p)/(2-p);
alpha_tweedie[number] = (2-p)/(p-1);
beta_tweedie[number] = 1/phi*mu[number]^(1-p)/(p-1);
}
}
}
}

The error message is as:

> stanmodel <- stan_model(file='model.stan')
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
p_star ~ normal(...)
Base type mismatch in assignment; variable name = mu, type = real; right-hand side type=vector
error in 'modelf0c6b935ad3_model' at line 61, column 24
-------------------------------------------------
59: number = k*n_s-n_s+j;
60:
61: mu[number] = exp(alpha[k] + X[number, 1]*beta);
^
62: lambda_tweedie[number] = 1/phi*mu[number]^(2-p)/(2-p);
-------------------------------------------------
PARSER EXPECTED: <expression assignable to left-hand side>
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\tay15002\Dropbox\Spatial Tweedie\CAR code\Simplified Model\model.stan'

Something on the right-hand side is apparently a vector. You know your code better than I, so you’ll have to track down that vector yourself.

Also, you should be leery of using beta as a variable name, since it’s the name of a distribution in Stan. I’m surprised the parser didn’t complain about that.

Thank you very much for your help. After printing out my code and going through it carefully, I eventually figured out where I did wrong. I defined the beta here as a vector, however, in my code, I referred it as a matrix. That is why I kept receiving these error messages.

Hi Tairan
I want to run a tweedie spatio-temporal model on my dataset with Bayesian estimation, like you. But I’m a green hand to stan and my codes have some errors. now, I have this error as you had, but I couldn’t solve it. may you guide me?
I appreciate any suggestion and help.