# Transform Parameter into logit form

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:

``````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,]);
}
}

****``````

Stan has a builtin logit function. Try this

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

hey, thanks I think that did the thing!

jan