Error in gamma_lpdf: shape parameter is nan

Hi! I keep getting error messaged as I tried to fit gamma distribution in hierarchical model. My goal was to fit semi parametric survival curve to data to get time-to-event. I have length of event (info: infectious period) of individual subject(id). few subjects have censored length of events (1 for censored, 2 for observed). the structure of my model is below. the error is at gamma_lpdf(infp[i]| alphaInf,betaInf).
" Exception: gamma_lpdf: Shape parameter[2] is nan, but must be positive finite!"


data{
    int<lower=0> M; // data length
    int id[M]; 
    real infp[M];
    real cens[M];
}

parameters{
    real<lower=0> alpha; // log recovery rate
    real<lower=0> beta;
    real<lower=0> zalpha[M];
    real<lower=0> zbeta[M];
    real logsigmaA; // variation
    real logsigmaB;
}
model{
    vector[M] alphaInf;
    vector[M] betaInf;
    alpha ~ lognormal(0,5);
    beta ~ lognormal(0, 5);
    zalpha ~lognormal(0, 1);
    zbeta ~ lognormal(0, 5);
    logsigmaA ~normal(0,1);
    logsigmaB ~normal(0,1);
    for ( i in 1:M ) {
      alphaInf[i]  = alpha + zalpha[id[i]]*exp(logsigmaA) ;
      betaInf[i] = log(beta) + zbeta[id[i]]*exp(logsigmaB) ;/
      target += cens[i]*gamma_lpdf(infp[i]| alphaInf,betaInf) + (1-cens[i]) *log(1gamma_lcdf(infp[i]| alphaInf,betaInf)); 
    }
}

The data looks like this
$M
[1] 372

$id
[1] 286 205 275 289 296 296 226 233 240 261 261 282 309 333 341 349 373 317 325

$infp
[1] 24 376 360 360 8 352 352 336 352 8 336 376 352 352 360 352 328 328 336 328 328 328
$cens
[1] 2 1 1 1 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1

I tried log transform and changing prior but it did not help. Should I tried different model where censored animals and non censored it calculated independently. thank you so much in advance

Are you using cmdstan 2.30? There was a fix to the input checks in that release. Maybe that helps?

As for you model,

I’ve typically seem censoring written such that the indicator is 1 if failure, 0 if censored. Assuming you’re referring to right censoring then the log likelihood is written as

\mathcal{L}(X, \delta) = \sum_{i = 1}^{N} \delta \log(f(x_i)) + (1 - \delta)\log(1 - F(x_i))

of course where \delta \in {0, 1}.

I’d write the likelihood in Stan as (assuming cens gets recoded)

vector[M] alphaInf  = alpha + zalpha[id] * exp(logsigmaA) ;
vector[M] betaInf =  log(beta) + zbeta[id] * exp(logsigmaB) ;
 for ( i in 1:M ) {
     target += cens[i] * gamma_lpdf(infp[i] | alphaInf[i], betaInf[i]) + (1 - cens[i])  * gamma_lccdf(infp[i] | alphaInf[i], betaInf[i])); 
}

Dear spinkney
Thank you for your suggestion. I updated the cmdstan to 2.30 version and add log to Beta. I also transformed censored to 1 and 0. you are right it is rigs censored data. I am getting this error messages
gamma_lpdf: Shape parameter is inf, but must be positive finite
BUT it is running, Thank you so much!

2 Likes