Skip to content

keep index of series when recoding a series - #5548

Merged
bashtage merged 1 commit into
statsmodels:masterfrom
twoertwein:master
May 5, 2019
Merged

bashtage merged 1 commit into
statsmodels:masterfrom
twoertwein:master

Conversation

@twoertwein

Copy link
Copy Markdown

interaction_plot tries to recode a 'list' of string to 'list' of integers:

x = _recode(x, dict(zip(x_levels, x_values)))

This causes problems when the input is a pandas Series which has indices which are not range(length) (in my case the series had not contiguous indices). After recording, interaction_plot tries to combine the two independent variables and dependent variable with pandas:
data = DataFrame(dict(x=x, trace=trace, response=response))

Pandas notices it should create a DataFrame from several Series which have (through the recoding) potentially different indices, so it adds NAN for the non-overlapping values. This leads to a wrong interaction plot.

I fixed this behavior in _recode by keeping the original index of the series.

@josef-pkt

Copy link
Copy Markdown
Member

Change looks simple, i.e. it looks correct even though I don't know the details of the function.
Can you come up with a unit test for the Series behavior, or add to an existing unit test.

@coveralls

coveralls commented Mar 26, 2019 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.0008%) to 84.525% when pulling a16b49c on twoertwein:master into 0c3f6ce on statsmodels:master.

@twoertwein

Copy link
Copy Markdown
Author

@josef-pkt I can add a unit test for _recode this evening.

Here is an example showing the issue:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.graphics.factorplots import interaction_plot, _recode

# create DataFrame with non-contiguous indices
data = pd.DataFrame({
    'factor_a': (['a'] * 10) + (['b'] * 10),
    'factor_b': ['c', 'd'] * 10,
    'dependent': np.random.rand(20)},
    index=np.arange(0, 40, 2, dtype=int))

## 'visual' test
# wrong interaction plot (because new indices mismatch)
interaction_plot(data['factor_a'], data['factor_b'], data['dependent'])
plt.show()

# correct interaction plot (external fix, index matches new index of _recode)
data = data.reset_index(drop=True)
interaction_plot(data['factor_a'], data['factor_b'], data['dependent'])
plt.show()


## unit test for _recode
data.index = np.arange(0, 40, 2, dtype=int)
factor_a = _recode(data['factor_a'], {'a': 0, 'b': 1})
assert (factor_a.index == data.index).all()

@codecov

codecov Bot commented Mar 26, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #5548 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5548      +/-   ##
==========================================
+ Coverage   82.01%   82.02%   +0.01%     
==========================================
  Files         588      588              
  Lines       93026    93031       +5     
  Branches    10313    10313              
==========================================
+ Hits        76298    76312      +14     
+ Misses      14363    14357       -6     
+ Partials     2365     2362       -3
Impacted Files Coverage Δ
statsmodels/graphics/factorplots.py 88.57% <100%> (+0.16%) ⬆️
statsmodels/graphics/tests/test_factorplots.py 97.82% <100%> (+0.2%) ⬆️
statsmodels/regression/mixed_linear_model.py 90.23% <0%> (+0.77%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0c3f6ce...a16b49c. Read the comment docs.

@twoertwein

Copy link
Copy Markdown
Author

I added a test case. I'm not familiar with pytest. Please let me know whether I need to adjust the test case.

@twoertwein

Copy link
Copy Markdown
Author

@josef-pkt I think the integration tests failed for some unrelated convergence issue.

@bashtage
bashtage merged commit bc724ee into statsmodels:master May 5, 2019
@bashtage

bashtage commented May 5, 2019

Copy link
Copy Markdown
Member

LGMT. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants