Describe the bug
az.loo fails if there is a dimension named sample in the log_likelihood data variable. It looks like the log_likelihood.stack(sample=("chain", "draw")) line is the culprit.
To Reproduce
import arviz as az
data = az.load_arviz_data("centered_eight")
data.sample_stats = data.sample_stats.rename({"school": "sample"})
az.loo(data)
Error
```
Traceback (most recent call last):
File "tmp/loo_issue.py", line 5, in
az.loo(data)
File "/Users/gibs/miniconda3/envs/birdman/lib/python3.7/site-packages/arviz/stats/stats.py", line 618, in loo
log_likelihood = log_likelihood.stack(sample=("chain", "draw"))
File "/Users/gibs/miniconda3/envs/birdman/lib/python3.7/site-packages/xarray/core/dataarray.py", line 2035, in stack
ds = self._to_temp_dataset().stack(dimensions, dimensions_kwargs)
File "/Users/gibs/miniconda3/envs/birdman/lib/python3.7/site-packages/xarray/core/dataset.py", line 3578, in stack
result = result._stack_once(dims, new_dim)
File "/Users/gibs/miniconda3/envs/birdman/lib/python3.7/site-packages/xarray/core/dataset.py", line 3524, in _stack_once
stacked_var = exp_var.stack({new_dim: dims})
File "/Users/gibs/miniconda3/envs/birdman/lib/python3.7/site-packages/xarray/core/variable.py", line 1554, in stack
result = result._stack_once(dims, new_dim)
File "/Users/gibs/miniconda3/envs/birdman/lib/python3.7/site-packages/xarray/core/variable.py", line 1507, in _stack_once
"cannot create a new dimension with the same "
ValueError: cannot create a new dimension with the same name as an existing dimension
```
Expected behavior
I expected that the computation would proceed as in the documentation page:
Computed from 2000 by 8 log-likelihood matrix
Estimate SE
elpd_loo -30.81 1.43
p_loo 0.95 -
Additional context
This was with arviz commit 5957527 installed through pip install git+git://github.com/arviz-devs/arviz.git and xarray version 0.17.0.
I realize that I can just change the name of my dimension from sample to something else to address this. However, I think it would be nice to not have to rename things, especially as the temporary dataset is not returned at all.
we could use sample__ or something less probable to be used. It is true it is not returned but it is used to indicate xarray which dimension should be summed over: https://github.com/arviz-devs/arviz/blob/main/arviz/stats/stats.py#L662
__sample__?
To fix this issue, one has to rename the sample dim created when stacking in loo and loo_pit to __sample__ to avoid name collisions.
To fix this issue, one has to rename the
sampledim created when stacking inlooandloo_pitto__sample__to avoid name collisions.
Thanks for the suggestion... I would like to work on the fix, can you assign it to me?
Most helpful comment
__sample__?