Skip to content

BUG : MICEData creates new values after imputation #5254

Closed

Description

@swab2000

This imputation model is supposed to impute values from other(pre-existing) observations, as I know.

It does, however, create a value that did never exist in other observations.

I used this imputation after applying LabelEnconder in Scikit-Learn.

In this process, NaN values were replaced with 0(if NaN exists) and after they were encoded by LabelEncoder, I again replaced 0 with NaN values. And then I did imputation as uploaded here.

Any suggestions about this problem?

Activity

  1. kshedden commented on Sep 24, 2018

    @kshedden
    Contributor
  2. swab2000 commented on Sep 25, 2018

    @swab2000
    Author

    MICE imputation code that you built in python is based on Predictive Mean Matching(PMM), as I see. The problem is PMM process only creates imputation that already exists in observed data. For example,

    X :

    x1 x2 x3
    1 0.6 A
    4 NA B
    NA 1.4 C
    2 3.8 B
    3 5.2 NA

    if the dataset has 3 NA values as I mentioned above, and for x1 case, the MICE algorithm based on PMM should suggest imputation for NA value in 1, 4, 2, 3. Likewise, for x3 case, NA must be imputed within A, B, C. However, MICEData(X) creates somewhat strange values that never exists in observed data, like 0.404994. Moreover, if there are several NA values in one column(variable), all the NA values are imputed by the same, not-existing, strange values.

    As I mentioned before, I used LabelEncoder in scikit-learn packages for categorical variables, in the example, x3(A B C B --> 1 2 3 1). I did this because MICEData does not take 'string' or 'object' type as input. I know this is a bad idea for categorical variables and also there is few reports imputing categorical variables via Multiple Imputation. Anyway, my total framework for imputation process is as follows.

    Numerical Variables -------------------------------------------------> MICE
    Categorical Variables ----------------> LabelEncoder --------------> MICE

    I am not sure if the problem takes place in LabelEncoder, but this problem happens in both numerical and categorical cases.

    I appreciate your prompt response and I hope you could capture the problem I explained. I cannot go further because of this problem.

  3. jtweeder commented on Oct 4, 2018

    @jtweeder
    Contributor

    Not an imputation expert, but in an effort to help triage, looking at the code for MICEData here is what I can tell where those strange values are coming from. If I am reading the doc string for _initial_imputation it may be working not as intended.

    Looking at _initial_imputation inside MICEData the docstring:
    image

    The way I interpret that statement is that the missing values in a given column will initially be set to the observed value in the column closest to the mean. But in reality it is setting it to the absolute distance from the mean, which in the sample case is 0.5 for x1.

    from statsmodels.imputation import mice
    import numpy as np
    import pandas as pd
    d = {'x1':[1,4,np.nan,2,3], 'x2':[0.6,np.nan, 1.4, 3.8, 5.2], 'x3':[1,2,3,4,np.nan]}
    df = pd.DataFrame(d)
    df

    image

    imp = mice.MICEData(df)
    imp.data

    image

    Where as the actual values that are closest should be something as follows:

    for x,y in np.abs(df - df.mean()).idxmin().iteritems():
        print('Observed Closest to Mean:',str(x), df[x].loc[y])

    image

    Not sure if MICEData was intended to be used in that manner of just taking the initial imputation, or if was intended to be run through multiple iterations as that would bring the values in range. Running just one MICE iteration would provide values in the range of the observed data.

    imp.update_all()
    imp.data

    image

    Again not an imputation expert so I cannot say how much effect it the initial value will have on one imputation iteration if it is indeed incorrect. My guess would be the effect depends on the actual numbers in the applicable dataset.

  4. josef-pkt commented on Oct 4, 2018

    @josef-pkt
    Member

    looks like a bug to me,
    the effect could last for many iterations if the neighborhood is not densely populated.

  5. swab2000 commented on Oct 9, 2018

    @swab2000
    Author

    I appreciate your answer @jtweeder ,and it seems you are right. After several iterations by update_all(), MICEData gives me the right imputations. Thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions