Error in loo::relative_eff( ) - Error in n_eff_vec/S : non-numeric argument to binary operator

Hi There,

I am trying to get effective parameters for loo estimation, but encountered the following error:

log_lik_SHA ← extract_log_lik(fit_SHA, merge_chains = FALSE)
r_eff ← relative_eff(exp(log_lik_SHA))
Error in n_eff_vec/S : non-numeric argument to binary operator

I am using R version 3.6.3 (2020-02-29), loo version 2.2.0, on ubuntu system. The following is the stan code for a simple model.

Any suggestions on how to fix the above error? I used standard stan() to fit the model and trying to extract the log likelihood to feed to the loo package. I run the example “Well water in Bangladesh” on my system with no issues.

Thanks!

ytyimin


data {
  int<lower=1> n; // number of observations
  vector[n] y; // observations
  real<lower=0> mu0;
  real<lower=0> sigma02;
  real<lower=0> alpha;
  real<lower=0> beta;
}
parameters {
  real mu;              // mean
  real<lower=0> sigma2; // variance
}
model {
  target += inv_gamma_lpdf(sigma2 | alpha, beta);
  target += normal_lpdf(mu | mu0, sqrt(sigma02));
  target += normal_lpdf(y | mu, sqrt(sigma2));
}
generated quantities {
  vector[n] log_lik;
  for (i in 1:n) {
    log_lik[i] = normal_lpdf(y[i] | mu, sqrt(sigma2));
  }
}

Sorry, your question seems to have slipped through - did you manage to resolve the issue in the mentime?

I am not really familiar with loo - but maybe @jonah can weigh in?

Hi Martin,

Thanks for getting back to me. I tried various alternatives, and I found that if I reduce the sampling size, then the relative_eff() function works fine. I suspect this might be caused by overflow of too large sampling size (I used 50,000)?

Any further insights on this issue? I would think that a larger sampling size should not cause numerical instability issues.

Best regards,

Yimin

Hi @ytyimin,

Are you sure you need that much?

No it shouldn’t and thanks for reporting this. Do you have an application where you would definitely need unusually large sample size of 50000?

Can you check that all elements of exp(log_lik_SHA) are numeric with no NaNs or Infs?

Hi Aki,

Thanks for following up on this issue. The reason I would like a very large sampling size is to compute marginal likelihood via bridge sampling. I checked the data and find no NaNs nor Infs. I am attaching the data (exported as csv) for your reference. Please let me know if you can replicate the issue and find out the root cause of the issue.

Thanks,

Yimin
log_lik_SHA.csv

1 Like

I don’t have permission to see that file in your G-drive.

Hi Aki,

My apologies. Here is the link you can access the file. If you encounter any issues, please let me know. Thanks! Yimin

https://drive.google.com/file/d/1BKl-CC5p_B6YwulJTQOaQNj_mPNA155G/view?usp=sharing

Sorry for the delay, I was busy with hard deadline for the book Regression and other stories.

I downloaded the csv, but it doesn’t have the chain information so it’s not possible to use relative_eff, so it’s not the same object as log_lik_SHA mentioned in your first post. Can you save log_lik_SHA in Rdata file?

1 Like