# Setting response not as a distribution but rather as an equality

**URL:** <https://discourse.mc-stan.org/t/setting-response-not-as-a-distribution-but-rather-as-an-equality/32981>\
**Category:** Modeling\
**Created:** [October 5, 2023, 12:47am UTC](https://discourse.mc-stan.org/t/setting-response-not-as-a-distribution-but-rather-as-an-equality/32981 "2023-10-05T00:47:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramiro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ramiro/32/7587_2.png) [@ramiro](https://discourse.mc-stan.org/u/ramiro)\
**Post date:** [October 5, 2023, 12:47am UTC](https://discourse.mc-stan.org/t/setting-response-not-as-a-distribution-but-rather-as-an-equality/32981/1 "2023-10-05T00:47:54Z")

</div>

This is probably very simple but I have been thinking about it too long and I don’t know where to look. Have some experience with Stan but have never come across this before

I am playing with trying to fit a simple model where I generate a set of lines \{l\_1 \ldots l\_K\}, where the slope of each line differs by a corresponding block-specific noise, \{blockNoise\_1 \ldots blockNoise\_K\}:

blockNoise\_i \sim \mathcal{N}(0,blockSD)

and

y\_{l\_j,i} = dose\_{d\_i}(1+blockNoise\_{l\_j})

where _doses_ are dose\_{1} \ldots dose\_m

I can easily generate data as:

```stan
data {
  int<lower=0> N;
  int<lower=0> nBlocks;
  array[N] real doses;
  array[N] int blocks;
}

generated quantities {
  real<lower=0> blockSD;
  vector[nBlocks] blockNoise;

  vector[N] y;

  blockSD = abs(normal_rng(0,0.1));

  for (i in 1:nBlocks) {
    blockNoise[i] = normal_rng(0,blockSD);
  }

  for (i in 1:N) {
    y[i] = doses[i]*(1+blockNoise[blocks[i]]);
  }
}

```

The question is, how do I fit this? My goal is to estimate blockSD. I am not sure how to use “y”, or maybe more generally, if the response does not exactly follow a distribution but is rather a function of elements that follow a distribution how does one use it in the program?

```stan
data {
  int<lower=0> N;
  int<lower=0> nBlocks;
  array[N] int blocks;
  array[N] real y;
  array[N] real doses;
}

parameters {
  real<lower=0> blockSD;
  vector[nBlocks] blockNoise;
}

model {

  blockSD ~ normal(0,1);
  blockNoise ~ normal(0,blockSD_b);

  for (i in 1:N) {
    y[i] = doses[i]*(1+blockNoise[blocks[i]]); // THIS DOES NOT WORK!
  }
}

```

Any help appreciated.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [October 5, 2023, 12:58am UTC](https://discourse.mc-stan.org/t/setting-response-not-as-a-distribution-but-rather-as-an-equality/32981/2 "2023-10-05T00:58:03Z")

</div>

Just compute the slopes as transformed data and then fit a normal distribution to them.

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [October 5, 2023, 6:32am UTC](https://discourse.mc-stan.org/t/setting-response-not-as-a-distribution-but-rather-as-an-equality/32981/3 "2023-10-05T06:32:43Z")

</div>

```stan
data {
  int<lower=0> N;
  int<lower=0> nBlocks;
  array[N] int blocks;
  array[N] real y;
  array[N] real doses;
}
transformed data {
  vector[nBlocks] blockNoise;
  for (i in 1:N) {
    blockNoise[block[i]] = y[i]/doses[i] - 1;
  }
}
parameters {
  real<lower=0> blockSD;
}
model {
  blockSD ~ normal(0,1);
  blockNoise ~ normal(0,blockSD);
}

```
