tag:blogger.com,1999:blog-10008494056960241942018-03-05T11:26:53.547-05:00Python statsmodels GSOCFrank Chengnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-1000849405696024194.post-27748651338644742062014-06-16T02:04:00.001-04:002014-06-16T02:04:09.598-04:00How to test MICE?MICE is a method that imputes missing data using simulation from an inferred posterior distribution. See the Appendix of http://www.statcan.gc.ca/ads-annonces/12-001-x/5857-eng.pdf to see the exact way we simulated data.<br /><br />With this in mind, MICE is a random method that will yield differing results every time it is run. Therefore, a typical testing procedure where we try to get exact agreement between our implementation and an existing, mature package is not possible. However, we should get roughly the same results for any given instance, and more importantly, the distribution of results should be very similar. So what we will aim to do is really a comparison of simulations between our implementation and those of <a href="http://www.stefvanbuuren.nl/publications/MICE%20in%20R%20-%20Draft.pdf" target="_blank">R </a>and <a href="http://www.jstatsoft.org/v45/i04/paper" target="_blank">Stata</a>. Right now, predictive mean matching is directly comparable between all implementations, but there are some details in our other asymptotically Gaussian approach that are different in the other packages. I will write a follow up post shortly describing the different approaches taken by different packages. For now, the takeaway for testing is that we will perform simulation studies to assess the similarity between several MICE implementations.Frank Chenghttps://plus.google.com/100505246439869708475noreply@blogger.com4tag:blogger.com,1999:blog-1000849405696024194.post-18194506258995567442014-05-23T02:51:00.001-04:002014-05-25T01:43:02.984-04:00coding structure first passMe and Kerby have gotten the structure of MICE to a reasonable point; it's a good time to make and update! Here is the current user interface:<br /><br />>>> import pandas as pd (1)<br />>>> import statsmodels.api as sm (2)<br />>>> from statsmodels.sandbox.mice import mice (3)<br />>>> data = pd.read_csv('directory_here') (4)<br />>>> impdata = mice.ImputedData(data) (5)<br />>>> m1 = impdata.new_imputer("x2") (6)<br />>>> m2 = impdata.new_imputer("x3") (7)<br />>>> m3 = impdata.new_imputer("x1", model_class=sm.Logit) (8)<br />>>> impcomb = mice.MICE("x1 ~ x2 + x3", sm.Logit, [m1,m2,m3]) (9)<br />>>> p1 = impcomb.combine(iternum=20, skipnum=10) (10)<br /><br />Now here is what's going on, step by step:<br /><br />1) Our base data type is going to be a pandas DataFrame. Currently, the data must be in a form that supports pd.DataFrame(data).<br /><br />2) Import our statsmodels API.<br /><br />3) Import our mice module.<br /><br />4) Read in our data.<br /><br />5) mice.ImputedData is a class that stores both the underlying data and its missing data attributes. Right now the key attribute is which indices for each variable are missing. Later we will be making changes directly to the underlying dataset, so we don't want to lose this information as we start filling in values. As soon as these indices are saved, we modify the underlying data by filling in as an initial imputation all the column-wise means for the missing values. ImputedData also contains a helper function that allows us to fill in values and a function that allows us to construct Imputers (described below). Note that changes to the data are within the scope of ImputedData, so your actual data will be safely preserved after all the MICE carnage is done :)<br /><br />6-8) For each variable with missing values that we want to impute, we initialize an Imputer. These Imputers contain two simulation methods that will help us impute the specific variable of interest: impute_asymptotic_bayes (described in my last post) and impute_pmm (a new one, predictive mean matching). There will be more simulation methods later on. Each Imputer's job is to impute one variable given a formula and model, specified by the user. ImputedData.new_imputer defaults to OLS and all other variables as predictors. Note that here we are implicitly assuming Missing At Random since, conditional on the predictors, the missing value is completely random (and asymptotically Gaussian).<br /><br />9) Initialize a MICE instance by specifying the model and formula that we are <i>really </i>interested in. The previous imputation models are simply conditional models that we think will do a good job at predicting the variable's missing values; this new model is an <i>analysis</i> model that we want to fit to the already-imputed datasets. We pass in a list containing all the Imputers from steps 6-8; these Imputers will do the work of imputation for us.<br /><br />10) Here's where we specify an iteration number (iternum) and number of imputations to skip between iterations (skipnum). Mechanically, what is happening is that once MICE.combine is called, we start stringing imputations together, with the variable with the least number of missing observations being imputed first. ImputerChain just makes sure every Imputer in the list is used once to make one fully-imputed dataset. However, one imputation for each variable may not be enough to ensure a stable conditional distribution, so we want to skip a number of datasets between actually fitting the analysis model. So we run through all the Imputers ad nauseum until we have skipped the set number of fully imputed datasets, then fit the model. This is one iteration; we repeat until have iternum number of fitted models. To be clear: if we specify iternum=10 and skipnum=5, we will go through a total of 50 imputation iterations (one iteration is a series of imputations for all specified Imputers) and only fit the analysis model for imputation number 5, 10, 15, etc.<br /><br />All this skipping and fitting happens in AnalysisChain. MICE.combine then takes all these fitted models (actually, only the parameters we care about: params, cov_params, and scale) and combines them using Rubin's rule, and finally the combined parameters are stuffed into a fitted analysis model instance.<br /><br />Whew, that's a mouthful! That's the gist of it, if you'd like to get under the hood (and I hope people do!) the Github is <a href="https://github.com/chengfulun/statsmodels/blob/5c546ee60fcff5817188fc51d333557f4bfc40bb/statsmodels/sandbox/mice/mice.py" target="_blank">here</a>. There are some minor things I did to statsmodels.api and the working directory just so I could work from my local Github folder/machine, so don't try to install it directly before changing those back.<br /><br />Next step is making the user interface simpler (hopefully he will just pass the data object directly into MICE and not have to deal with initializing Imputers and ImputerData) and also adding logic in that lets the user specify which simulation method he wants to use for imputation. Hopefully get some feedback and make more improvements before my next post!Frank Chenghttps://plus.google.com/100505246439869708475noreply@blogger.com0tag:blogger.com,1999:blog-1000849405696024194.post-41443979532131156192014-05-16T04:10:00.002-04:002014-05-19T04:51:01.505-04:00I've made some good progress with the help of my fantastic mentor, Kerby Shedden, as well as the statsmodels guru Josef Perktold. In general terms, we have a working implementation of MICE for two parametric models that have good theoretical properties: logistic regression and OLS.<br /><br />On simulation:<br /><br />Our implementation uses a Gaussian approximation to the posterior distribution. We draw a parameter from this distribution and add noise according to the estimated quantities. The center of this Gaussian is the maximum likelihood estimator and the covariance matrix is the inverse Fisher information matrix. So, the distribution of the parameters for OLS is just approximately normal with the estimated conditional mean function and covariance, with an (optional) perturbation in the variance parameters given by a chi-square draw. Similarly, for logistic regression the draw is from a Bernoulli. These are asymptotically Bayesian because the real Bayesian posterior mean is a weighted sum of some prior and this estimated mean (as are the scale parameters for OLS), but as the sample size grows large, the prior is discounted. The approximation is most accurate when the prior distribution is uniform. Note that IVEware, a well respected MICE package in SAS, uses this methodology.<br /><br />On combining:<br /><br />The combining rules are given by Rubin's rules, which are described in detail <a href="http://sites.stat.psu.edu/~jls/mifaq.html">here</a>. This is a very intuitive set of rules that are not trivial to derive, but for our purposes we can take them for granted. The estimated parameters are just the mean of the parameters fit on each imputed dataset, and the standard errors are a weighted sum of the within-imputed-data variance and the between-imputed-data variance.<br /><br />And that's it for the actual statistics! A lot of time was spent figuring out how this can fit into the general statsmodels framework. Next step for now is going to be implementing predictive mean matching. This is important because, although it is potentially biased, it is an imputation method that is robust to choice of model. So when we get into other parametric families that are resistant to our attempts to define a posterior, PMM will become a viable backup.<br /><br />Cool graph on a bunch of imputations run on a single dataset:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-OQCOnLiY_z4/U3XHmxqBaxI/AAAAAAAAAjY/1ELN1NsCya0/s1600/missing_by_color.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-OQCOnLiY_z4/U3XHmxqBaxI/AAAAAAAAAjY/1ELN1NsCya0/s1600/missing_by_color.png" height="160" width="320" /></a></div><br />Notice that the green dots fulfill our missing at random assumption: conditional on the horizontal axis variable, the value of the vertical axis variable is random. The reverse is also true, but harder to see on this graph. More in a week or so.Frank Chenghttps://plus.google.com/100505246439869708475noreply@blogger.com0tag:blogger.com,1999:blog-1000849405696024194.post-64607055717051606602014-03-21T04:31:00.003-04:002014-03-29T03:30:22.988-04:00GSOC Blog initializedToday was the deadline for GSOC submission, and I have completed my application. The small fix I made to statsmodels was also done, it was an M-Dependent covariance structure for Generalized Estimating Equations. It can be found <a href="https://github.com/statsmodels/statsmodels/pull/1495">here</a>.Frank Chenghttps://plus.google.com/100505246439869708475noreply@blogger.com0