# Box cox transformation for autoregressive structure

Hello, Stan people,

I want to put an autoregressive structure on the variable u_t , which is however a positive continuous distribution (economic theory says that it should be positive as it is an inefficiency term).

However as I have time-series data, inefficiency must have an autoregressive structure (AR1). As I understand for this u_t needs to be normally distributed for the error to be normal (unless there is a way in stan to put an autoregressive structure on a positive distribution). What I have is a gamma distribution for u_t.
I have used box-cox transformation in the hope of converting u_t into normal and then estimating the autoregressive function.
But I am getting error “Chain 1: Exception: normal_lpdf: Random variable[2] is nan, but must not be nan! (in ‘model464555525d_YEARLY_IDF’ at line 43)”.

Does anybody have any idea of how I can resolve this issue or another way of putting an autoregressive structure on positive continuous distribution?

Any assistance will be greatly appreciated.

data {
int<lower=1> N;// Number of observations
int<lower=1> P; // Number of predictors in main regression
vector [N]Y; // dependent variables
int<lower=0> T; // Number of time period in main regression
matrix[N,P] X; // matrix of independent variables
int<lower=1,upper=T> TIME[N];
}

parameters {
real alpha;
vector[P] beta;
real<lower=0> sigma;
vector<lower=0>[N]u_t;
real lambda;
real delta;
real<lower=0> phi;
real<lower=0> rho;
}

transformed parameters {
vector[N] yhat;
real one_minus_rho_sq;
real u1_mean;
real u1_sigma;
one_minus_rho_sq = (1 - rho) * (1 + rho);
u1_mean = delta /(1 - rho);
u1_sigma = phi /sqrt(one_minus_rho_sq);

yhat =   alpha+X*beta - u_t;

}
model {
vector[N] u_pow;

for (n in 1:N) {
u_pow[n] = u_t[n]^lambda;
if(TIME[n] == 1){

line 43->      target +=normal_lpdf( (u_pow - 1) / lambda |u1_mean , u1_sigma) ;
}
else {
target +=normal_lpdf( (u_pow - 1) / lambda | delta+ rho* u_t[n-1], phi) ;
}
target += (lambda - 1) * sum(log(u_t));
}

delta ~ normal(0,1);
phi ~ gamma(1,1);
sigma ~ gamma(1,1);
alpha ~ normal(0,1);
beta ~normal(0,1);
rho ~ normal(0,1);

lambda ~ normal (0,1);

u_t ~ gamma(1,1);

Y ~ normal(yhat  , sigma);

}

generated quantities {
vector[N] SRTE;
SRTE = exp(-u_t);
}
1 Like

The Box-Cox transformation that ensures positivity arises in the limit \lambda \rightarrow 0, i.e. log(y), but implementing that limit requires careful analytical cancelations.

Perhaps more importantly Box-Cox transformations are designed to be applied to the observed variables and not the parameters. For example a common approach to modeling positively-constrained data is to model the log of the observations which are unconstrained,

\log(y_t) \sim \text{normal}(\alpha + X \cdot \beta - u_t, \sigma).

This is also equivalent to modeling y_t with a log normal normal.

1 Like