# Recovering natural parameters in generated quantities block

Hi,

I’m trying to use the generated quantities block to recapture original parameters from a model I fit with standardized parameters. Everything works fine when I comment out the first two lines of the generated quantities block. It’s a simple example, but I can’t seem to figure out what I’m doing wrong.

Here’s a simplified model:
data {
// Dimensions
int<lower=0> N;

``````  // Variables
vector[N] fert;
vector[N] y;
}
transformed data {
// Define standardized variables
vector[N] fert_std;

// Standardize variables
fert_std = (fert - mean(fert)) / (2*sd(fert));
}
parameters  {
// Define standardized parameters
real alpha_std;
real beta_std;
real<lower=0> sigma_std;
}
model {
alpha_std ~ normal(0,10);
beta_std ~ normal(0,10);
sigma_std ~ cauchy(0,5);

y ~ lognormal(fert_std*beta_std + alpha_std,sigma_std);
}
generated quantities {
// Generate natural parameter
real beta_fert;
beta_fert = beta_std * sd(y) / sd(fert);

// Predict data
vector[N] y_tilde;
for (n in 1:N)
y_tilde[n] = lognormal_rng(fert_std[n]*beta_std + alpha_std,sigma_std);
}
``````

Sample data are:
\$N
19

``````\$fert
c(36.9, 45.6, 25.6, 6.7, 49.5, 2.3, 26.9, 43.2, 63.9, 35.9, 25.2, 62.0, 40.5, 36.6, 12.4, 19.8, 33.9, 5.2, 7.6)

\$y
c(3380.783, 1836.104, 2052.786, 1542.258, 2091.062, 1325.479, 1817.238, 1996.406, 2347.418, 4793.289, 1876.380, 1245.219, 3692.308, 1717.033, 2032.520, 2125.850, 2383.790, 1328.904, 6403.415)
``````

Thank you!

-Steve

In what way do things fail?

If it’s just syntax, then change the top of the generated quantities to look like this:

``````  // Generate natural parameter
real beta_fert = beta_std * sd(y) / sd(fert);

// Predict data
vector[N] y_tilde;
``````

Variables in Stan need to be declared at the top of the blocks in which they appear (so you can’t do calculations between declarations). You can assign variables at the same time you declare them, which is why it’s still okay to have the beta_std * sd(y) / sd(fert) calculation in there.

That make sense?

That did the trick. Thank you!

mc_stan+da29da278a77fefdcc1b57dc60966f1f@discoursemail.com