<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/five-steps-for-missing-data-with-finalfit/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.datasurg.net/2018/08/30/five-steps-for-missing-data-with-finalfit/"> R – DataSurg</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>As a journal editor, I often receive studies in which the investigators fail to describe, analyse, or even acknowledge missing data. This is frustrating, as it is often of the utmost importance. Conclusions may (and do) change when missing data is accounted for.  A few seem to not even appreciate that in conventional regression, only rows with complete data are included.</p>
<p>These are the five steps to ensuring missing data are correctly identified and appropriately dealt with:</p>
<ol>
<li>Ensure your data are coded correctly.</li>
<li>Identify missing values within each variable.</li>
<li>Look for patterns of missingness.</li>
<li>Check for associations between missing and observed data.</li>
<li>Decide how to handle missing data.</li>
</ol>
<p><code>Finalfit</code> includes a number of functions to help with this.</p>
<h2>Some confusing terminology</h2>
<p>But first there are some terms which easy to mix up. These are important as they describe the mechanism of missingness and this determines how you can handle the missing data.</p>
<h3>Missing completely at random (MCAR)</h3>
<p>As it says, values are randomly missing from your dataset. Missing data values do not relate to any other data in the dataset and there is no pattern to the actual values of the missing data themselves.</p>
<p>For instance, when smoking status is not recorded in a random subset of patients.</p>
<p>This is easy to handle, but unfortunately, data are almost never missing completely at random.</p>
<h3>Missing at random (MAR)</h3>
<p>This is confusing and would be better stated as missing <em>conditionally</em> at random. Here, missing data <em>do</em> have a relationship with other variables in the dataset. However, the actual values that are missing are random.</p>
<p>For example, smoking status is not documented in female patients because the doctor was too shy to ask. Yes ok, not that realistic!</p>
<h3>Missing not at random (MNAR)</h3>
<p>The pattern of missingness is related to other variables in the dataset, but in addition, the values of the missing data are not random. </p>
<p>For example, when smoking status is not recorded in patients admitted as an emergency, who are also more likely to have worse outcomes from surgery.</p>
<p>Missing not at random data are important, can alter your conclusions, and are the most difficult to diagnose and handle. They can only be detected by collecting and examining some of the missing data. This is often difficult or impossible to do. </p>
<p>How you deal with missing data is dependent on the type of missingness. Once you know this, then you can sort it.</p>
<p>More on this below.</p>
<h2>1. Ensure your data are coded correctly: <code>ff_glimpse</code></h2>
<p>While clearly obvious, this step is often ignored in the rush to get results. The first step in any analysis is robust data cleaning and coding. Lots of packages have a <code>glimpse</code> function and <code>finalfit</code> is no different. This function has three specific goals:</p>
<ol>
<li>Ensure all factors and numerics are correctly assigned. That is the commonest reason to get an error with a <code>finalfit</code> function. You think you’re using a factor variable, but in fact it is incorrectly coded as a continuous numeric.</li>
<li>Ensure you know which variables have missing data. This presumes missing values are correctly assigned <code>NA</code>. See <a rel="nofollow" href="https://www.statmethods.net/input/missingdata.html" target="_blank">here</a> for more details if you are unsure.</li>
<li>Ensure factor levels and variable labels are assigned correctly.</li>
</ol>
<h3>Example scenario</h3>
<p>Using the colon cancer dataset that comes with <code>finalfit</code>, we are interested in exploring the association between a cancer obstructing the bowel and 5-year survival, accounting for other patient and disease characteristics.</p>
<p>For demonstration purposes, we will create random MCAR and MAR smoking variables to the dataset.</p>
<pre># Make sure finalfit is up-to-date 
install.packages(&quot;finalfit&quot;) 

library(finalfit) 

# Create some extra missing data
## Smoking missing completely at random
set.seed(1)
colon_s$smoking_mcar = 
  sample(c(&quot;Smoker&quot;, &quot;Non-smoker&quot;, NA), 
    dim(colon_s)[1], replace=TRUE, 
    prob = c(0.2, 0.7, 0.1)) %>% 
  factor()
Hmisc::label(colon_s$smoking_mcar) = &quot;Smoking (MCAR)&quot;

## Smoking missing conditional on patient sex
colon_s$smoking_mar[colon_s$sex.factor == &quot;Female&quot;] = 
  sample(c(&quot;Smoker&quot;, &quot;Non-smoker&quot;, NA), 
    sum(colon_s$sex.factor == &quot;Female&quot;), 
    replace = TRUE,
    prob = c(0.1, 0.5, 0.4))

colon_s$smoking_mar[colon_s$sex.factor == &quot;Male&quot;] = 
  sample(c(&quot;Smoker&quot;, &quot;Non-smoker&quot;, NA), 
    sum(colon_s$sex.factor == &quot;Male&quot;), 
    replace=TRUE, prob = c(0.15, 0.75, 0.1))
colon_s$smoking_mar = factor(colon_s$smoking_mar)
Hmisc::label(colon_s$smoking_mar) = &quot;Smoking (MAR)&quot;</pre>
<pre># Examine with ff_glimpse
explanatory = c(&quot;age&quot;, &quot;sex.factor&quot;, 
  &quot;nodes&quot;, &quot;obstruct.factor&quot;,  
  &quot;smoking_mcar&quot;, &quot;smoking_mar&quot;)
dependent = &quot;mort_5yr&quot;

colon_s %>% 
  ff_glimpse(dependent, explanatory)

Numerics
            label   n missing_n missing_percent mean sd min max range  se
age   Age (years) 929         0             0.0   60 12  18  85    67 0.4
nodes        NULL 911        18             1.9    4  4   0  33    33 0.1

Factors
                           label   n missing_n missing_percent level_n
sex.factor                   Sex 929         0               0       2
obstruct.factor      Obstruction 908        21             2.3       2
mort_5yr        Mortality 5 year 915        14             1.5       2
smoking_mcar      Smoking (MCAR) 828       101              11       2
smoking_mar        Smoking (MAR) 719       210              23       2
                                levels  levels_count   levels_percent
sex.factor            &quot;Female&quot;, &quot;Male&quot;      445, 484           48, 52
obstruct.factor            &quot;No&quot;, &quot;Yes&quot;  732, 176, 21 78.8, 18.9,  2.3
mort_5yr               &quot;Alive&quot;, &quot;Died&quot;  511, 404, 14 55.0, 43.5,  1.5
smoking_mcar    &quot;Non-smoker&quot;, &quot;Smoker&quot; 645, 183, 101       69, 20, 11
smoking_mar     &quot;Non-smoker&quot;, &quot;Smoker&quot; 591, 128, 210       64, 14, 23</pre>
<p>The function summarises a data frame or tibble by numeric (continuous) variables and factor (discrete) variables. The <code>dependent</code> and <code>explanatory</code>  are for convenience. Pass either or neither e.g. to summarise data frame or tibble:</p>
<pre>colon %>%
  ff_glimpse()</pre>
<p>It doesn’t present well if you have factors with lots of levels, so you may want to remove these.</p>
<pre>library(dplyr)
colon_s %>% 
  select(-hospital) %>% 
  ff_glimpse()</pre>
<p>Use this to check that the variables are all assigned and behaving as expected. The proportion of missing data can be seen, e.g. <code>smoking_mar</code> has 23% missing data.</p>
<h2>2. Identify missing values in each variable: <code>missing_plot</code></h2>
<p>In detecting patterns of missingness, this plot is useful. Row number is on the x-axis and all included variables are on the y-axis. Associations between missingness and observations can be easily seen, as can relationships of missingness between variables.</p>
<pre>colon_s %>%
  missing_plot()</pre>
<figure class="wp-block-image"><a rel="nofollow" href="https://i1.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_values.png" target="_blank"><img data-orig-file="https://i1.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_values.png?w=456" data-orig-size="1304,397" data-medium-file="http://www.datasurg.net/wp-content/uploads/2018/07/missing_values-300x91.png" data-attachment-id="1068" data-image-meta="{" data-image-description="" src="https://i1.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_values.png?w=456" data-permalink="http://www.datasurg.net/2018/08/30/five-steps-for-missing-data-with-finalfit/missing_values/" data-large-file="http://www.datasurg.net/wp-content/uploads/2018/07/missing_values-1024x312.png" alt="" data-image-title="missing_values" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" data-comments-opened="1" class="wp-image-1068" data-recalc-dims="1"></a></figure>
<p>Click to enlarge.</p>
<p>It was only when writing this post that I discovered the amazing package, <a rel="nofollow" href="http://naniar.njtierney.com/" target="_blank">naniar</a>. This package is recommended and provides lots of great visualisations for missing data.</p>
<h2>3. Look for patterns of missingness: <code>missing_pattern</code></h2>
<p><code>missing_pattern</code> simply wraps <code>mice::md.pattern</code> using <code>finalfit</code> grammar. This produces a table and a plot showing the pattern of missingness between variables.</p>
<pre>explanatory = c(&quot;age&quot;, &quot;sex.factor&quot;, 
  &quot;nodes&quot;, &quot;obstruct.factor&quot;,  
  &quot;smoking_mcar&quot;, &quot;smoking_mar&quot;)
dependent = &quot;mort_5yr&quot;

colon_s %>% 
  missing_pattern(dependent, explanatory)</pre>
<figure class="wp-block-image"><a rel="nofollow" href="https://i0.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pattern.png" target="_blank"><img data-orig-file="https://i0.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pattern.png?w=456" data-orig-size="560,992" data-medium-file="http://www.datasurg.net/wp-content/uploads/2018/07/missing_pattern-169x300.png" data-attachment-id="1069" data-image-meta="{" data-image-description="" src="https://i0.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pattern.png?w=456" data-permalink="http://www.datasurg.net/2018/08/30/five-steps-for-missing-data-with-finalfit/missing_pattern/" data-large-file="https://i0.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pattern.png?w=456" alt="" data-image-title="missing_pattern" sizes="(max-width: 560px) 100vw, 560px" data-comments-opened="1" class="wp-image-1069" data-recalc-dims="1"></a></figure>
<p>This allows us to look for patterns of missingness <em>between</em> variables. There are 14 patterns in this data. The number and pattern of missingness help us to determine the likelihood of it being random rather than systematic.  </p>
<h3>Make sure you include missing data in demographics tables</h3>
<p>Table 1 in a healthcare study is often a demographics table of an “explanatory variable of interest” against other explanatory variables/confounders. Do not silently drop missing values in this table. It is easy to do this correctly with <code>summary_factorlist</code>. This function provides a useful summary of a dependent variable against explanatory variables. Despite its name, continuous variables are handled nicely.</p>
<p><code>na_include=TRUE</code> ensures missing data from the explanatory variables (but not dependent) are included. Note that any p-values are generated across missing groups as well, so run a second time with <code>na_include=FALSE</code> if you wish a hypothesis test only over observed data.</p>
<pre>library(finalfit)

# Explanatory or confounding variables
explanatory = c(&quot;age&quot;, &quot;sex.factor&quot;, 
  &quot;nodes&quot;,  
  &quot;smoking_mcar&quot;, &quot;smoking_mar&quot;)

# Explanatory variable of interest
dependent = &quot;obstruct.factor&quot; # Bowel obstruction

colon_s %>% 
  summary_factorlist(dependent, explanatory, 
  na_include=TRUE, p=TRUE)
  
          label     levels          No         Yes     p
    Age (years)  Mean (SD) 60.2 (11.5) 57.3 (13.3) 0.014
            Sex     Female  346 (79.2)   91 (20.8) 0.290
                      Male  386 (82.0)   85 (18.0)      
          nodes  Mean (SD)   3.7 (3.7)   3.5 (3.2) 0.774
 Smoking (MCAR) Non-smoker  500 (79.4)  130 (20.6) 0.173
                    Smoker  154 (85.6)   26 (14.4)      
                   Missing   78 (79.6)   20 (20.4)      
  Smoking (MAR) Non-smoker  467 (80.9)  110 (19.1) 0.056
                    Smoker   91 (73.4)   33 (26.6)      
                   Missing  174 (84.1)   33 (15.9)</pre>
<h2>4. Check for associations between missing and observed data: <code>missing_pairs | </code><code>missing_compare</code></h2>
<p>In deciding whether data is MCAR or MAR, one approach is to explore patterns of missingness between levels of included variables. This is particularly important (I would say absolutely required) for a primary outcome measure / dependent variable. </p>
<p>Take for example “death”. When that outcome is missing it is often for a particular reason. For example, perhaps patients undergoing emergency surgery were less likely to have complete records compared with those undergoing planned surgery. And of course, death is more likely after emergency surgery.</p>
<p><code>missing_pairs</code> uses functions from the excellent <a rel="nofollow" href="http://ggobi.github.io/ggally/" target="_blank">GGally</a> package. It produces pairs plots to show relationships between missing values and observed values in all variables.</p>
<pre>explanatory = c(&quot;age&quot;, &quot;sex.factor&quot;, 
  &quot;nodes&quot;, &quot;obstruct.factor&quot;,  
  &quot;smoking_mcar&quot;, &quot;smoking_mar&quot;)
dependent = &quot;mort_5yr&quot;
colon_s %>% 
  missing_pairs(dependent, explanatory)</pre>
<figure class="wp-block-image"><a rel="nofollow" href="https://i1.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pairs1.png" target="_blank"><img data-orig-file="https://i1.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pairs1.png?w=456" data-orig-size="1181,894" data-medium-file="http://www.datasurg.net/wp-content/uploads/2018/07/missing_pairs1-300x227.png" data-attachment-id="1071" data-image-meta="{" data-image-description="" src="https://i1.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pairs1.png?w=456" data-permalink="http://www.datasurg.net/2018/08/30/five-steps-for-missing-data-with-finalfit/missing_pairs1/" data-large-file="http://www.datasurg.net/wp-content/uploads/2018/07/missing_pairs1-1024x775.png" alt="" data-image-title="missing_pairs1" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" data-comments-opened="1" class="wp-image-1071" data-recalc-dims="1"></a></figure>
<p>For continuous variables (age and nodes), the distributions of observed and missing data can be visually compared. Is there a difference between age and mortality above?</p>
<p>For discrete, data, counts are presented by default. It is often easier to compare proportions: </p>
<pre>colon_s %>% 
  missing_pairs(dependent, explanatory, position = &quot;fill&quot;, )</pre>
<figure class="wp-block-image"><a rel="nofollow" href="https://i0.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pairs2-1.png" target="_blank"><img data-orig-file="https://i0.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pairs2-1.png?w=456" data-orig-size="1181,845" data-medium-file="http://www.datasurg.net/wp-content/uploads/2018/07/missing_pairs2-1-300x215.png" data-attachment-id="1073" data-image-meta="{" data-image-description="" src="https://i0.wp.com/www.datasurg.net/wp-content/uploads/2018/07/missing_pairs2-1.png?w=456" data-permalink="http://www.datasurg.net/2018/08/30/five-steps-for-missing-data-with-finalfit/missing_pairs2-2/" data-large-file="http://www.datasurg.net/wp-content/uploads/2018/07/missing_pairs2-1-1024x733.png" alt="" data-image-title="missing_pairs2" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" data-comments-opened="1" class="wp-image-1073" data-recalc-dims="1"></a></figure>
<p>It should be obvious that missingness in <code>Smoking (MCAR)</code> does not relate to <code>sex</code> (row 6, column 3). But missingness  in <code>Smoking (MAR)</code> does differ by <code>sex</code> (last row, column 3) as was designed above when the missing data were created.</p>
<p>We can confirm this using <code>missing_compare</code>. </p>
<pre>explanatory = c(&quot;age&quot;, &quot;sex.factor&quot;, 
  &quot;nodes&quot;, &quot;obstruct.factor&quot;)
dependent = &quot;smoking_mcar&quot;
colon_s %>% 
  missing_compare(dependent, explanatory)

 Missing data analysis: Smoking (MCAR)           Not missing     Missing     p
                           Age (years) Mean (SD) 59.7 (11.9) 59.9 (12.6) 0.867
                                   Sex    Female  399 (89.7)   46 (10.3) 0.616
                                            Male  429 (88.6)   55 (11.4)      
                                 nodes Mean (SD)   3.6 (3.4)     4 (4.5) 0.990
                           Obstruction        No  654 (89.3)   78 (10.7) 0.786
                                             Yes  156 (88.6)   20 (11.4)     
											 
dependent = &quot;smoking_mar&quot;
colon_s %>% 
  missing_compare(dependent, explanatory)

 Missing data analysis: Smoking (MAR)           Not missing    Missing      p
                          Age (years) Mean (SD) 59.6 (11.9)  60.1 (12)  0.709
                                  Sex    Female  288 (64.7) 157 (35.3) &lt;0.001
                                           Male  431 (89.0)  53 (11.0)       
                                nodes Mean (SD)   3.6 (3.6)  3.8 (3.6)  0.730
                          Obstruction        No  558 (76.2) 174 (23.8)  0.154
                                            Yes  143 (81.2)  33 (18.8)</pre>
<p>It takes “dependent” and “explanatory” variables, but in this context “dependent” just refers to the variable being tested for missingness against the “explanatory” variables.</p>
<p>Comparisons for continuous data use a Kruskal Wallis and for discrete data a chi-squared test.</p>
<p>As expected, a relationship is seen between <code>Sex</code> and <code>Smoking (MAR)</code> but not <code>Smoking (MCAR)</code>. </p>
<h3>For those who like an omnibus test</h3>
<p>If you are work predominately with numeric rather than discrete data (categorical/factors), you may find these tests from the <code>MissMech</code> package useful. The package and output is well <a rel="nofollow" href="https://www.jstatsoft.org/article/view/v056i06" target="_blank">documented</a>, and provides two tests which can be used to determine whether data are MCAR.</p>
<pre>library(finalfit)
library(dplyr)
library(MissMech)
explanatory = c(&quot;age&quot;, &quot;nodes&quot;)
dependent = &quot;mort_5yr&quot; 

colon_s %>% 
  select(explanatory) %>% 
  MissMech::TestMCARNormality()</pre>
<h2>5. Decide how to handle missing data<br></h2>
<p><a rel="nofollow" href="http://www.theanalysisfactor.com/missing-data-mechanism/" target="_blank">These pages</a> from Karen Grace-Martin are great for this.</p>
<p>Prior to a standard regression analysis, we can either:</p>
<ul>
<li>Delete the variable with the missing data</li>
<li>Delete the cases with the missing data</li>
<li>Impute (fill in) the missing data</li>
<li>Model the missing data</li>
</ul>
<h3>MCAR, MAR, or MNAR</h3>
<h4><span>MCAR</span> vs MAR</h4>
<p>Using the examples, we identify that <code>Smoking (MCAR)</code> is missing completely at random.  </p>
<p>We know nothing about the missing values themselves, but we know of no plausible reason that the values of the missing data, for say, people who died should be different to the values of the missing data for those who survived. The pattern of missingness is therefore not felt to be MNAR.</p>
<p><strong>Common solution</strong></p>
<p>Depending on the number of data points that are missing, we may have sufficient power with complete cases to examine the relationships of interest.</p>
<p>We therefore elect to simply omit the patients in whom smoking is missing. This is known as list-wise deletion and will be performed by default in standard regression analyses including <code>finalfit</code>. </p>
<pre>explanatory = c(&quot;age&quot;, &quot;sex.factor&quot;, 
  &quot;nodes&quot;, &quot;obstruct.factor&quot;,  
  &quot;smoking_mcar&quot;)
dependent = &quot;mort_5yr&quot;
colon_s %>% 
	finalfit(dependent, explanatory, metrics=TRUE)

 Dependent: Mortality 5 year                  Alive        Died          OR (univariable)        OR (multivariable)
                 Age (years)  Mean (SD) 59.8 (11.4) 59.9 (12.5) 1.00 (0.99-1.01, p=0.986) 1.01 (1.00-1.02, p=0.200)
                         Sex     Female  243 (47.6)  194 (48.0)                         -                         -
                                   Male  268 (52.4)  210 (52.0) 0.98 (0.76-1.27, p=0.889) 1.02 (0.76-1.38, p=0.872)
                       nodes  Mean (SD)   2.7 (2.4)   4.9 (4.4) 1.24 (1.18-1.30, p&lt;0.001) 1.25 (1.18-1.33, p&lt;0.001)
                 Obstruction         No  408 (82.1)  312 (78.6)                         -                         -
                                    Yes   89 (17.9)   85 (21.4) 1.25 (0.90-1.74, p=0.189) 1.53 (1.05-2.22, p=0.027)
              Smoking (MCAR) Non-smoker  358 (79.9)  277 (75.3)                         -                         -
                                 Smoker   90 (20.1)   91 (24.7) 1.31 (0.94-1.82, p=0.113) 1.37 (0.96-1.96, p=0.083)
								 
&quot;Number in dataframe = 929, Number in model = 782, Missing = 147, AIC = 1003.3, C-statistic = 0.687, H&amp;L = Chi-sq(8) 15.03 (p=0.058)</pre>
<p><strong>Other considerations</strong></p>
<ol>
<li>Sensitivity analysis</li>
<li>Omit the variable</li>
<li>Imputation</li>
<li>Model the missing data</li>
</ol>
<p>If the variable in question is thought to be particularly important, you may wish to perform a sensitivity analysis. A sensitivity analysis in this context aims to capture the effect of uncertainty on the conclusions drawn from the model. Thus, you may choose to re-label all missing smoking values as “smoker”, and see if that changes the conclusions of your analysis. The same procedure can be performed labeling with “non-smoker”.</p>
<p>If smoking is not associated with the explanatory variable of interest (bowel obstruction) or the outcome, it may be considered not to be a confounder  and so could be omitted. That neatly deals with the missing data issue, but of course may not be appropriate.</p>
<p>Imputation and modelling are considered below.</p>
<h4>MCAR vs <span>MAR</span></h4>
<p>But life is rarely that simple. </p>
<p>Consider that the smoking variable is more likely to be missing if the patient is female (<code>missing_compare</code>shows a relationship). But, say, that the missing values are not different from the observed values. Missingness is then MAR. </p>
<p>If we simply drop all the cases (patients) in which smoking is missing (list-wise deletion), then proportionality we drop more females than men. This may have consequences for our conclusions if sex is associated with our explanatory variable of interest or outcome. </p>
<p><strong>Common solution</strong></p>
<p><code>mice</code> is our go to package for multiple imputation. That’s the process of filling in missing data using a best-estimate from all the other data that exists. When first encountered, this doesn’t sounds like a good idea. </p>
<p>However, taking our simple example, if missingness in smoking is predicted strongly by sex, and the values of the missing data are random, then we can impute (best-guess) the missing smoking values using sex and other variables in the dataset.</p>
<p>Imputation is not usually appropriate for the explanatory variable of interest or the outcome variable. With both of these, the hypothesis is that there is an meaningful association with other variables in the dataset, therefore it doesn’t make sense to use these variables to impute them.</p>
<p>Here is some code to run <code>mice</code>. The package is well documented, and there are a number of checks and considerations that should be made to inform the imputation process. Read the documentation carefully prior to doing this yourself. </p>
<pre># Multivariate Imputation by Chained Equations (mice)
library(finalfit)
library(dplyr)
library(mice)
explanatory = c(&quot;age&quot;, &quot;sex.factor&quot;, 
  &quot;nodes&quot;, &quot;obstruct.factor&quot;, &quot;smoking_mar&quot;)
dependent = &quot;mort_5yr&quot;

colon_s %>% 
  select(dependent, explanatory) %>% 
  # Exclude outcome and explanatory variable of interest from imputation
  dplyr::filter(!is.na(mort_5yr), !is.na(obstruct.factor)) %>%
  # Run imputation with 10 imputed sets
  mice(m = 10) %>% 
  # Run logistic regression on each imputed set
  with(glm(formula(ff_formula(dependent, explanatory)), 
    family=&quot;binomial&quot;)) %>%
  # Pool and summarise results
  pool() %>%                                            
  summary(conf.int = TRUE, exponentiate = TRUE) %>%
  # Jiggle into finalfit format
  mutate(explanatory_name = rownames(.)) %>%            
  select(explanatory_name, estimate, `2.5 %`, `97.5 %`, p.value) %>% 
  condense_fit(estimate_suffix = &quot; (multiple imputation)&quot;) %>% 
  remove_intercept() -> fit_imputed

# Use finalfit merge methods to create and compare results
colon_s %>% 
  summary_factorlist(dependent, explanatory, fit_id = TRUE) -> summary1

colon_s %>% 
  glmuni(dependent, explanatory) %>% 
  fit2df(estimate_suffix = &quot; (univariable)&quot;) -> fit_uni

colon_s %>% 
  glmmulti(dependent, explanatory) %>% 
  fit2df(estimate_suffix = &quot; (multivariable inc. smoking)&quot;) -> fit_multi

explanatory = c(&quot;age&quot;, &quot;sex.factor&quot;, 
  &quot;nodes&quot;, &quot;obstruct.factor&quot;)
colon_s %>% 
  glmmulti(dependent, explanatory) %>% 
  fit2df(estimate_suffix = &quot; (multivariable)&quot;) -> fit_multi_r

# Combine to final table
summary1 %>% 
  ff_merge(fit_uni) %>% 
  ff_merge(fit_multi_r) %>% 
  ff_merge(fit_multi) %>% 
  ff_merge(fit_imputed) %>% 
  select(-fit_id, -index)

         label     levels       Alive        Died          OR (univariable)        OR (multivariable) OR (multivariable inc. smoking)  OR (multiple imputation)
   Age (years)  Mean (SD) 59.8 (11.4) 59.9 (12.5) 1.00 (0.99-1.01, p=0.986) 1.01 (1.00-1.02, p=0.122)       1.02 (1.00-1.03, p=0.010) 1.01 (1.00-1.02, p=0.116)
           Sex     Female  243 (55.6)  194 (44.4)                         -                         -                               -                         -
                     Male  268 (56.1)  210 (43.9) 0.98 (0.76-1.27, p=0.889) 0.98 (0.74-1.30, p=0.890)       0.88 (0.64-1.23, p=0.461) 0.99 (0.75-1.31, p=0.957)
         nodes  Mean (SD)   2.7 (2.4)   4.9 (4.4) 1.24 (1.18-1.30, p&lt;0.001) 1.25 (1.19-1.32, p&lt;0.001)       1.25 (1.18-1.33, p&lt;0.001) 1.25 (1.19-1.32, p&lt;0.001)
   Obstruction         No  408 (56.7)  312 (43.3)                         -                         -                               -                         -
                      Yes   89 (51.1)   85 (48.9) 1.25 (0.90-1.74, p=0.189) 1.36 (0.95-1.93, p=0.089)       1.26 (0.85-1.88, p=0.252) 1.36 (0.95-1.93, p=0.089)
 Smoking (MAR) Non-smoker  328 (56.4)  254 (43.6)                         -                         -                               -                         -
                   Smoker   68 (53.5)   59 (46.5) 1.12 (0.76-1.65, p=0.563)                         -       1.25 (0.82-1.89, p=0.300) 1.26 (0.82-1.94, p=0.289)</pre>
<p>The final table can easily be exported to Word or as a PDF as described <a rel="nofollow" href="http://www.datasurg.net/2018/05/22/finalfit-knitr-and-r-markdown-for-quick-results/" target="_blank">else where</a>.</p>
<p>By examining the coefficients, the effect of the imputation compared with the complete case analysis can be clearly seen. </p>
<p><strong>Other considerations</strong></p>
<ol>
<li>Omit the variable</li>
<li>Imputing factors with new level for missing data</li>
<li>Model the missing data</li>
</ol>
<p>As above, if the variable does not appear to be important, it may be omitted from the analysis. A sensitivity analysis in this context is another form of imputation. But rather than using all other available information to best-guess the missing data, we simply assign the value as above. Imputation is therefore likely to be more appropriate. </p>
<p>There is an alternative method to model the missing data for the categorical in this setting – just consider the missing data as a factor level. This has the advantage of simplicity, with the disadvantage of increasing the number of terms in the model. Multiple imputation is generally preferred.  </p>
<pre>library(dplyr)
colon_s %>% 
  mutate(
    smoking_mar = forcats::fct_explicit_na(smoking_mar)
  ) %>% 
  finalfit(dependent, explanatory)

 Dependent: Mortality 5 year                  Alive        Died          OR (univariable)        OR (multivariable)
                 Age (years)  Mean (SD) 59.8 (11.4) 59.9 (12.5) 1.00 (0.99-1.01, p=0.986) 1.01 (1.00-1.02, p=0.119)
                         Sex     Female  243 (47.6)  194 (48.0)                         -                         -
                                   Male  268 (52.4)  210 (52.0) 0.98 (0.76-1.27, p=0.889) 0.96 (0.72-1.30, p=0.809)
                       nodes  Mean (SD)   2.7 (2.4)   4.9 (4.4) 1.24 (1.18-1.30, p&lt;0.001) 1.25 (1.19-1.32, p&lt;0.001)
                 Obstruction         No  408 (82.1)  312 (78.6)                         -                         -
                                    Yes   89 (17.9)   85 (21.4) 1.25 (0.90-1.74, p=0.189) 1.34 (0.94-1.91, p=0.102)
               Smoking (MAR) Non-smoker  328 (64.2)  254 (62.9)                         -                         -
                                 Smoker   68 (13.3)   59 (14.6) 1.12 (0.76-1.65, p=0.563) 1.24 (0.82-1.88, p=0.308)
                              (Missing)  115 (22.5)   91 (22.5) 1.02 (0.74-1.41, p=0.895) 0.99 (0.69-1.41, p=0.943)</pre>
<h4><span>MNAR</span> vs MAR</h4>
<p>Missing not at random data is tough in healthcare. To determine if data are MNAR for definite, we need to know their value in a subset of observations (patients).</p>
<p>Using our example above. Say smoking status is poorly recorded in patients admitted to hospital as an emergency with an obstructing cancer. Obstructing bowel cancers may be larger or their position may make the prognosis worse. Smoking may relate to the aggressiveness of the cancer and may be an independent predictor of prognosis. The missing values for smoking may therefore not random. Smoking may be more common in the emergency patients and may be more common in those that die. </p>
<p>There is no easy way to handle this. If at all possible, try to get the missing data. Otherwise, take care when drawing conclusions from analyses where data are thought to be missing not at random. </p>
<h2>Where to next</h2>
<p>We are now doing more in <a rel="nofollow" href="http://mc-stan.org/users/interfaces/rstan" target="_blank">Stan</a>. Missing data can be imputed directly within a Stan model which feels neat. Stan doesn’t yet have the equivalent of <code>NA</code> which makes passing the data block into Stan a bit of a faff. </p>
<p>Alternatively, the missing data can be directly modelled in Stan. Examples are provided in the <a rel="nofollow" href="http://mc-stan.org/users/documentation/" target="_blank">manual</a>. Again, I haven’t found this that easy to do, but there are a number of Stan developments that will hopefully make this more straightforward in the future. </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.datasurg.net/2018/08/30/five-steps-for-missing-data-with-finalfit/"> R – DataSurg</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/GjXzq1bW6S8" height="1"></a></div>