Posterior predictive checks and LOOCV for multinomial logistic regression

Hi all,

I am running a multinomial logit regression. Everything is running well, but when I am getting an error message when I try to obtain the replications for posterior predictive checking, as well as an error message when trying to get the LOOCV. Here is the code

data.list <- list(N=nrow(M), 

modelString <- "

data {
  int <lower=2> K;   
  int <lower=0> N;            
  int <lower=1> D;  
  int <lower=1, upper=K> ASBR07A[N];
  matrix[N, D] x;
parameters {
  matrix[D, K] beta;
transformed parameters {
  matrix[N, K] x_beta= x * beta;
model {
  to_vector(beta) ~ normal (0,2);
  for (i in 1:N) {
  ASBR07A[i] ~ categorical_logit(x_beta[i]');
 generated quantities {
  int <lower=1, upper=K> ASBR07A_rep[N];
  vector[N] log_lik;
  for (i in 1:N){
    ASBR07A_rep[i] = categorical_logit_rng(x_beta[i]');
    log_lik[i] = categorical_logit_lpmf(ASBR07A[i] |x_beta[i]');

Start estimation

nChains = 4
nIter= 5000
thinSteps = 10
burnInSteps = floor(nIter/2)

ASBR07A = data.list$ASBR07A

MultiNomRegFit = stan(data=data.list,model_code=modelString,
              chains=nChains,control = list(adapt_delta = 0.99),

and here are the error messages

> ASBR07A_rep = extract(MultiNomRegFit,"ASBR07A_rep")$ASBR07A_rep
Error in extract(MultiNomRegFit, "ASBR07A_rep") : 
 object of type 'S4' is not subsettable


 log_lik1 = extract_log_lik(MultiNomRegFit, parameter_name="log_lik", merge_chains = FALSE)
Error in extract_log_lik(MultiNomRegFit, parameter_name = "log_lik", merge_chains = FALSE) : 
  could not find function "extract_log_lik"

Thanks in advance,


Actually, I solved the problem with the posterior predictive check by writing

ASBR07A_rep = rstan::extract(MultiNomRegFit,"ASBR07A_rep")$ASBR07A_rep

However, I still can’t seem to get log_lik1. I tried rstan::extract_log_lik, but that didn’t work. I also tried various combinations by just asking for “extract” with rstan:: and also removing the parameter_name statement, to no avail. Thoughts?



Are you using extract_log_lik() from the loo package? Maybe loo::extract_log_lik()?

1 Like

That did the trick. It’s interesting because in other analyses, I didn’t need to use ::