# Transform Parameter into logit form

**URL:** <https://discourse.mc-stan.org/t/transform-parameter-into-logit-form/16304>\
**Category:** Modeling\
**Tags:** cognitive-science\
**Created:** [June 30, 2020, 9:35am UTC](https://discourse.mc-stan.org/t/transform-parameter-into-logit-form/16304 "2020-06-30T09:35:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jangoe](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@jangoe](https://discourse.mc-stan.org/u/jangoe)\
**Post date:** [June 30, 2020, 9:35am UTC](https://discourse.mc-stan.org/t/transform-parameter-into-logit-form/16304/1 "2020-06-30T09:35:42Z")

</div>

Hey there, I’m relativly new to stan and made some steps forward the last few weeks. I implemented a relativ simple cognitive model (code below). Since the parameter f should map individual differences, it is drawn from a normal distribution. However, the parameter should then be between 0 and 1, so I wanted to transform it with 1/1+exp(f) into the logit form, like this;

```
transformed parameters {
real Xsi[N] 
Xsi[N] = 1 / exp(f[N])
}

```

I tried to implement this, with different approaches, but failed. Do I have to declare a helper variable like this? Any tip is appreciated, since the Stan User guide was not that helpful

thanks

jan

* * *

Model:

```stan
data {
  int <lower=0> N; // number of subjects
  int <lower=0> K; // categories 
  int R[K]; // number of responses per category
  int count[N,K]; // observed data
  real scale_b; // set scaling for background noise
}

parameters {
  // subject parameters
  real <lower=0> a[N];
  real <lower=0> c[N];
  real <lower=0> f[N];

  // Mu & Sigma for hyper distributions
  real <lower=0> mu_a;
  real <lower=0> sig_a;
  real <lower=0> mu_c;
  real <lower=0> sig_c;
  real <lower=0> mu_f;
  real <lower=0> sig_f;
}

transformed parameters{
  // activations
  vector[K] acts[N];
  real SummedActs[N];
  

  
  // probabilities
  
  vector[K] probs[N];
  
  // loop over subjects to compute activations and probabilites
  for (i in 1:N){
    acts[i,1] = scale_b + a[i] + c[i];
    acts[i,2] = scale_b + a[i];
    acts[i,3] = scale_b + f[i]*(a[i]+c[i]);
    acts[i,4] = scale_b + f[i] *a[i];
    acts[i,5] = scale_b;
    
    SummedActs[i] = R[1] * acts[i,1] + R[2] * acts[i,2] + R[3] * acts[i,3]+ R[4] * acts[i,4]+ R[5] * acts[i,5];
    
    probs[i,1] = (R[1] * acts[i,1]) ./ (SummedActs[i]);  
    probs[i,2] = (R[2] * acts[i,2]) ./ (SummedActs[i]);
    probs[i,3] = (R[3] * acts[i,3]) ./ (SummedActs[i]);
    probs[i,4] = (R[4] * acts[i,4]) ./ (SummedActs[i]);
    probs[i,5] = (R[5] * acts[i,5]) ./ (SummedActs[i]);
    

  }

}

model{
  // priors for hyper parameters
  mu_c ~ normal(10,10);
  sig_c ~ gamma(1,0.01);
  
  mu_a ~normal(10,10);
  sig_a ~ gamma(1,0.01);
  
  mu_f ~ normal(0,10);
  sig_f ~ gamma(1,0.01);
  
  // Loop over subjects
  for(i in 1:N){
    // Draw subject parameters from truncated normal
    c[i] ~ normal(mu_c, sig_c);
    a[i] ~ normal(mu_a, sig_a);
    f[i] ~ normal(mu_f, sig_f);

    // draw data from probabilities determined by MMM parms
    count[i,] ~ multinomial(probs[i,]);
  }
}

****
```

---

<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:** [June 30, 2020, 9:58am UTC](https://discourse.mc-stan.org/t/transform-parameter-into-logit-form/16304/2 "2020-06-30T09:58:12Z")

</div>

Stan has a builtin logit function. Try this

```stan
Xsi = inv_logit(f); // Xsi = 1 / (1 + exp(-f))

```

---

<div class="post-metadata">

**Author:** ![jangoe](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@jangoe](https://discourse.mc-stan.org/u/jangoe)\
**Post date:** [July 1, 2020, 8:17am UTC](https://discourse.mc-stan.org/t/transform-parameter-into-logit-form/16304/3 "2020-07-01T08:17:41Z")

</div>

hey, thanks I think that did the thing!

jan
