Skip to content

BUG: MICEData sets initial imputation incorrectly - #5301

Merged
kshedden merged 1 commit into
statsmodels:masterfrom
jtweeder:miceInitializerFix
Oct 9, 2018
Merged

kshedden merged 1 commit into
statsmodels:masterfrom
jtweeder:miceInitializerFix

Conversation

@jtweeder

@jtweeder jtweeder commented Oct 4, 2018

Copy link
Copy Markdown
Contributor

Previous code only imputed value as the distance from the mean for the closest observed value, and not actually the observed value when MICEData is initialized.

closes #5254

@jtweeder

jtweeder commented Oct 4, 2018 •

Copy link
Copy Markdown
Contributor Author

Failling this test in statsmodels.imputation.test.test_mice.py

def test_combine(self):

    np.random.seed(3897)
    x1 = np.random.normal(size=300)
    x2 = np.random.normal(size=300)
    y = x1 + x2 + np.random.normal(size=300)
    x1[0:100] = np.nan
    x2[250:] = np.nan
    df = pd.DataFrame({"x1": x1, "x2": x2, "y": y})
    idata = mice.MICEData(df)
    mi = mice.MICE("y ~ x1 + x2", sm.OLS, idata, n_skip=20)
    result = mi.fit(10, 20)

    fmi = np.asarray([ 0.1920533 ,  0.1587287 ,  0.33174032])
    assert_allclose(result.frac_miss_info, fmi, atol=1e-5)

    params = np.asarray([-0.05397474,  0.97273307,  1.01652293])
    assert_allclose(result.params, params, atol=1e-5)

    tvalues = np.asarray([ -0.84781698,  15.10491582,  13.59998039])
    assert_allclose(result.tvalues, tvalues, atol=1e-5)

I have not confirmed yet by manually calculating, but I am thinking the numbers in hard coded arrarys are based on the MICEData code bug that assigns distance from the mean, and not actual mean of a column.

@kshedden

kshedden commented Oct 5, 2018

Copy link
Copy Markdown
Contributor

@jtweeder Thanks, this looks correct to me.

The currently failing test is a regression test, so you should change the hard-coded constants to whatever you are getting with the new code.

Would it be possible to write a test that confirms that the set of unique values in each column does not change when doing mice imputation? That test would have caught this issue.

@coveralls

coveralls commented Oct 5, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 84.211% when pulling 03bbf92 on jtweeder:miceInitializerFix into b858e6e on statsmodels:master.

@jtweeder

jtweeder commented Oct 6, 2018

Copy link
Copy Markdown
Contributor Author

One Travis job is failing at the pip install flake8 command. Says there is a parse error, but I am not sure how to get it green, or if this is already known problem. All others passed this point without an issue.

@thequackdaddy

Copy link
Copy Markdown
Member

Just some experimenting around shows that testpath=0.4.1 from conda-forge is the culprit for the docbuild failure.

conda-forge/testpath-feedstock#7

Thats fixed in version 0.4.2 which is coming soon to a travis build near you:

conda-forge/testpath-feedstock#8

I'd suggest waiting a day a two and re-pushing once conda-forge is pushing the newer testpath.

@codecov

codecov Bot commented Oct 6, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #5301 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5301      +/-   ##
==========================================
+ Coverage   81.71%   81.71%   +<.01%     
==========================================
  Files         569      569              
  Lines       89710    89712       +2     
  Branches    10028    10030       +2     
==========================================
+ Hits        73306    73308       +2     
  Misses      14132    14132              
  Partials     2272     2272
Impacted Files Coverage Δ
statsmodels/imputation/mice.py 87.25% <100%> (ø) ⬆️
statsmodels/imputation/tests/test_mice.py 96.65% <100%> (+0.02%) ⬆️

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 b858e6e...03bbf92. Read the comment docs.

Previous code only imputed value as the distance from the mean for the closest observed value.
@jtweeder
jtweeder force-pushed the miceInitializerFix branch from 1a09456 to 03bbf92 Compare October 6, 2018 19:53
@kshedden

kshedden commented Oct 9, 2018

Copy link
Copy Markdown
Contributor

Thanks @jtweeder, I am merging now

@kshedden
kshedden merged commit 3a05b78 into statsmodels:master Oct 9, 2018
@jtweeder
jtweeder deleted the miceInitializerFix branch October 9, 2018 11:13
@josef-pkt josef-pkt added this to the 0.10 milestone Dec 5, 2018
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.

BUG : MICEData creates new values after imputation

5 participants