statsmodels/statsmodels — pull request #1927 by mbatchkarov (2014-08-22 10:08:44+00:00)
Title: Added error handling to MultiComparison

In the current head the following pieces of code
```python
import numpy as np
from statsmodels.sandbox.stats.multicomp import MultiComparison
data = range(10)
groups = [1]*10
mod = MultiComparison(np.array(data), groups, group_order=sorted(set(groups)))
a = mod.tukeyhsd(alpha=0.01)
```

raises the following

```
File "statsmodels/statsmodels/stats/libqsturng/qsturng_.py", line 470, in _func
a[2]*math.log(r-1.)**3 + \
ValueError: math domain error
```

The error is cryptic and buried deep down in `statsmodels`'s internals, but is simply due to non-sensical user input (performing multiple comparison with a single group). Similarly,

```python
data = [1]*1
groups = [1]*1
mod = MultiComparison(np.array(data), groups, group_order=sorted(set(groups)))
a = mod.tukeyhsd(alpha=0.01)
````

raises

```
File "statsmodels/statsmodels/stats/libqsturng/qsturng_.py", line 683, in _qsturng
raise ValueError('v must be > 1 when p >= .9')
ValueError: v must be > 1 when p >= .9
```

which I believe is also down to incorrect user input.

The third example actually runs, but behaves incorrectly. If `len(groups) < len(data)`, `groups` is effectively padded with zeroes (line 804, `self.groupintlab = np.zeros(len(data))`). When `len(groups) > len(data)` an `IndexError` is raised, which is fortunately fairly informative.

```python
data = [1]*10
groups = [1, 2]*4
mod = MultiComparison(np.array(data), groups, group_order=sorted(set(groups)))
a = mod.tukeyhsd(alpha=0.01)
```

I've added a two checks that fail early, raising more helpful error messages.