Skip to content

MICE issue #4393

Open
Open

Description

@BekzodR

hi, I have a dataframe, and I am trying very simplistic imputation with MICE and I keep getting an error:

RuntimeWarning: divide by zero encountered in double_scalars return np.dot(wresid, wresid) / self.df_resid

Dataset source:
Food-consumption.csv
My code is as follows:

import pandas as pd
import statsmodels.api as sm
from statsmodels.imputation.mice import MICEData

food = pd.read_csv('Data/food-consumption.csv', )
food.drop('Country', axis=1, inplace=True)
f_imp = MICEData(food)
f_imp.set_imputer('Sweetener', formula='Tea+Frozen_veggies+Frozen_fish+Tinned_fruit+Crisp_bread+Garlic', k_pmm=5)
f_imp.update_all()

IDE: Jupyternotebook
Statsmodels v. 0.80.0
Pandas v. 0.22.0
Numpy: 1.14.1

Activity

  1. kshedden commented on Mar 24, 2018

    @kshedden
    Contributor

    The dataset you are working with has more variables than observations. By default, MICE imputes missing values for each variable by regressing it against all other variables. This leads to an underdetermined least squares fit in your case.

    One way to resolve this is to provide imputation formulas for all variables with missing values (like you have already done with "Sweetener").

    We are close to merging a PR that will allow regularized regression to be used in MICE. This will allow you to do something like below:

    f_imp.set_imputer('Sweetener', fit_kwds={"alpha": 1, "L1_wt": 0}, perturbation_method="boot", k_pmm=5, regularized=True)
    f_imp.set_imputer('Biscuits', fit_kwds={"alpha": 1, "L1_wt": 0},  perturbation_method="boot", k_pmm=5, regularized=True)
    f_imp.set_imputer('Yoghurt', fit_kwds={"alpha": 1, "L1_wt": 0},  perturbation_method="boot", k_pmm=5, regularized=True)
    
  2. BekzodR commented on Mar 24, 2018

    @BekzodR
    Author

    I reduced the number of columns to 7, but now I am getting another error.
    SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

    I am passing the original dataframe.

  3. kshedden commented on Mar 24, 2018

    @kshedden
    Contributor

    That is a common Pandas warning, see e.g. the link below. I think it can be ignored here.
    https://www.dataquest.io/blog/settingwithcopywarning/

  4. BekzodR commented on Mar 24, 2018

    @BekzodR
    Author

    Thanks for the info.

  5. BekzodR commented on Mar 24, 2018

    @BekzodR
    Author

    @kshedden a question unrelated to this ticket. If I use .set_imputer for only one variable and do .update_all, is it supposed to output identical result to that of MICE(...).fit() given that formulas are identical?

  6. kshedden commented on Mar 24, 2018

    @kshedden
    Contributor

    set_imputer is called behind the scenes for every variable when MICEData is initialized. When you call set_imputer manually, it is just an opportunity to redefine the imputation scheme for one variable using non-default values. If you call set_imputer with the defaults, it should have no effect.

    self.set_imputer(vname)

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