Binomial model with number of trials as a variable

Thought I would share my solution. I couldn’t seem to get it to work in brms so I just wrote the stan script instead. Here’s what I came up with:


data {
  int<lower=0> N; //number of observations
  int n[N];
  vector[N] offsets; // time offset
  int Y[N];

}

parameters {
  real<lower=0> lambda;
  real<lower=0, upper = 1> prob_success;

}

transformed parameters {
}

model {
  vector[N] lambda_offset;
  lambda_offset = lambda + offsets;
  
  target += binomial_lpmf(Y| n, prob_success);
  
  target += poisson_log_lpmf(Y| lambda_offset + log(prob_success)  );
}

generated quantities {
  // actual population-level intercept
  real final_pois = lambda + log(prob_success);
}