<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/suppressed-data-left-censored-counts-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://freerangestats.info/blog/2018/11/06/suppressed-data"> free range statistics - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is a post about dealing with tables that have been subject to cell suppression or perturbation for confidentiality reasons. While some of the data world is only just waking up to issues of confidentiality and privacy, national statistics offices and associated researchers over some decades have developed a whole field of knowledge and tools in the area of “statistical disclosure control”. I found <a rel="nofollow" href="https://www.springer.com/us/book/9781441978011" target="_blank">Duncan, Elliot and Salazar-Gonzalez’ <em>Statistical Confidentiality: Principles and Practice</em></a> and excellent text on this subject when I had to get on top of it late in 2017, while working on Stats NZ’s Integrated Data Infrastructure (IDI) output release processes.</p>
<p>Why does one need to suppress data when it has already been aggregated and isn’t sensitive itself? Because we fear “data snoopers” (yes, that really is the correct technical term) who will combine our published data with other datasets – public or private – to reveal sensitive information. For example, if we publish a table that shows there is only one hairdresser in Smallville, that in itself isn’t sensitive; but if we combine it with a separate dataset showing the average income of hairdressers in Smallville we have given away their private information. The thing that makes statistical disclosure control so hard is that we <em>don’t know</em> what other datasets the data snooper may have available to them to use in combination with our data, however innocuous it looks on its own.</p>
<h2>Suppression in context</h2>
<p>National stats offices rarely rely alone on cell suppression. One reason is that unless analysis is very very tightly controlled, the secondary suppressions that are needed to avoid uncovering the true value of a suppressed cell quickly get out of hand. For example, in this table which I’ll be returning to later, we would have to suppress not only number of tigers and bears in region B, but also the total number of tigers and bears:</p>
<table>
<thead>
<tr>
<th>region</th>
<th>lions</th>
<th>tigers</th>
<th>bears</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>7</td>
<td>6</td>
<td>13</td>
</tr>
<tr>
<td>B</td>
<td>9</td>
<td>&lt;6</td>
<td>&lt;6</td>
</tr>
<tr>
<td>C</td>
<td>20</td>
<td>24</td>
<td>33</td>
</tr>
<tr>
<td>D</td>
<td>18</td>
<td>8</td>
<td>90</td>
</tr>
</tbody>
</table>
<p>If it were critical that we publish the total number of tigers and bears, then we would need to suppress at least one more region count for each animal. So the numbers of tigers and bears in Region A would probably also be suppressed, not because it is less than six but because revealing it would let a snooper with the marginal totals deduce the counts in Region B. With the hierarchical tables and various slices and dices that official statistics practice usually requires, this procedure soon gets pretty complicated.</p>
<p>There is software to help calculate the best secondary cell suppressions (such as the <a rel="nofollow" href="https://cran.r-project.org/package=sdcTable" target="_blank">sdcTable R package</a>), but sometimes there are practical situations in which it would be difficult to apply, and for this or other reasons we need more techniques than just suppression. One key situation is when a significant number of analysts, not under the direct control of the agency, have a degree of access to the original microdata. Two examples I’m aware of are:</p>
<ul>
<li>the <a rel="nofollow" href="http://www.abs.gov.au/websitedbs/censushome.nsf/home/tablebuilder" target="_blank">Australian Bureau of Statistics’ Census Table Builder</a> is an online tool (with free guest access) that lets you slice and dice granular census microdata, without seeing the original data but delivering aggregate tables of counts. The ABS can’t control which combinations of filters and slices will be applied, so secondary cell suppression isn’t realistic. Instead, results are reported all the way down to zero, but have been perturbed so one never knows exactly what the correct value is (other than within a small margin, of course).</li>
<li>the Stats NZ data lab allows access to de-identified microdata including the extraordinary Integrated Data Infrastructure which combines <em>inter alia</em> tax, benefits, health, education and justice data at the individual level. While the data are rigorously de-identified (researchers cannot see names, tax or other identification numbers, or addresses more granular than meshblock), a snooper with full access could probably combine results from the IDI with external datasets to identify sensitive information. Because of this, access is tightly controlled to <a rel="nofollow" href="https://www.stats.govt.nz/integrated-data/apply-to-use-microdata-for-research/" target="_blank">secure laboratories and approved researchers</a>, who are responsible for confidentialisation of their results. Results have to go through an “output checking” process to be sure appropriate confidentialisation has been applied. There is a <a rel="nofollow" href="http://archive.stats.govt.nz/tools_and_services/microdata-access/data-lab/microdata-output-guide.aspx?_ga=2.56156779.1327683982.1541452764-1061460037.1533109136" target="_blank">37 page Microdata Output Guide</a> for researchers to follow throughout this process. Many of the rules involve suppression and secondary suppression, but these are not the only tools relied on. For example, counts are perturbed by means of random rounding.</li>
</ul>
<p>Random rounding is an interesting case. Base 3 random rounding works by rounding a number to the nearest number divisible by 3 with 2/3 probability, and the second nearest with 1/3. If the number is already divisible by 3 it stays the same. So this means that of all the numbers that might end up being published as a six, “4” has a 1/3 chance of being 6; “5” has 2/3; “6” has 3/3; “7” has 2/3; and “8” has 1/3.</p>
<p>Simple application of Bayes’ formula indicates that if we started with equal chances for the real value being anything from 4 to 8, then the probabilities of a published six really meaning 4, 5, 6, 7 and 8 are 1/9, 2/9, 3/9, 2/9, 1/9 respectively. But were the starting chances equal? Not really. In fact, I’m sure the number “8” occurs in the sort of cross tabs that get cell suppression many times more than the number “4”. I don’t have proof of this, just an intuition based on the process of deciding what categories are used in the first place, which tables are thought worth publishing, etc. Obviously if half the numbers in a table were small enough to be suppressed, pretty soon we would stop bothering to publish the table and find other categories with higher cell counts.</p>
<p>If we change our priors from equal chances to something more realistic, we get quite a different picture:</p>
<p><img width="100%" src="http://freerangestats.info/img/0138-rr3-priors.svg"></p>
<p>The inference process here can be thought of as: “Before we even see the table, we know it is a table of counts. We know that most published counts of this sort are substantial numbers, at least in double figures. Then we look at this particular cell, and see that it is published as a random rounded 6. We update our prior, with all possibilities lower than 4 and greater than 8 automatically becoming zero. Then we combine our updated prior with the chances of each value being published as 6, and get a posterior distribution for the underlying number that relies on both our starting assumptions and the process of random rounding.”</p>
<p>We’ll come back to this line of thinking when we impute some numbers that are suppressed completely.</p>
<p>Here’s the code that generated the graphic above and sets us up for the rest of the session.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>knitr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>MASS</span><span>)</span><span> </span><span># for corresp
</span><span>library</span><span>(</span><span>Cairo</span><span>)</span><span>
</span><span>library</span><span>(</span><span>png</span><span>)</span><span>
</span><span>library</span><span>(</span><span>VGAM</span><span>)</span><span>
</span><span>library</span><span>(</span><span>grid</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggrepel</span><span>)</span><span>
</span><span>library</span><span>(</span><span>mice</span><span>)</span><span>
</span><span>library</span><span>(</span><span>testthat</span><span>)</span><span>
</span><span>#---------random rounding base 3-----------------
# probability of observing a six, given actual values 4, 5, 6, 7, 8
</span><span>prob</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>,</span><span>2</span><span>,</span><span>1</span><span>)</span><span> </span><span>/</span><span> </span><span>3</span><span>
</span><span>prior15</span><span> </span><span>&lt;-</span><span> </span><span>dpois</span><span>(</span><span>4</span><span>:</span><span>8</span><span>,</span><span> </span><span>15</span><span>)</span><span>
</span><span>prior30</span><span> </span><span>&lt;-</span><span> </span><span>dpois</span><span>(</span><span>4</span><span>:</span><span>8</span><span>,</span><span> </span><span>30</span><span>)</span><span>
</span><span>rr3_data</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>Value</span><span> </span><span>=</span><span> </span><span>4</span><span>:</span><span>8</span><span>,</span><span>
</span><span>`Equal probabilities`</span><span> </span><span>=</span><span> </span><span>prob</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>prob</span><span>),</span><span>
</span><span>`Poisson with mean of 15`</span><span> </span><span>=</span><span> </span><span>prob</span><span> </span><span>*</span><span> </span><span>prior15</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>prior15</span><span> </span><span>*</span><span> </span><span>prob</span><span>),</span><span>
</span><span>`Poisson with mean of 30`</span><span> </span><span>=</span><span> </span><span>prob</span><span> </span><span>*</span><span> </span><span>prior30</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>prior30</span><span> </span><span>*</span><span> </span><span>prob</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>prior</span><span>,</span><span> </span><span>post_prob</span><span>,</span><span> </span><span>-</span><span>Value</span><span>)</span><span>
</span><span># check our probabilities add up to 1
</span><span>expect_equal</span><span>(</span><span>as.vector</span><span>(</span><span>tapply</span><span>(</span><span>rr3_data</span><span>$</span><span>post_prob</span><span>,</span><span> </span><span>rr3_data</span><span>$</span><span>prior</span><span>,</span><span> </span><span>sum</span><span>)),</span><span>
</span><span>c</span><span>(</span><span>1</span><span>,</span><span>1</span><span>,</span><span>1</span><span>))</span><span>
</span><span>rr3_data</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Value</span><span>,</span><span> </span><span>weight</span><span> </span><span>=</span><span> </span><span>post_prob</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.8</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>prior</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Posterior probability this is the actual value&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;A value that has been random rounded (base 3) for statistical disclosure control&quot;</span><span>,</span><span>
</span><span>&quot;Distribution of true value when published value is 6, with three different prior expectations&quot;</span><span>)</span></code></pre>
</figure>
<h2>Simulated suppressed data</h2>
<p>Even though national stats offices rarely rely just on cell suppression, agencies that need to safely publish aggregate data but lack the resources and expertise of a stats office will often use it as their main method to provide a certain minimal level of confidentialisation. So it’s of some interest to look at the case of how to analyse a table that has had cells suppressed but no other statistical disclosure control. I am using two such datasets, from completely different sources on different projects, in my work at the moment.</p>
<p>Earlier in this post I introduced this table with two suppressed cells:</p>
<h4>Data as it is published</h4>
<table>
<thead>
<tr>
<th>region</th>
<th>lions</th>
<th>tigers</th>
<th>bears</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>7</td>
<td>6</td>
<td>13</td>
</tr>
<tr>
<td>B</td>
<td>9</td>
<td>&lt;6</td>
<td>&lt;6</td>
</tr>
<tr>
<td>C</td>
<td>20</td>
<td>24</td>
<td>33</td>
</tr>
<tr>
<td>D</td>
<td>18</td>
<td>8</td>
<td>90</td>
</tr>
</tbody>
</table>
<p>I generated this from a model with a strong relationship between animal type and region, and I also know both the <em>true observed values</em> (as though I were the statistical office managing suppression and publication) and the <em>expected values from the data generating process</em> (as though I were God).</p>
<h4>The actual data</h4>
<table>
<thead>
<tr>
<th>region</th>
<th>lions</th>
<th>tigers</th>
<th>bears</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>7</td>
<td>6</td>
<td>13</td>
</tr>
<tr>
<td>B</td>
<td>9</td>
<td>1</td>
<td>5</td>
</tr>
<tr>
<td>C</td>
<td>20</td>
<td>24</td>
<td>33</td>
</tr>
<tr>
<td>D</td>
<td>18</td>
<td>8</td>
<td>90</td>
</tr>
</tbody>
</table>
<h4>The expected values from the data generating process</h4>
<table>
<thead>
<tr>
<th>region</th>
<th>lions</th>
<th>tigers</th>
<th>bears</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>7.4</td>
<td>4.8</td>
<td>13.2</td>
</tr>
<tr>
<td>B</td>
<td>6.2</td>
<td>1.6</td>
<td>11.6</td>
</tr>
<tr>
<td>C</td>
<td>15.9</td>
<td>22.8</td>
<td>31.4</td>
</tr>
<tr>
<td>D</td>
<td>17.8</td>
<td>10.7</td>
<td>79.1</td>
</tr>
</tbody>
</table>
<p>The data was generated by means of a generalized linear model with a Poisson response (a classic and critical model to understand when dealing with tables of count data), this way:</p>
<figure>
<pre><code data-lang="r"><span>data</span><span> </span><span>&lt;-</span><span> </span><span>expand.grid</span><span>(</span><span>animals</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;lions&quot;</span><span>,</span><span> </span><span>&quot;tigers&quot;</span><span>,</span><span> </span><span>&quot;bears&quot;</span><span>),</span><span>
</span><span>region</span><span> </span><span>=</span><span> </span><span>LETTERS</span><span>[</span><span>1</span><span>:</span><span>4</span><span>],</span><span>
</span><span>count</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>animals</span><span> </span><span>=</span><span> </span><span>fct_relevel</span><span>(</span><span>animals</span><span>,</span><span> </span><span>&quot;lions&quot;</span><span>))</span><span>
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>nrow</span><span>(</span><span>data</span><span>)</span><span>
</span><span>mm</span><span> </span><span>&lt;-</span><span> </span><span>model.matrix</span><span>(</span><span>count</span><span> </span><span>~</span><span> </span><span>animals</span><span> </span><span>*</span><span> </span><span>region</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>data</span><span>)</span><span>
</span><span># for reproducibility:
</span><span>set.seed</span><span>(</span><span>123</span><span>)</span><span>
</span><span># generate true coefficients of the model:
</span><span>true_coefs</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span> </span><span>runif</span><span>(</span><span>n</span><span> </span><span>-</span><span> </span><span>1</span><span>,</span><span> </span><span>-1</span><span>,</span><span> </span><span>1</span><span>))</span><span>
</span><span># generate the actual data and make the suppressed version:
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>data</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>expected</span><span> </span><span>=</span><span> </span><span>exp</span><span>(</span><span>mm</span><span> </span><span>%*%</span><span> </span><span>true_coefs</span><span>),</span><span>
</span><span>count</span><span> </span><span>=</span><span> </span><span>rpois</span><span>(</span><span>n</span><span>,</span><span> </span><span>lambda</span><span> </span><span>=</span><span> </span><span>exp</span><span>(</span><span>mm</span><span> </span><span>%*%</span><span> </span><span>true_coefs</span><span>)),</span><span>
</span><span>censored_count</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>count</span><span> </span><span></span><span> </span><span>6</span><span>,</span><span> </span><span>&quot;&lt;6&quot;</span><span>,</span><span> </span><span>count</span><span>),</span><span>
</span><span>censored_count_num</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>censored_count</span><span>),</span><span>
</span><span>count_replaced</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>count</span><span> </span><span></span><span> </span><span>6</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>count</span><span>))</span><span>
</span><span># data as it is published:
</span><span>data</span><span> </span><span>%>%</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>animals</span><span>,</span><span> </span><span>region</span><span>,</span><span> </span><span>censored_count</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>animals</span><span>,</span><span> </span><span>censored_count</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>kable</span><span>
</span><span># actual underlying data:
</span><span>data</span><span> </span><span>%>%</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>animals</span><span>,</span><span> </span><span>region</span><span>,</span><span> </span><span>count</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>animals</span><span>,</span><span> </span><span>count</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>kable</span><span>
</span><span># actual underlying data generating process expectations:
</span><span>data</span><span> </span><span>%>%</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>animals</span><span>,</span><span> </span><span>region</span><span>,</span><span> </span><span>expected</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>expected</span><span> </span><span>=</span><span> </span><span>round</span><span>(</span><span>expected</span><span>,</span><span> </span><span>1</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>animals</span><span>,</span><span> </span><span>expected</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>kable</span></code></pre>
</figure>
<h2>Correspondence analysis and tests of independence</h2>
<p>The most common statistical test done on this sort of data is a Chi-square test, which tests the plausibility of the row and column variables being independent of each other. It’s equivalent to comparing a generalized linear model with a Poisson response that has an interaction effect between two variables (<code>count ~ animals + region + animals:region</code>) with one that doesn’t (<code>count ~ animals + region</code>). As I generated the data with the fully saturated model, I know that the correct decision is to reject the null hypothesis of “no interaction”. But what’s a general way to perform this test on a table with data that is missing, very much not at random but missing because it is below a known threshold?</p>
<p>A second technique I want to look at is correspondence analysis, coming from a more exploratory paradigm of data analysis rather than basing itself on statistical tests. Correspondence analysis works well as a graphic summary of a two-way cross tab. Here’s the biplot from correspondence analysis of the true observed data in our case:</p>
<p><img width="100%" src="http://freerangestats.info/img/0138-corresp-real.svg"></p>
<p>If you’re not familiar with this sort of plot and you deal with count data in cross tabs, I strongly recommend the technique. At a glance we see that bears are distinctively frequent to area D, tigers to C and lions B and A (although A is a more typical region than others, close to the cross in the centre of the chart). You can confirm this by looking at the original table and calculating row-wise and column-wise percentages, but it’s not such an easy snapshot as is provided by the graphic.</p>
<p>Like the Chi-square test, we need a full table of results to do this analysis.</p>
<p>With only two cells suppressed, there are only 36 possibilities for what they could be (each cell could really be any number from zero to five inclusive). So it’s quite feasible to perform analysis on 36 different possible data sets. If the results are substantively the same for all combinations, we can conclude it doesn’t matter what the suppressed cells really are. This is a nice, common sense and easily explainable approach.</p>
<p>For the correspondence analysis, the logical way to show all 36 possibilities is via an animation:</p>
<p><img src="https://i1.wp.com/freerangestats.info/img/0138-corresp.gif?w=100%25" data-recalc-dims="1"></p>
<p>For the Chi square tests the results are numeric so we can summarise them with standard methods including maximum and minimum values. Here are the summaries of the p values from the Fisher exact test for independence (<code>pvalues_f</code>) and the Chi-squared test:</p>
<div>
<pre><code>
   pvalues_f           pvalues_c        
 Min.   :0.0004998   Min.   :4.810e-11  
 1st Qu.:0.0004998   1st Qu.:6.381e-09  
 Median :0.0004998   Median :3.989e-08  
 Mean   :0.0004998   Mean   :1.480e-07  
 3rd Qu.:0.0004998   3rd Qu.:1.730e-07  
 Max.   :0.0004998   Max.   :8.438e-07 
 
</code></pre>
</div>
<p>For both the correspondence analysis and the tests of independence, the results don’t vary materially when we change the values in the suppressed cells, so we could proceed to inference with confidence.</p>
<p>Here’s the code that did those two types of analysis with a full set of possible values plugged into the suppressed cells:</p>
<figure>
<pre><code data-lang="r"><span># make a traditional cross tab matrix of the data:
</span><span>tab</span><span> </span><span>&lt;-</span><span> </span><span>data</span><span> </span><span>%>%</span><span> </span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>animals</span><span>,</span><span> </span><span>region</span><span>,</span><span> </span><span>count</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>animals</span><span>,</span><span> </span><span>count</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>select</span><span>(</span><span>lions</span><span>,</span><span> </span><span>tigers</span><span>,</span><span> </span><span>bears</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>as.matrix</span><span>()</span><span>
</span><span>rownames</span><span>(</span><span>tab</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>LETTERS</span><span>[</span><span>1</span><span>:</span><span>4</span><span>]</span><span>
</span><span># biplot with the actual full values (obviously not usually available to analysts):
</span><span>par</span><span>(</span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>col.axis</span><span> </span><span>=</span><span> </span><span>&quot;grey80&quot;</span><span>,</span><span> </span><span>fg</span><span> </span><span>=</span><span> </span><span>&quot;grey80&quot;</span><span>)</span><span>
</span><span>xl</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>-1.2</span><span>,</span><span> </span><span>0.5</span><span>)</span><span>
</span><span>yl</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>-1.1</span><span>,</span><span> </span><span>0.4</span><span>)</span><span>
</span><span>biplot</span><span>(</span><span>corresp</span><span>(</span><span>tab</span><span>,</span><span> </span><span>nf</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Full data including suppressed values\n&quot;</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;orange&quot;</span><span>,</span><span> </span><span>&quot;steelblue&quot;</span><span>),,</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>xl</span><span>,</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>yl</span><span>)</span><span>
</span><span># read in image of the table for including in the animated graphic:
</span><span>table_image</span><span> </span><span>&lt;-</span><span> </span><span>readPNG</span><span>(</span><span>&quot;0138-table-image.png&quot;</span><span>)</span><span>
</span><span># set up empty vectors to store the results of tests of independence:
</span><span>pvalues_f</span><span> </span><span>&lt;-</span><span> </span><span>numeric</span><span>()</span><span>
</span><span>pvalues_c</span><span> </span><span>&lt;-</span><span> </span><span>numeric</span><span>()</span><span>
</span><span># create a temporary folder for storing frames of the animation:
</span><span>dir.create</span><span>(</span><span>&quot;tmp&quot;</span><span>)</span><span>
</span><span>pd</span><span> </span><span>&lt;-</span><span> </span><span>setwd</span><span>(</span><span>&quot;tmp&quot;</span><span>)</span><span>
</span><span># we can use brute force and see if the results are statistically significant
# for all possible values. We'll do chi square tests and correspondence analysis
# at the same time
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>0</span><span>:</span><span>5</span><span>){</span><span>
</span><span>for</span><span>(</span><span>j</span><span> </span><span>in</span><span> </span><span>0</span><span>:</span><span>5</span><span>){</span><span>
</span><span>tab</span><span>[</span><span>2</span><span>,</span><span> </span><span>2</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>i</span><span> </span><span># tigers in B
</span><span> </span><span>tab</span><span>[</span><span>2</span><span>,</span><span> </span><span>3</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>j</span><span> </span><span># bears in B
</span><span> </span><span>pvalues_f</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>pvalues_f</span><span>,</span><span> </span><span>fisher.test</span><span>(</span><span>tab</span><span>,</span><span> </span><span>simulate.p.value</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>$</span><span>p.value</span><span>)</span><span>
</span><span>pvalues_c</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>pvalues_c</span><span>,</span><span> </span><span>chisq.test</span><span>(</span><span>tab</span><span>)</span><span>$</span><span>p.value</span><span>)</span><span>
</span><span>CairoPNG</span><span>(</span><span>paste0</span><span>(</span><span>i</span><span>,</span><span> </span><span>j</span><span>,</span><span> </span><span>&quot;.png&quot;</span><span>),</span><span> </span><span>2000</span><span>,</span><span> </span><span>2000</span><span>,</span><span> </span><span>res</span><span> </span><span>=</span><span> </span><span>300</span><span>)</span><span>
</span><span>par</span><span>(</span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>col.axis</span><span> </span><span>=</span><span> </span><span>&quot;grey80&quot;</span><span>,</span><span> </span><span>fg</span><span> </span><span>=</span><span> </span><span>&quot;grey80&quot;</span><span>,</span><span> </span><span>mar</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span>1</span><span>,</span><span>4</span><span>,</span><span>1</span><span>))</span><span>
</span><span>biplot</span><span>(</span><span>corresp</span><span>(</span><span>tab</span><span>,</span><span> </span><span>nf</span><span> </span><span>=</span><span> </span><span>2</span><span>),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;orange&quot;</span><span>,</span><span> </span><span>&quot;steelblue&quot;</span><span>),</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;Imputed values: tigers in B = &quot;</span><span>,</span><span> </span><span>i</span><span>,</span><span> </span><span>&quot;; bears in B = &quot;</span><span>,</span><span> </span><span>j</span><span>,</span><span> </span><span>&quot;.\n&quot;</span><span>),</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>xl</span><span>,</span><span> </span><span>bty</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>yl</span><span>)</span><span>
</span><span>grid.raster</span><span>(</span><span>table_image</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>0.66</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>0.3</span><span>)</span><span>
</span><span>dev.off</span><span>()</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>system</span><span>(</span><span>'magick -loop 0 -delay 40 *.png &quot;0138-corresp.gif&quot;'</span><span>)</span><span>
</span><span># move the result to where I use it for the blog
</span><span>file.rename</span><span>(</span><span>&quot;0138-corresp.gif&quot;</span><span>,</span><span> </span><span>&quot;../..http://freerangestats.info/img/0138-corresp.gif&quot;</span><span>)</span><span>
</span><span>setwd</span><span>(</span><span>pd</span><span>)</span><span>
</span><span># clean up (uncomment the last line if happy to delete the above results):
# unlink(&quot;tmp&quot;, recursive = TRUE)
</span><span>
</span><span># p-values:
</span><span>summary</span><span>(</span><span>cbind</span><span>(</span><span>pvalues_f</span><span>,</span><span> </span><span>pvalues_c</span><span>))</span></code></pre>
</figure>
<h2>Generalized linear model</h2>
<p>For a more general approach, I wanted to be ready for a situation with more than two categorical variables, and where the number of suppressed cells is too large to repeat the analysis with all combinations. As a use case, I fit a generalized linear model where my interest is in the values of the coefficients (ie the tiger effect relative to lions, bear effect, region B effect relative to region A, etc and their interaction impacts, on “count” as the response variable).</p>
<p>One thought is to treat the missing data as a parameter, constrained to be an integer from zero to five, and estimate it simultaneously with the main model in a Bayesian framework. In principle this would work, except the missing data has to be an integer (because it then forms part of a Poisson or negative binomial distribution as part of the main model) and it is difficult to estimate such discontinuous parameters. You can’t do it in Stan, for example.</p>
<p>Another way of looking at the problem is to note that this is <a rel="nofollow" href="https://en.wikipedia.org/wiki/Censoring_(statistics)" target="_blank">censored data</a> and use methods developed specifically with this in mind. “Survival analysis” has of course developed methods for dealing with all sorts of censored data; most obviously with “right-censored” data such as age at death when some of the subjects are still alive. Left-censored data occurs when measurement instruments struggle at the lower end of values, but is also an appropriate description of our suppressed values. In R, the <code>VGAM</code> package provides methods for fitting generalized linear models to data that is either left, right or interval censored. <em>Caveat (and spoiler) – my results from doing this are so bad that I suspect I was using it wrong, so read all references to this below with even more than usual caution!</em>.</p>
<p>The third family of methods I tried involved substituting values for the suppressed cells. At its simplest, I tried replacing them all with “3”. At the more complex end, I used multiple imputation to generate 20 different datasets with random values from zero to five in each suppressed cell; fit the model to each dataset; and pool the results. This is a method I use a lot with other missing data and is nicely supported by the <code>mice</code> R package. <code>mice</code> doesn’t have an imputation method (that I can see) that exactly meets my case of a number from zero to five, but it’s easy to write your own. I did this two different ways:</p>
<ul>
<li>with a uniform chance for each number</li>
<li>with probabilities proportional to the density of a Poisson distribution with mean equal to the trimmed mean of the unsuppressed counts (the reasoning being the same as that used earlier in this post in discussing random rounding)</li>
</ul>
<p>Here’s the results of all those different methods. First, in a summary of the mean squared error comparing coefficient estimates to their known true values:</p>
<p><img width="100%" src="http://freerangestats.info/img/0138-summary.svg"></p>
<p>… and second, with the coefficient-by-coefficient detail:</p>
<p><img width="100%" src="http://freerangestats.info/img/0138-coefs.svg"></p>
<p>What we see from this single simulated dataset is:</p>
<ul>
<li>the survival analysis method performed really badly. So badly that I think I must be doing it wrong somehow (I can’t see how), so treat this with caution.</li>
<li>simply substituting the number “3” in for all suppressed values worked fine; in fact as well as using the original unavailable data, or the most sophisticated multiple imputation method</li>
<li>multiple imputation with probabilities proportional to the Poisson density out-performed multiple imputation with sampling from a uniform distribution. Sampling the suppressed values from a uniform distribution gives particularly bad coefficients for the coefficients related to the suppressed cells (interaction of tigers with region B, and bears with region B).</li>
</ul>
<p>Because this is a single simulated dataset, the conclusions above should be treated as only indicative. A good next step would be to generalise this by testing it with many datasets, but that will have to wait for a later blog post as this one is already long enough! My working hypothesis when I get to that is that multiple imputation with probabilities proportional to the Poisson density will be the best method, but I’m open to being wrong on that.</p>
<p>Here’s the code for the comparison of those different methods:</p>
<figure>
<pre><code data-lang="r"><span>#====================different approaches to dealing with the missing data===================
</span><span>
</span><span>#---------------regenerate data-----------
# doing this here to make this chunk of code easier for when I want to generalise this whole approach
# to multiple simulations
</span><span>set.seed</span><span>(</span><span>123</span><span>)</span><span>
</span><span>true_coefs</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span> </span><span>runif</span><span>(</span><span>n</span><span> </span><span>-</span><span> </span><span>1</span><span>,</span><span> </span><span>-1</span><span>,</span><span> </span><span>1</span><span>))</span><span>
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>data</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>expected</span><span> </span><span>=</span><span> </span><span>exp</span><span>(</span><span>mm</span><span> </span><span>%*%</span><span> </span><span>true_coefs</span><span>),</span><span>
</span><span>count</span><span> </span><span>=</span><span> </span><span>rpois</span><span>(</span><span>n</span><span>,</span><span> </span><span>lambda</span><span> </span><span>=</span><span> </span><span>exp</span><span>(</span><span>mm</span><span> </span><span>%*%</span><span> </span><span>true_coefs</span><span>)),</span><span>
</span><span>censored_count</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>count</span><span> </span><span></span><span> </span><span>6</span><span>,</span><span> </span><span>&quot;&lt;6&quot;</span><span>,</span><span> </span><span>count</span><span>),</span><span>
</span><span>censored_count_num</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>censored_count</span><span>),</span><span>
</span><span>count_replaced</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>count</span><span> </span><span></span><span> </span><span>6</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>count</span><span>))</span><span>
</span><span>#--------------straightforward methods-----------
</span><span>mod0</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>count</span><span> </span><span>~</span><span> </span><span>animals</span><span> </span><span>*</span><span> </span><span>region</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>data</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;poisson&quot;</span><span>)</span><span>
</span><span>mod2</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>count_replaced</span><span> </span><span>~</span><span> </span><span>animals</span><span> </span><span>*</span><span> </span><span>region</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>data</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;poisson&quot;</span><span>)</span><span>
</span><span>#------------with censored poisson regression-------------------------
# (this method does so badly that I can only assume I am using it wrongly
</span><span>data</span><span>$</span><span>z</span><span> </span><span>&lt;-</span><span> </span><span>pmax</span><span>(</span><span>6</span><span>,</span><span> </span><span>data</span><span>$</span><span>count</span><span>)</span><span>
</span><span>data</span><span>$</span><span>lcensored</span><span> </span><span>&lt;-</span><span> </span><span>is.na</span><span>(</span><span>data</span><span>$</span><span>censored_count_num</span><span> </span><span>)</span><span>
</span><span>mod3</span><span> </span><span>&lt;-</span><span> </span><span>vglm</span><span>(</span><span>SurvS4</span><span>(</span><span>z</span><span>,</span><span> </span><span>lcensored</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;left&quot;</span><span>)</span><span> </span><span>~</span><span> </span><span>animals</span><span> </span><span>*</span><span> </span><span>region</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>cens.poisson</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>data</span><span>)</span><span>
</span><span>#------------------ with multiple imputation----------
</span><span>#' Imputation function for suppressed data for use with mice - Poisson-based
#'
#' @param y vector to be imputed
#' @param ry logical vector of observed (TRUE) and missing (FALSE) values of y
#' @param x design matrix. Ignored but is probably needed for mice to use.
#' @param wy vector that is TRUE where imputations are created for y. Not sure when this is different
#' to just !ry (which is the default).
</span><span>mice.impute.desuppress</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span> </span><span>(</span><span>y</span><span>,</span><span> </span><span>ry</span><span>,</span><span> </span><span>x</span><span>,</span><span> </span><span>wy</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span> </span><span>max_value</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>...</span><span>)</span><span> </span><span>{</span><span>
</span><span># during dev:
</span><span> </span><span># y &lt;-data$censored_count_num; ry &lt;-!is.na(y)
</span><span> </span><span>if</span><span> </span><span>(</span><span>is.null</span><span>(</span><span>wy</span><span>)){</span><span> </span><span>wy</span><span> </span><span>&lt;-</span><span> </span><span>!</span><span>ry</span><span>
</span><span>}</span><span>
</span><span># What are the relative chances of getting values from 0 to the maximum allowed value,
</span><span> </span><span># if we have a Poisson distribution which we have estimated the mean of via trimmed mean?
</span><span> </span><span># (this is very approximate but probably better than just giving equal chances to 0:5)
</span><span> </span><span>probs</span><span> </span><span>&lt;-</span><span> </span><span>dpois</span><span>(</span><span>0</span><span>:</span><span>max_value</span><span>,</span><span> </span><span>mean</span><span>(</span><span>y</span><span>[</span><span>ry</span><span>],</span><span> </span><span>tr</span><span> </span><span>=</span><span> </span><span>0.2</span><span>))</span><span>
</span><span>return</span><span>(</span><span>sample</span><span>(</span><span>0</span><span>:</span><span>max_value</span><span>,</span><span> </span><span>sum</span><span>(</span><span>wy</span><span>),</span><span> </span><span>prob</span><span> </span><span>=</span><span> </span><span>probs</span><span>,</span><span> </span><span>replace</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span>
</span><span>}</span><span>
</span><span>#' Imputation function for suppressed data for use with mice - simple
#'
</span><span>mice.impute.uniform</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span> </span><span>(</span><span>y</span><span>,</span><span> </span><span>ry</span><span>,</span><span> </span><span>x</span><span>,</span><span> </span><span>wy</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span> </span><span>max_value</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>...</span><span>)</span><span> </span><span>{</span><span>
</span><span>return</span><span>(</span><span>sample</span><span>(</span><span>0</span><span>:</span><span>max_value</span><span>,</span><span> </span><span>sum</span><span>(</span><span>wy</span><span>),</span><span> </span><span>replace</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span>
</span><span>}</span><span>
</span><span>m</span><span> </span><span>&lt;-</span><span> </span><span>20</span><span> </span><span># number of imputed/complete datasets to generate
</span><span>data_imp1</span><span> </span><span>&lt;-</span><span> </span><span>mice</span><span>(</span><span>data</span><span>[</span><span> </span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;censored_count_num&quot;</span><span>,</span><span> </span><span>&quot;animals&quot;</span><span>,</span><span> </span><span>&quot;region&quot;</span><span>)],</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;desuppress&quot;</span><span>,</span><span> </span><span>print</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>m</span><span> </span><span>=</span><span> </span><span>m</span><span>)</span><span>
</span><span>imp_y1</span><span> </span><span>&lt;-</span><span> </span><span>data_imp1</span><span>$</span><span>imp</span><span>$</span><span>`censored_count_num`</span><span> </span><span>imp_y1</span><span> </span><span># visual check
</span><span>
</span><span>data_imp2</span><span> </span><span>&lt;-</span><span> </span><span>mice</span><span>(</span><span>data</span><span>[</span><span> </span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;censored_count_num&quot;</span><span>,</span><span> </span><span>&quot;animals&quot;</span><span>,</span><span> </span><span>&quot;region&quot;</span><span>)],</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;uniform&quot;</span><span>,</span><span> </span><span>print</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>m</span><span> </span><span>=</span><span> </span><span>m</span><span>)</span><span>
</span><span>imp_y2</span><span> </span><span>&lt;-</span><span> </span><span>data_imp2</span><span>$</span><span>imp</span><span>$</span><span>`censored_count_num`</span><span> </span><span>imp_y2</span><span> </span><span># visual check
</span><span>
</span><span>mod_mice1</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>data_imp1</span><span>,</span><span> </span><span>glm</span><span>(</span><span>censored_count_num</span><span> </span><span>~</span><span> </span><span>animals</span><span> </span><span>*</span><span> </span><span>region</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;poisson&quot;</span><span>))</span><span>
</span><span>coef_mice1</span><span> </span><span>&lt;-</span><span> </span><span>pool</span><span>(</span><span>mod_mice1</span><span>)</span><span>$</span><span>pooled</span><span>$</span><span>estimate</span><span>
</span><span>mod_mice2</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>data_imp2</span><span>,</span><span> </span><span>glm</span><span>(</span><span>censored_count_num</span><span> </span><span>~</span><span> </span><span>animals</span><span> </span><span>*</span><span> </span><span>region</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;poisson&quot;</span><span>))</span><span>
</span><span>coef_mice2</span><span> </span><span>&lt;-</span><span> </span><span>pool</span><span>(</span><span>mod_mice2</span><span>)</span><span>$</span><span>pooled</span><span>$</span><span>estimate</span><span>
</span><span>#---------------results----------------------
# comparison data
</span><span>d</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>underlying</span><span> </span><span>=</span><span> </span><span>true_coefs</span><span>,</span><span> </span><span>`Using full data including suppressed values (not usually possible!)`</span><span> </span><span>=</span><span> </span><span>coef</span><span>(</span><span>mod0</span><span>),</span><span>
</span><span>`Replacing suppressed values with 3`</span><span> </span><span>=</span><span> </span><span>coef</span><span>(</span><span>mod2</span><span>),</span><span>
</span><span>`Left-censored survival-based method`</span><span> </span><span>=</span><span> </span><span>coef</span><span>(</span><span>mod3</span><span>),</span><span>
</span><span>`MICE with Poisson proportional probabilities`</span><span> </span><span>=</span><span> </span><span>coef_mice1</span><span>,</span><span>
</span><span>`MICE with uniform probabilities`</span><span> </span><span>=</span><span> </span><span>coef_mice2</span><span>,</span><span>
</span><span>labels</span><span> </span><span>=</span><span> </span><span>names</span><span>(</span><span>coef</span><span>(</span><span>mod0</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>labels</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;animals&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>labels</span><span>),</span><span>
</span><span>labels</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;region&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>labels</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>method</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>underlying</span><span>,</span><span> </span><span>-</span><span>labels</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>str_wrap</span><span>(</span><span>method</span><span>,</span><span> </span><span>25</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>value</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>is.na</span><span>(</span><span>value</span><span>),</span><span> </span><span>0</span><span>,</span><span> </span><span>value</span><span>))</span><span> </span><span># summary data: </span><span>d</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>d</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>square_error</span><span> </span><span>=</span><span> </span><span>(</span><span>value</span><span> </span><span>-</span><span> </span><span>underlying</span><span>)</span><span> </span><span>^</span><span> </span><span>2</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>method</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>mse</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>square_error</span><span>),</span><span>
</span><span>trmse</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>square_error</span><span>,</span><span> </span><span>tr</span><span> </span><span>=</span><span> </span><span>0.2</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>method</span><span>,</span><span> </span><span>mse</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>trmse</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>method</span><span>,</span><span> </span><span>trmse</span><span>))</span><span>
</span><span># summary graphic: </span><span>ggplot</span><span>(</span><span>d</span><span>2</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>mse</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>trmse</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>method</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text_repel</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Mean squared error of coefficient estimates&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Trimmed mean squared error of coefficient estimates&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Comparison of different methods of handling suppressed counts in a frequency table&quot;</span><span>,</span><span>
</span><span>&quot;Summary results of models fitted after different treatment methods&quot;</span><span>)</span><span>
</span><span># coefficients graphic:	</span><span>d</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>method</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>levels</span><span>(</span><span>d</span><span>2</span><span>$</span><span>method</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>underlying</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>labels</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>method</span><span>,</span><span> </span><span>nrow</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_abline</span><span>(</span><span>slope</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>intercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_text_repel</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Correct value of coefficient in underlying data generating process&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Value estimated from a GLM with one particular method of dealing with censored data&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_equal</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Comparison of different methods of handling suppressed counts in a frequency table&quot;</span><span>,</span><span>
</span><span>&quot;Coefficient estimates from models fitted after different treatment methods&quot;</span><span>)</span></code></pre>
</figure>
	<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://freerangestats.info/blog/2018/11/06/suppressed-data"> free range statistics - R</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/b3F1TLfCznM" height="1"></a></div>