# Cannot assign to variable

**URL:** <https://discourse.mc-stan.org/t/cannot-assign-to-variable/31902>\
**Category:** Modeling\
**Tags:** fitting-issues, rstan\
**Created:** [June 28, 2023, 5:53pm UTC](https://discourse.mc-stan.org/t/cannot-assign-to-variable/31902 "2023-06-28T17:53:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LuizRocha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/luizrocha/32/16599_2.png) [@LuizRocha](https://discourse.mc-stan.org/u/LuizRocha)\
**Post date:** [June 28, 2023, 5:53pm UTC](https://discourse.mc-stan.org/t/cannot-assign-to-variable/31902/1 "2023-06-28T17:53:28Z")

</div>

I am new to the STAN language and would like to request help here in this space by asking about the error I am facing. In the code below the following line “x[i] = (pow(x[i], lambda[i]) - 1) / (lambda[i]);” displays the following error message: “Cannot assign to variable”.  
Thanks a lot in advance!

data {  
int\<lower=1\> N;  
vector[N] n;  
vector[N] x;  
vector[N] y;  
vector[N] p;  
vector[N] lambda;  
}

parameters {  
real beta0;  
real beta1;  
}

transformed parameters {  
for (i in 1:N){  
if (lambda[i] != 0) {  
x[i] = (pow(x[i], lambda[i]) - 1) / (lambda[i]);  
}else{  
x[i] = log(x[i]);  
}  
p = inv\_logit(beta0 + beta1 \* x);  
}  
}  
model {  
beta0 ~ normal(0, 100);  
beta1 ~ normal(0, 100);  
lambda ~ uniform(-5, 5);  
y[i] ~ binomial(n[i],p[i]);  
}

Thank you!

---

<div class="post-metadata">

**Author:** ![simonbrauer](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/simonbrauer/32/5431_2.png) [@simonbrauer](https://discourse.mc-stan.org/u/simonbrauer)\
**Post date:** [June 28, 2023, 6:19pm UTC](https://discourse.mc-stan.org/t/cannot-assign-to-variable/31902/2 "2023-06-28T18:19:24Z")

</div>

Welcome to the Stan forum.

Note that you declared `x` as data being passed to the model. I don’t think you can re-assign to data. Rather, you can create a new variable to hold your calculations. If the transformations only depend on the data, then you can (and should, for efficiency) do this in the `transformed data` block. If it depends on model parameters, then you have to do this in the `transformed parameters` or `model` blocks. See the [Stan guide’s discussion of the program blocks](https://mc-stan.org/docs/reference-manual/blocks.html) for more details. In this case, `x` and `lambda` are both data, so it can go in the `transformed data block.` But `p` depends on `beta0` and `beta1`, so needs to stay in `transformed parameters.`

Edit: fixed the model code.

```stan
data {
   int<lower=1> N;
   vector[N] n;
   vector[N] x;
   vector[N] y;
   vector[N] lambda;
}
transformed data{
   vector[N] x2;
   for (i in 1:N){
      if (lambda[i] != 0) {
         x2[i] = (pow(x[i], lambda[i]) - 1) / (lambda[i]);
      }else{
         x2[i] = log(x[i]);
      }
   }
}
parameters {
   real beta0;
   real beta1;
}
transformed parameters {
   vector[N] p = inv_logit(beta0 + beta1 * x2);
}
model {
   beta0 ~ normal(0, 100);
   beta1 ~ normal(0, 100);
   lambda ~ uniform(-5, 5);
   for (i in 1:N){
      y[i] ~ binomial(n[i],p[i]);
   } 
}

```

---

<div class="post-metadata">

**Author:** ![LuizRocha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/luizrocha/32/16599_2.png) [@LuizRocha](https://discourse.mc-stan.org/u/LuizRocha)\
**Post date:** [June 28, 2023, 9:29pm UTC](https://discourse.mc-stan.org/t/cannot-assign-to-variable/31902/3 "2023-06-28T21:29:08Z")

</div>

Thank you so much @simonbrauer for taking your time to help me. But I was presented with a new error message, when using your code:

Semantic error in ‘string’, line 24, column 13 to column 14:

Identifier ‘p’ is already in use.

Thanks a lot in advance!

---

<div class="post-metadata">

**Author:** ![LuizRocha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/luizrocha/32/16599_2.png) [@LuizRocha](https://discourse.mc-stan.org/u/LuizRocha)\
**Post date:** [June 28, 2023, 9:43pm UTC](https://discourse.mc-stan.org/t/cannot-assign-to-variable/31902/4 "2023-06-28T21:43:35Z")

</div>

@simonbrauer I removed the variable “p” from the “data” block and left it only in the “transformed parameters” block and the following error message is being displayed.

Semantic error in ‘string’, line 29, column 10 to column 11:

Identifier ‘i’ not in scope.

I verify that it is line 29 where I have:

y[i] ~ binomial(n[i],p[i]);

I believe I can’t declare the variable “i” in the model block. Not what could be done to resolve it.

Thanks a lot in advance!

---

<div class="post-metadata">

**Author:** ![simonbrauer](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/simonbrauer/32/5431_2.png) [@simonbrauer](https://discourse.mc-stan.org/u/simonbrauer)\
**Post date:** [June 28, 2023, 11:23pm UTC](https://discourse.mc-stan.org/t/cannot-assign-to-variable/31902/5 "2023-06-28T23:23:35Z")

</div>

You’re right, I missed a couple small details. I’ve fixed the code above by

1. Removing `p`from the data block
2. Adding back the for loop that I accidentally removed

---

<div class="post-metadata">

**Author:** ![LuizRocha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/luizrocha/32/16599_2.png) [@LuizRocha](https://discourse.mc-stan.org/u/LuizRocha)\
**Post date:** [June 29, 2023, 2:32am UTC](https://discourse.mc-stan.org/t/cannot-assign-to-variable/31902/6 "2023-06-29T02:32:44Z")

</div>

Made the necessary corrections, in my new code the following error message is displayed:

> model = stan\_model(“Model\_1\_A.stan”)

Error in stanc(file = file, model\_code = model\_code, model\_name = model\_name, : 0

Semantic error in ‘string’, line 43, column 5 to column 30:  
Ill-typed arguments to ‘~’ statement. No distribution ‘binomial’ was found with the correct signature.

Below is the code:

data  
{  
int\<lower=1\> N;  
vector[N] n;  
vector[N] x;  
vector[N] y;  
vector[N] lambda;  
}  
transformed data  
{  
vector[N] x2;  
for (i in 1:N){  
if (lambda[i] != 0) {  
x2[i] = (pow(x[i], lambda[i]) - 1) / (lambda[i]);  
}else{  
x2[i] = log(x[i]);  
}  
}  
}  
parameters  
{  
real beta0;  
real beta1;  
}  
transformed parameters  
{  
vector[N] p;

for (c in 1:N)  
{  
p[c] = inv\_logit(beta0 + beta1 \* x2[c] \* lambda[c]);  
}

}  
model  
{  
beta0 ~ normal(0, 100);  
beta1 ~ normal(0, 100);  
lambda ~ uniform(-5, 5);

for(j in 1:N)  
{  
y[j] ~ binomial(n[j], p);  
}  
}

---

<div class="post-metadata">

**Author:** ![LuizRocha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/luizrocha/32/16599_2.png) [@LuizRocha](https://discourse.mc-stan.org/u/LuizRocha)\
**Post date:** [June 29, 2023, 5:15am UTC](https://discourse.mc-stan.org/t/cannot-assign-to-variable/31902/7 "2023-06-29T05:15:25Z")

</div>

Hi, @simonbrauer I found the problem, in the data block the variables were incorrectly declared, in the transformed parameters block the matrix equation was incorrect and in the model block the binomial distribution variables were also incorrect.  
Thank you very much for your help, really the way you showed me was correct. Thank you so much for taking your time to help me! Below is the correct STAN code:

data  
{  
int\<lower=1\> N;  
array[N] int y;  
array[N] int n;  
array[N] real x;  
array[N] real lambda;  
}  
transformed data  
{  
vector[N] x2;  
for (i in 1:N){  
if (lambda[i] != 0) {  
x2[i] = (pow(x[i], lambda[i]) - 1) / (lambda[i]);  
}else{  
x2[i] = log(x[i]);  
}  
}  
}  
parameters  
{  
real beta0;  
real beta1;  
}  
transformed parameters  
{  
array[N] real p;  
for (c in 1:N)  
{  
p[c] = inv\_logit(beta0 + beta1 \* x2[c] \* lambda[c]);  
}  
}  
model  
{  
beta0 ~ normal(0, 100);  
beta1 ~ normal(0, 100);  
for(j in 1:N)  
{  
y[j] ~ binomial(n[j], p[j]);  
}  
}

Here the R code:

library(“rstan”)  
library(“ggplot2”)  
library(“StanHeaders”)  
options(mc.cores=4)

lambda = c(0.12,0.44,0.45,0.47,0.5,0.21,0.13,0.4,0.45,0.32,  
0.23,0.3,0.23,0.15,0.38,0.26,0.11,0.17,0.5,0.19,0.15,0.14,0.22,0.17,4.9)

```
 x = c(9.21,10.21,10.58,10.83,11.03,11.33,11.58,11.83,12.08,
       12.33,12.58,12.83,13.08,13.33,13.58,13.83,14.08,14.33,
       14.58,14.83,15.08,15.33,15.58,15.83,17.58)
 
 n = c(376,200,93,120,90,88,105,111,100,93,100,108,99,106,105,117,
       98,97,120,102,122,111,94,114,1049)
 
 y = c(0,0,0,2,2,5,10,17,16,29,39,51,47,67,81,88,79,90,113,95,117,107,92,112,1049)

```

model = stan\_model(“Model\_1\_A.stan”)

fit = sampling(model,list(N=25, n=n, y=y, x=x, lambda=lambda),iter=5000,chains=4)  
print(fit)
