Search code examples
pythonpandasstatsmodelspairwisetukey

Type Error when using TukeyHSD for multi comparison


I am trying to apply TukeyHSD from statsmodels but receive the following error message.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-68-503f7cac5cbb> in <module>()
      1 from statsmodels.stats.multicomp import pairwise_tukeyhsd
      2 
----> 3 pairwise_tukeyhsd(endog=data['value'], groups=data['sample_id'])

c:\users\hambarsous\appdata\local\programs\python\python36\lib\site-packages\statsmodels\stats\multicomp.py in pairwise_tukeyhsd(endog, groups, alpha)
     36     '''
     37 
---> 38     return MultiComparison(endog, groups).tukeyhsd(alpha=alpha)

c:\users\hambarsous\appdata\local\programs\python\python36\lib\site-packages\statsmodels\sandbox\stats\multicomp.py in __init__(self, data, groups, group_order)
    794         if group_order is None:
    795             self.groupsunique, self.groupintlab = np.unique(groups,
--> 796                                                             return_inverse=True)
    797         else:
    798             #check if group_order has any names not in groups

c:\users\hambarsous\appdata\local\programs\python\python36\lib\site-packages\numpy\lib\arraysetops.py in unique(ar, return_index, return_inverse, return_counts, axis)
    221     ar = np.asanyarray(ar)
    222     if axis is None:
--> 223         return _unique1d(ar, return_index, return_inverse, return_counts)
    224     if not (-ar.ndim <= axis < ar.ndim):
    225         raise ValueError('Invalid axis kwarg specified for unique')

c:\users\hambarsous\appdata\local\programs\python\python36\lib\site-packages\numpy\lib\arraysetops.py in _unique1d(ar, return_index, return_inverse, return_counts)
    278 
    279     if optional_indices:
--> 280         perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
    281         aux = ar[perm]
    282     else:

TypeError: '<' not supported between instances of 'str' and 'int'

I've tried things along the lines of the following to see if I could get around the type error but no luck.

import pandas as pd

pd.to_numeric(data['value'], errors='coerce')

Below is the code that produces my error, my only assumption at this point is when I read the data in and melt it maybe that's doing something to the data type that I am not fully understanding. When I check the data types for value and sample_id I get float64 and object.

import pandas as pd

data = pd.read_excel('CHOK1CGA2016WB1.xlsx', sheet_name='Cleaned Data')
data = pd.melt(data, id_vars=['sample_code', 'sample_id', 'day', 'test'], value_vars=['rep 1', 'rep 2', 'rep 3', 'rep 4', 'rep 5', 'rep 6'])
data.rename(columns={'variable': 'rep'}, inplace=True)

from statsmodels.stats.multicomp import pairwise_tukeyhsd

pairwise_tukeyhsd(endog=data['value'], groups=data['sample_id'])

Solution

  • The comment to my question put me on the path of further inspecting the elements of the data['sample_id'] column. It seems this is where the problem lied, since it was a mix of number or letter/numbers I believe the number only entries were causing the error. I simply added some letters into the elements that had numbers and the code ran.