<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/estimating-relative-risk-in-a-simulated-complex-survey-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/08/24/simulating-survey"> free range statistics - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post I simulate a population and a complex survey of it. The survey has stratification, two-stage sampling and post-collection calibration to marginal population totals. The original idea was to follow up on <a rel="nofollow" href="http://freerangestats.info/blog/2018/08/17/risk-ratios" target="_blank">last week's post</a> on relative risk ratios and odds ratios and in particular to explore the use of the <code>quasipoisson(log)</code> family compared to the <code>quasibinomial(log)</code> family when modelling a binary outcome with categorical data.</p>
<p>In the end, the more interesting thing (for me) was the general challenge of simulating a population and complex survey process and doing some basic comparison of the success of different estimation strategies. In particular, I was interested in how different the results are if we treat the sample with appropriate complex survey methods (ie Thomas Lumley's <code>survey</code> R package) in contrast to naively fitting the same model while ignoring the cluster sampling, stratification, post-stratification, etc.</p>
<p>To cut to the chase, this chart compares those two approaches fit to 100 different samples of 2,100 individuals:</p>
<p><img width="100%" src="http://freerangestats.io/img/0132-comparisons.svg"></p>
<p>What we see is that there isn't much to choose between them when it comes to point estimates. The mean squared errors of the point estimates of effect sizes from both methods, compared to fitting the model to the full population of 1 million people, are similar. However, the complex survey methods do better in terms of confidence intervals. The 95% confidence intervals delivered by survey methods contained the true values 94% of the time, nearly as good as advertised; whereas the naive method's confidence intervals contained the true values only 89% of the time.</p>
<h2>Simulating data</h2>
<p>I wanted to simulate data that resembled real-life survey data that I often work with – mostly categorical values, some unknown variables, some reasonable models of behaviour available but certainly at least partly mis-specified. The one complication I left out for now was missing data.</p>
<p>I created data in 14 regions, with 100 neighbourhoods or PSUs (primary sampling units) in each region that were going to be used for cluster sampling. I made variables for people's &quot;shapeâ€� (circle, square, triangle or hexagon), &quot;colourâ€� (red, blue, etc), a mystery unobserved variable, an unobserved latent continuous variable that depends on all the other variables mentioned so far, and a single target or response variable of interest called <code>y</code> which is TRUE or FALSE with probability depending on that latent variable.</p>
<p>In the end we are going to use these variables in these ways:</p>
<ul>
<li>We'll use <code>region</code> as strata and <code>psu</code> as primary sampling unit in a cluster sampling approach</li>
<li>We'll use the marginal population counts by <code>region</code>, <code>shape</code> and <code>colour</code> in a weighting scheme</li>
<li>We'll use <code>region</code>, <code>shape</code> and <code>colour</code> as explanatory variables in a model with <code>y</code>. This model is slightly mis-specified because it doesn't include the &quot;mystery variableâ€� which is unobserved, and the relationship of the variables to y isn't exactly as such variables normally work together in a logistic regression.</li>
</ul>
<p>Once I've generated the data, here is how it looks for the full population of one million:</p>
<p><img width="100%" src="http://freerangestats.io/img/0132-proportions.svg"></p>
<p>Or here is the correlation matrix, showing the relationships of underlying unobserved continuous variables (rather than their categorical manifestations) with eachother and with <code>y</code>:</p>
<table>
<thead>
<tr>
<th>Â </th>
<th>mystery_var</th>
<th>region_y</th>
<th>psu_y</th>
<th>shape_y</th>
<th>colour_y</th>
<th>latent</th>
<th>y</th>
</tr>
</thead>
<tbody>
<tr>
<td>mystery_var</td>
<td>1.00</td>
<td>0.00</td>
<td>0.02</td>
<td>0.00</td>
<td>0.01</td>
<td>0.09</td>
<td>0.01</td>
</tr>
<tr>
<td>region_y</td>
<td>0.00</td>
<td>1.00</td>
<td>0.00</td>
<td>0.00</td>
<td>-0.01</td>
<td>0.31</td>
<td>0.07</td>
</tr>
<tr>
<td>psu_y</td>
<td>0.02</td>
<td>0.00</td>
<td>1.00</td>
<td>0.01</td>
<td>-0.01</td>
<td>0.73</td>
<td>0.18</td>
</tr>
<tr>
<td>shape_y</td>
<td>0.00</td>
<td>0.00</td>
<td>0.01</td>
<td>1.00</td>
<td>0.07</td>
<td>0.55</td>
<td>0.11</td>
</tr>
<tr>
<td>colour_y</td>
<td>0.01</td>
<td>-0.01</td>
<td>-0.01</td>
<td>0.07</td>
<td>1.00</td>
<td>0.23</td>
<td>0.05</td>
</tr>
<tr>
<td>latent</td>
<td>0.09</td>
<td>0.31</td>
<td>0.73</td>
<td>0.55</td>
<td>0.23</td>
<td>1.00</td>
<td>0.22</td>
</tr>
<tr>
<td>y</td>
<td>0.01</td>
<td>0.07</td>
<td>0.18</td>
<td>0.11</td>
<td>0.05</td>
<td>0.22</td>
<td>1.00</td>
</tr>
</tbody>
</table>
<p>For example, <code>region_y</code> in the above is a variable that takes a given continuous value for each of regions A, B, C, etc. The value of <code>region_y</code> isn't observed, I just generate it as part of the simulation process. The underlying value of <code>region_y</code> has a correlation of 0.31 with the unobserved underlying value of <code>latent</code>. <code>latent</code> itself is the direct driver of the probability of <code>y</code> being TRUE, but <code>y</code> is a random variable so the correlation of <code>region_y</code> with <code>y</code> is 0.07 (ie less than 0.31), still more than zero but not as strong as <code>region_y</code>'s correlation with the unobserved <em>probability</em> of y.</p>
<p><code>psu_y</code> has the highest correlation with <code>y</code> and I did this by design – I wanted there to be a lot of intra-class correlation at the PSU level, to give distinction to the complex survey sampling and estimation strategy I'm exploring here. So knowing the PSU (which I think of as a suburb or meshblock) tells you a lot about whether an individual has <code>y</code> or not – more so than the individual's shape or colour. Perhaps <code>y</code> is some highly spatially correlated indicator like a contagious disease.</p>
<p>To sum up – in my simulated population, there is a definite relationship between each categorical variable and <code>y</code>, via unobserved latent continuous variables, but it's not dramatic.</p>
<p>I wrote an R function to generate this population; even though I only use it once in this blog post, it was very useful to have it as a function with clear parameter choices when I was playing around.</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>boot</span><span>)</span><span>
</span><span>library</span><span>(</span><span>testthat</span><span>)</span><span>
</span><span>library</span><span>(</span><span>survey</span><span>)</span><span>
</span><span>library</span><span>(</span><span>broom</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>gridExtra</span><span>)</span><span>
</span><span>library</span><span>(</span><span>kable</span><span>)</span><span>
</span><span>#--------------------create a simulated population------------------------
</span><span>#' @param N total population size
#' @param n_regions number of regions (which will be used in sampling as strata)
#' @param psu_sd standard deviation of random contribution to latent variable under y at the PSU #' (primary sampling unit eg suburb or meshblock) level. Higher values lead to higher intra-class
#' correlation, and hence higher design effects when we use psu as the primary sampling unit in a
#' sampling design
#' @param region_sd as psu_sd but at region level
#' @param mystery_sd extra random continuous variable that contributes to the latent variable under y.
</span><span>make_population</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>N</span><span> </span><span>=</span><span> </span><span>100000</span><span>,</span><span> </span><span>n_regions</span><span> </span><span>=</span><span> </span><span>14</span><span>,</span><span>
</span><span>psu_sd</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span>
</span><span>region_sd</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span>
</span><span>colours_sd</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span>
</span><span>shapes_sd</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span>
</span><span>mystery_sd</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span>
</span><span>colours_v</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;red&quot;</span><span>,</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>&quot;green&quot;</span><span>,</span><span> </span><span>&quot;yellow&quot;</span><span>,</span><span> </span><span>&quot;orange&quot;</span><span>,</span><span> </span><span>&quot;grey&quot;</span><span>),</span><span>
</span><span>shapes</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;square&quot;</span><span>,</span><span> </span><span>&quot;circle&quot;</span><span>,</span><span> </span><span>&quot;triangle&quot;</span><span>,</span><span> </span><span>&quot;hexagon&quot;</span><span>),</span><span>
</span><span>seed</span><span> </span><span>=</span><span> </span><span>123</span><span>){</span><span>
</span><span>set.seed</span><span>(</span><span>seed</span><span>)</span><span>
</span><span>population1</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>region</span><span> </span><span>=</span><span> </span><span>sample</span><span>(</span><span>LETTERS</span><span>[</span><span>1</span><span>:</span><span>n_regions</span><span>],</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>N</span><span>,</span><span> </span><span>replace</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>prob</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>runif</span><span>(</span><span>n_regions</span><span>)))</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>region</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>psu</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>region</span><span>,</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>(</span><span>n</span><span>()</span><span> </span><span>/</span><span> </span><span>100</span><span>)</span><span> </span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>n</span><span>(),</span><span> </span><span>replace</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)),</span><span>
</span><span>shape</span><span> </span><span>=</span><span> </span><span>sample</span><span>(</span><span>shapes</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>n</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>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span># we want colours and shapes to be crosscorrelated, so we make generation of colour dependent on shape:
</span><span> </span><span>mutate</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>case_when</span><span>(</span><span>
</span><span>shape</span><span> </span><span>==</span><span> </span><span>shapes</span><span>[</span><span>1</span><span>]</span><span> </span><span>~</span><span> </span><span>sample</span><span>(</span><span>colours_v</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>n</span><span>(),</span><span> </span><span>prob</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span> </span><span>1</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>4</span><span>),</span><span> </span><span>replace</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span>
</span><span>shape</span><span> </span><span>==</span><span> </span><span>shapes</span><span>[</span><span>2</span><span>]</span><span> </span><span>~</span><span> </span><span>sample</span><span>(</span><span>colours_v</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>n</span><span>(),</span><span> </span><span>prob</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span> </span><span>2</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>),</span><span> </span><span>replace</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span>
</span><span>shape</span><span> </span><span>==</span><span> </span><span>shapes</span><span>[</span><span>3</span><span>]</span><span> </span><span>~</span><span> </span><span>sample</span><span>(</span><span>colours_v</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>n</span><span>(),</span><span> </span><span>prob</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span> </span><span>4</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>3</span><span>,</span><span> </span><span>2</span><span>),</span><span> </span><span>replace</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span>
</span><span>shape</span><span> </span><span>==</span><span> </span><span>shapes</span><span>[</span><span>4</span><span>]</span><span> </span><span>~</span><span> </span><span>sample</span><span>(</span><span>colours_v</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>n</span><span>(),</span><span> </span><span>prob</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span> </span><span>2</span><span>,</span><span> </span><span>5</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>1</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>%>%</span><span>
</span><span>mutate</span><span>(</span><span>mystery_var</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>scale</span><span>(</span><span>rnorm</span><span>(</span><span>n</span><span>(),</span><span> </span><span>0</span><span>,</span><span> </span><span>1</span><span>)</span><span> </span><span>*</span><span> </span><span>as.numeric</span><span>(</span><span>as.factor</span><span>(</span><span>region</span><span>)))</span><span> </span><span>*</span><span> </span><span>mystery_sd</span><span>))</span><span>
</span><span># numeric values to add for each particular value of region, psu, colour, and shape:
</span><span> </span><span>regions</span><span> </span><span>&lt;-</span><span> </span><span>population1</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>(</span><span>region</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>region_y</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>scale</span><span>(</span><span>runif</span><span>(</span><span>n</span><span>()))</span><span> </span><span>*</span><span> </span><span>region_sd</span><span>))</span><span>
</span><span>psus</span><span> </span><span>&lt;-</span><span> </span><span>population1</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>(</span><span>psu</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>psu_y</span><span> </span><span>=</span><span> </span><span>rnorm</span><span>(</span><span>n</span><span>())</span><span> </span><span>*</span><span> </span><span>psu_sd</span><span>)</span><span>
</span><span>shapes</span><span> </span><span>&lt;-</span><span> </span><span>population1</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>(</span><span>shape</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>shape_y</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>scale</span><span>(</span><span>rgamma</span><span>(</span><span>n</span><span>(),</span><span> </span><span>1</span><span>,</span><span> </span><span>0.1</span><span>))</span><span> </span><span>*</span><span> </span><span>shapes_sd</span><span>))</span><span>
</span><span>colours</span><span> </span><span>&lt;-</span><span> </span><span>population1</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>(</span><span>colour</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>colour_y</span><span> </span><span>=</span><span> </span><span>as.vector</span><span>(</span><span>scale</span><span>(</span><span>rnorm</span><span>(</span><span>n</span><span>()))</span><span> </span><span>*</span><span> </span><span>colours_sd</span><span>))</span><span>
</span><span>population</span><span> </span><span>&lt;-</span><span> </span><span>population1</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>regions</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;region&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>psus</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;psu&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>shapes</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;shape&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>colours</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;colour&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>latent</span><span> </span><span>=</span><span> </span><span>inv.logit</span><span>(</span><span>as.vector</span><span>(</span><span>scale</span><span>(</span><span>mystery_var</span><span> </span><span>+</span><span> </span><span>region_y</span><span> </span><span>+</span><span> </span><span>psu_y</span><span> </span><span>+</span><span> </span><span>shape_y</span><span> </span><span>+</span><span> </span><span>colour_y</span><span> </span><span>+</span><span> </span><span>rnorm</span><span>(</span><span>n</span><span>(),</span><span> </span><span>0</span><span>,</span><span> </span><span>0.05</span><span>))))</span><span> </span><span>/</span><span> </span><span>2</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>as.logical</span><span>(</span><span>rbinom</span><span>(</span><span>n</span><span>(),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>prob</span><span> </span><span>=</span><span> </span><span>latent</span><span>)))</span><span>
</span><span>return</span><span>(</span><span>population</span><span>)</span><span>
</span><span>}</span><span>
</span><span># Simulate the actual population I'll be using for the blog:
</span><span>population</span><span> </span><span>&lt;-</span><span>make_population</span><span>(</span><span>N</span><span> </span><span>=</span><span> </span><span>10</span><span> </span><span>^</span><span> </span><span>6</span><span>,</span><span>
</span><span>psu_sd</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span>
</span><span>region_sd</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span>
</span><span>mystery_sd</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span>
</span><span>shapes_sd</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span>
</span><span>colours_sd</span><span> </span><span>=</span><span> </span><span>1.5</span><span>)</span><span>
</span><span># draw graphic
</span><span>population</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>region</span><span>,</span><span> </span><span>shape</span><span>,</span><span> </span><span>colour</span><span>,</span><span> </span><span>y</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>variable</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>y</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>value</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>y</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>variable</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;fill&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Prevalence of 'y'&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_flip</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;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Prevalence of y in simulated population&quot;</span><span>)</span><span>
</span><span># correlation matrix
</span><span>population</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>mystery_var</span><span>:</span><span>y</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>sample_n</span><span>(</span><span>10000</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>cor</span><span> </span><span>%>%</span><span>
</span><span>round</span><span>(</span><span>2</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>kable</span><span>()</span></code></pre>
</figure>
<h2>Sampling and processing</h2>
<p>Next step is to write a function that takes a two-stage cluster-based sample from this population. This means we first select a sample of clusters (in this case <code>psu</code>); and then from those clusters we select a sample of individuals. I did this in a way that selects a set number of PSUs (defaults to 10, but most of this blog I use 5) from each and every region, then a set random number of people (15 people if we have 10 PSUs per region, 30 people if we have 5) per PSU to bring the total sample size up to 2,100. 2,100 feels an unremarkable sample size for the sorts of surveys I've been looking at this year.</p>
<p>Because the regions have quite different sizes, and the PSUs have slightly different sizes, there are many different probabilities of an individual being selected. Everyone has a non-zero chance; which we need to calculate for subsequent weighting purposes. There are R packages that will perform this sort of sampling directly for you, but I've just done it from scratch in the code below for clarity.</p>
<p>Once I've got the sample, I:</p>
<ul>
<li>use the inverse of the selection probability as initial weights</li>
<li>create a set of jackknife replicate weights suitable for stratified sample designs</li>
<li>calibrate each set of weights to match the marginal population counts for each value of region, shape and colour.</li>
</ul>
<p>If replicate weights are new to you, they're worth getting on top of. When we use the jackknife or bootstrap with simple random samples, we create new samples in quite a simple way, by resampling with replacement from the original sample. When we have a complex survey, we instead create (and save) new sets of weights reflecting which points are in and which are out, and the new calibrated survey weights for that particular resample. Compared to doing all the resampling from scratch each time you do an analysis, this puts a lot of the computational load up front in the creation of multiple sets of (expensively) calibrated weights. These can be re-used repeatedly, for reproducibility and efficiency purposes.</p>
<p>Here's my function, which as well as containing my home-made sampling algorithm, uses Lumley's <code>survey</code> package to specify the design, replicate weights, and calibration to population totals for the analysis stage:</p>
<figure>
<pre><code data-lang="r"><span>#==================sampling======================
</span><span>
</span><span>#' Two stage sample from a &quot;population&quot; data frame
#' #' @param population a data frame to be sampled from, must include columns with name region, psu,
#' shape and colour
#' @param npsu number of PSUs (primary sampling units - think suburbs or meshblocks) per region (strata) to
#' saple
#' @param n total sample size
#' @param npeop number of people to sample per PSU
#' @value a survey design object, calibrated to population totals for region, shape and colour; with primary
#' selection by psu and stratified by region; with Jackknife replicate weights.
</span><span>samp</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>population</span><span>,</span><span> </span><span>npsu</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>2100</span><span>,</span><span> </span><span>npeop</span><span> </span><span>=</span><span> </span><span>round</span><span>(</span><span>n</span><span> </span><span>/</span><span> </span><span>npsu</span><span> </span><span>/</span><span> </span><span>length</span><span>(</span><span>unique</span><span>(</span><span>population</span><span>$</span><span>region</span><span>))),</span><span>
</span><span>seed</span><span> </span><span>=</span><span> </span><span>123</span><span>){</span><span>
</span><span>if</span><span>(</span><span>!</span><span>is.null</span><span>(</span><span>seed</span><span>)){</span><span>
</span><span>set.seed</span><span>(</span><span>seed</span><span>)</span><span>
</span><span>}</span><span>
</span><span># Population totals for margins, which we presume are known from a census or similar. We are
</span><span> </span><span># pretending we know the marginal totals for these dimensions, but not a full cross tabulation
</span><span> </span><span># (ie we know the total number of circles, and of red people, but not of red circular people):
</span><span> </span><span>shape_tots</span><span> </span><span>&lt;-</span><span> </span><span>population</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>shape</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Freq</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span>
</span><span>colour_tots</span><span> </span><span>&lt;-</span><span> </span><span>population</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>colour</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Freq</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span>
</span><span>region_tots</span><span> </span><span>&lt;-</span><span> </span><span>population</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>region</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>Freq</span><span> </span><span>=</span><span> </span><span>n</span><span>())</span><span>
</span><span># number of people in each psu, which we will use for our weighting:
</span><span> </span><span>region_psu_pops</span><span> </span><span>&lt;-</span><span> </span><span>population</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>region</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>psus_in_region</span><span> </span><span>=</span><span> </span><span>length</span><span>(</span><span>unique</span><span>(</span><span>psu</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>region</span><span>,</span><span> </span><span>psu</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>
</span><span># how many people in each PSU:
</span><span> </span><span>people_in_psu</span><span> </span><span>=</span><span> </span><span>n</span><span>(),</span><span>
</span><span># given the PSU is sampled, what's the chance of an individual from that PSU being in sample:
</span><span> </span><span>prob_from_psu</span><span> </span><span>=</span><span> </span><span>npeop</span><span> </span><span>/</span><span> </span><span>people_in_psu</span><span>,</span><span>
</span><span># so what's the a total priori probability for an individual being sampled:
</span><span> </span><span>prob</span><span> </span><span>=</span><span> </span><span>npsu</span><span> </span><span>/</span><span> </span><span>unique</span><span>(</span><span>psus_in_region</span><span>)</span><span> </span><span>*</span><span> </span><span>prob_from_psu</span><span>,</span><span>
</span><span># inverse of that probability for use as weight:
</span><span> </span><span>wt</span><span> </span><span>=</span><span> </span><span>1</span><span> </span><span>/</span><span> </span><span>prob</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>region_tots</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;region&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>people_in_region</span><span> </span><span>=</span><span> </span><span>Freq</span><span>)</span><span>
</span><span># check that the total sum of probabilities of being selected times people in psus </span><span> </span><span># equals our desired sample size:
</span><span> </span><span>expect_equal</span><span>(</span><span>round</span><span>(</span><span>n</span><span>,</span><span> </span><span>-1</span><span>),</span><span> </span><span>round</span><span>(</span><span>with</span><span>(</span><span>region_psu_pops</span><span>,</span><span> </span><span>sum</span><span>(</span><span>prob</span><span> </span><span>*</span><span> </span><span>people_in_psu</span><span>)),</span><span> </span><span>-1</span><span>))</span><span>
</span><span># we sample PSUs with equal probability within each region (ie not proportionate to size, </span><span> </span><span># which would be a better sampling strategy):
</span><span> </span><span>psu_sampled</span><span> </span><span>&lt;-</span><span> </span><span>population</span><span> </span><span>%>%</span><span>
</span><span>distinct</span><span>(</span><span>region</span><span>,</span><span> </span><span>psu</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># group by region, so each region acts as a stratum and we get the same number of
</span><span> </span><span># of PSUs per region (again, this isn't efficient sampling, but part of the
</span><span> </span><span># point of this exercise is to have a complex non-optimal sample):
</span><span> </span><span>group_by</span><span>(</span><span>region</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>seq</span><span> </span><span>=</span><span> </span><span>rnorm</span><span>(</span><span>n</span><span>()))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>seq</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>slice</span><span>(</span><span>1</span><span>:</span><span>npsu</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>seq</span><span>)</span><span>
</span><span>sampled</span><span> </span><span>&lt;-</span><span> </span><span>population</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>region</span><span>,</span><span> </span><span>psu</span><span>,</span><span> </span><span>shape</span><span>,</span><span> </span><span>colour</span><span>,</span><span> </span><span>y</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># limit ourselves to just the PSUs we have chosen for our sample:
</span><span> </span><span>inner_join</span><span>(</span><span>psu_sampled</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;region&quot;</span><span>,</span><span> </span><span>&quot;psu&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span># within each PSU, pick `npeop` people at random:
</span><span> </span><span>group_by</span><span>(</span><span>psu</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>seq</span><span> </span><span>=</span><span> </span><span>rnorm</span><span>(</span><span>n</span><span>()))</span><span> </span><span>%>%</span><span>
</span><span>slice</span><span>(</span><span>1</span><span>:</span><span>npeop</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>region_psu_pops</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;region&quot;</span><span>,</span><span> </span><span>&quot;psu&quot;</span><span>))</span><span>
</span><span># Specify the survey design.
</span><span> </span><span># we will simplify things by ignoring any finite population corrections, either
</span><span> </span><span># for PSUs within region, or for individuals within PSUs. This is ok if the sample
</span><span> </span><span># is small compared to population (which it is).
</span><span> </span><span># Sampling scheme:
</span><span> </span><span>des</span><span> </span><span>&lt;-</span><span> </span><span>svydesign</span><span>(</span><span>~</span><span>psu</span><span>,</span><span> </span><span>strata</span><span> </span><span>=</span><span> </span><span>~</span><span>region</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>sampled</span><span>,</span><span> </span><span>weights</span><span> </span><span>=</span><span> </span><span>~</span><span>wt</span><span>)</span><span>
</span><span># Replicate weights:
</span><span> </span><span>des</span><span> </span><span>&lt;-</span><span> </span><span>as.svrepdesign</span><span>(</span><span>des</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;JKn&quot;</span><span>)</span><span>
</span><span># Post stratification calibration to marginal population totals:
</span><span> </span><span>des</span><span> </span><span>&lt;-</span><span> </span><span>calibrate</span><span>(</span><span>des</span><span>,</span><span> </span><span>list</span><span>(</span><span>~</span><span>shape</span><span>,</span><span> </span><span>~</span><span>colour</span><span>,</span><span> </span><span>~</span><span>region</span><span>),</span><span>
</span><span>population</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>shape_tots</span><span>,</span><span> </span><span>colour_tots</span><span>,</span><span> </span><span>region_tots</span><span>))</span><span>
</span><span>return</span><span>(</span><span>des</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<p>Like most real world complex surveys, this sampling design has a couple of inefficiencies compared to a simple random sample:</p>
<ul>
<li>Because we sample equally from each region, some points (from larger regions) are representing many more population individuals than others. A typical reason for this strategy would be when we are prepared to sacrifice some efficiency in estimating population totals because we want good estimates of totals for each region, even smaller regions.</li>
<li>Because we first pick a PSU (think of these as a suburb or meshblock), then sample individuals from that PSU, the individual values are correlated. Once you've interviewed someone in the PSU, their neighbours are less valuable statistically than people chosen at random. A typical reason for following this strategy is cost and convenience of moving interviewers around.</li>
</ul>
<p>All this means that the variance of estimates from our complex survey sample is higher than if we had a sample of the same size that was selected by simple random sampling. In fact, if the number of PSUs selected per region is the default of 10, an estimated &quot;design effectâ€� for this particular population and sampling strategy is 1.8. That is, the variance of our estimates is 1.8 times larger than a hypothetical simple random sample. Another way to look at this is that if a simple random sample were possible, we would need only 2,100 / 1.8 = 1,167 members in the sample to get the same quality estimate of total (population) <code>y</code> as from our cluster sample.</p>
<div>
<pre><code>> des &lt;- samp(population, npsu = 10, seed = 123)
> svymean(~y, des, deff = TRUE)
          mean      SE   DEff
yFALSE 0.74814 0.01282 1.8346
yTRUE  0.25186 0.01282 1.8346
</code></pre>
</div>
<p>The design effect comes mostly from the clustering of individuals in PSUs. The more PSUs we select (and hence, less individuals per PSU for the same overall sample size), the lower the design effect, as seen in this chart:</p>
<p><img width="100%" src="http://freerangestats.io/img/0132-design-effects.svg"></p>
<figure>
<pre><code data-lang="r"><span>npsus</span><span> </span><span>&lt;-</span><span> </span><span>rep</span><span>(</span><span>2</span><span>:</span><span>40</span><span>,</span><span> </span><span>6</span><span>)</span><span>
</span><span>deffs_l</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>npsus</span><span>,</span><span> </span><span>function</span><span>(</span><span>i</span><span>){</span><span>
</span><span>des</span><span> </span><span>&lt;-</span><span> </span><span>samp</span><span>(</span><span>population</span><span>,</span><span> </span><span>npsu</span><span> </span><span>=</span><span> </span><span>i</span><span>,</span><span> </span><span>seed</span><span> </span><span>=</span><span> </span><span>NULL</span><span>)</span><span>
</span><span>X</span><span> </span><span>&lt;-</span><span> </span><span>svymean</span><span>(</span><span>~</span><span>y</span><span>,</span><span> </span><span>des</span><span>,</span><span> </span><span>deff</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>return</span><span>(</span><span>c</span><span>(</span><span>npsu</span><span> </span><span>=</span><span> </span><span>i</span><span>,</span><span> </span><span>deff</span><span> </span><span>=</span><span> </span><span>attributes</span><span>(</span><span>X</span><span>)</span><span>$</span><span>&quot;deff&quot;</span><span>[</span><span>1</span><span>,</span><span>1</span><span>]))</span><span>
</span><span>})</span><span>
</span><span>deffs</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>deffs_l</span><span>))</span><span>
</span><span>ggplot</span><span>(</span><span>deffs</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>npsu</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>deff</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;loess&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;Number of PSUs selected per stratum&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Design effect for mean(y)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Design effect is higher when we rely on fewer primary sampling units&quot;</span><span>)</span></code></pre>
</figure>
<h2>Modelling</h2>
<p>OK, so we've got a function to produce our complex sample. Let's get into the business of modelling <code>y</code> based on <code>region</code>, <code>colour</code> and <code>shape</code>. From now on, I'll be selecting 5 PSUs per region and 30 people per PSU, which gives us a relatively high design effect, but still a total of 14 x 5 = 70 PSUs for the whole sample. This is a plausibly realistic number of PSUs if the sampling process involves moving around a whole country and interviewing people face to face. By way of comparison, Lumley's book <em>Complex Surveys: A Guide to Analysis Using R</em> cites (page 40) the example of the National Health and Nutrition Examination Survey, NHANES II, which sampled 64 locations and 440 participants in each location, for a relatively intensive and expensive interview/examination process.</p>
<p>Because I'm following on from <a rel="nofollow" href="http://freerangestats.info/blog/2018/08/17/risk-ratios" target="_blank">last week's post</a>, I'm interested in relative risks comparing one category to another (eg the probability of a &quot;circleâ€� having <code>y</code>, compared to the probability of a &quot;squareâ€� having it). So in the generalized linear model I'm going to fit, I need to use a logarithm link function connecting the expected value of the response variable with the linear predictor of the explanatory variables (<em>if asking why? I don't have time to explain that now</em>). Two of the best candidates for this are <code>family = quasibinomial(log)</code> and <code>family = quasipoisson(log)</code>. Taking e to the power of coefficients or other contrast in the linear predictor will give us relative risks in this case. It's the <code>log</code> that's particularly important; the default link function for <code>quasibinomial</code> would be <code>logit</code>, which would give us odds ratios, not risk ratios.</p>
<p>Here's an example of the sort of outcome I get from two such risk models, fitted to a single sample of 2,100 drawn with my functions set out above. We can see the relative risks of different levels of my categorical variables (compared to the base levels of the variable in each case). &quot;1â€� would mean the given risk level of the value is the same as for the reference level. So we would conclude, for example, that &quot;squareâ€� people have 1.4 to 2.5 times the relative risk of getting <code>y</code> as do &quot;circleâ€� people (circle is the reference level for shape).</p>
<p><img width="100%" src="http://freerangestats.io/img/0132-example-cis.svg"></p>
<p>I've marked the &quot;trueâ€� values of the coefficients with dots, and shown the 95% confidence intervals from two <code>quasibinomial(log)</code> options. One of these is what I'm calling &quot;naiveâ€� (although it's still some fairly sophisticated stats) because it ignores the complex survey nature of the data and just uses R's <code>glm</code> function as though we had a simple random sample. The other method uses Lumley's <code>svyglm</code> to appropriately take the sampling and weighting into account. The clustering will tend to increase standard errors (and hence the width of confidence intervals); the use of auxiliary information in weighting to match population totals for shape, colour and region will decrease them. In this case, the net effect of treating the survey design with respect is to increase standard errors and the width of confidence intervals, as seen by the longer blue lines compared to the red lines.</p>
<p>Here's the code that creates that one sample and fits the two models to it, comparing the results to the &quot;correctâ€� values from fitting a model to the full population of 1 million people:</p>
<figure>
<pre><code data-lang="r"><span>#===========modelling============
# First, we model the full population to get the &quot;true&quot; values (note that these are not
# really &quot;true&quot; as the model is somewhat mis-specified, but they are as true as can be done
# given the imperfection of the model, and hence form a good benchmark for the same model
# fit to a sample). As we have the full population, we don't need to worry about survey methods
# and can just use stats::glm rather than survey::svyglm
</span><span>full_pop_model_bin</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>region</span><span> </span><span>+</span><span> </span><span>shape</span><span> </span><span>+</span><span> </span><span>colour</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasibinomial</span><span>(</span><span>log</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>population</span><span>)</span><span>
</span><span>#-------------compare a single example-------
# Survey design, for use with svyglm:
</span><span>des</span><span> </span><span>&lt;-</span><span> </span><span>samp</span><span>(</span><span>population</span><span>,</span><span> </span><span>npsu</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>seed</span><span> </span><span>=</span><span> </span><span>999</span><span>)</span><span>
</span><span># Extract just the data from the design for use with glm:
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>des</span><span>$</span><span>variables</span><span>
</span><span>samp_model_bin</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>region</span><span> </span><span>+</span><span> </span><span>shape</span><span> </span><span>+</span><span> </span><span>colour</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasibinomial</span><span>(</span><span>log</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>data</span><span>)</span><span>
</span><span>samp_model_bin_svy</span><span> </span><span>&lt;-</span><span> </span><span>svyglm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>region</span><span> </span><span>+</span><span> </span><span>shape</span><span> </span><span>+</span><span> </span><span>colour</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasibinomial</span><span>(</span><span>log</span><span>),</span><span> </span><span>design</span><span> </span><span>=</span><span> </span><span>des</span><span>)</span><span>
</span><span># Combine results for graphic:
</span><span>res</span><span> </span><span>&lt;-</span><span> </span><span>mutate</span><span>(</span><span>tidy</span><span>(</span><span>exp</span><span>(</span><span>confint</span><span>(</span><span>samp_model_bin</span><span>))),</span><span> </span><span>model</span><span> </span><span>=</span><span> </span><span>&quot;naive&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rbind</span><span>(</span><span>mutate</span><span>(</span><span>tidy</span><span>(</span><span>exp</span><span>(</span><span>confint</span><span>(</span><span>samp_model_bin_svy</span><span>))),</span><span> </span><span>model</span><span> </span><span>=</span><span> </span><span>&quot;survey&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>lower</span><span> </span><span>=</span><span> </span><span>X</span><span>2.5</span><span>..</span><span>,</span><span>
</span><span>upper</span><span> </span><span>=</span><span> </span><span>X</span><span>97.5</span><span>..</span><span>,</span><span>
</span><span>names</span><span> </span><span>=</span><span> </span><span>.rownames</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>tidy</span><span>(</span><span>exp</span><span>(</span><span>coef</span><span>(</span><span>full_pop_model_bin</span><span>)),</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;names&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>name_number</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>as.factor</span><span>(</span><span>names</span><span>)),</span><span>
</span><span>true_value</span><span> </span><span>=</span><span> </span><span>x</span><span>,</span><span>
</span><span>y_pos</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>model</span><span> </span><span>==</span><span> </span><span>&quot;naive&quot;</span><span>,</span><span> </span><span>name_number</span><span> </span><span>+</span><span> </span><span>0.1</span><span>,</span><span>
</span><span>name_number</span><span> </span><span>-</span><span> </span><span>0.1</span><span>),</span><span>
</span><span>names</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;colour&quot;</span><span>,</span><span> </span><span>&quot;Colour: &quot;</span><span>,</span><span> </span><span>names</span><span>),</span><span>
</span><span>names</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;region&quot;</span><span>,</span><span> </span><span>&quot;Region: &quot;</span><span>,</span><span> </span><span>names</span><span>),</span><span>
</span><span>names</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;shape&quot;</span><span>,</span><span> </span><span>&quot;Shape: &quot;</span><span>,</span><span> </span><span>names</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>model</span><span>,</span><span> </span><span>name_number</span><span>)</span><span>
</span><span># Draw graphic:
</span><span>res</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_segment</span><span>(</span><span>aes</span><span>(</span><span>yend</span><span> </span><span>=</span><span> </span><span>y_pos</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>y_pos</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>lower</span><span>,</span><span> </span><span>xend</span><span> </span><span>=</span><span> </span><span>upper</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>model</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1.7</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span> </span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>as.factor</span><span>(</span><span>names</span><span>)),</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>x</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>labels</span><span> </span><span>=</span><span> </span><span>res</span><span>[</span><span>1</span><span>:</span><span>22</span><span>,</span><span> </span><span>]</span><span>$</span><span>names</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>22</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;Relative risk ratios&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Variable&quot;</span><span>,</span><span>
</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;Estimation method&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Complex sample of around 2,100 drawn from a population of a million. Black points show the true values&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Example confidence intervals for risk ratios from a quasi-binomial model&quot;</span><span>,</span><span>
</span><span>&quot;Two modelling methods from the same sample, compared to true values from total population.&quot;</span><span>)</span></code></pre>
</figure>
<p>That's just one sample of course. How about if we do this 100 times? That brings us to the graphic I started the blog with. We can see that both methods are fair performers when it comes to point estimates, and indeed aren't too bad even for confidence interval coverage (I would be pleased and surprised if 85% of one's &quot;95%â€� time series prediction intervals contained the true value, by comparison, but even the naive method exceeds that for these confidence intervals). But the coverage of the survey method very nearly matches the advertised 95% design, which is excellent, particularly as the model is slightly mis-specified compared to the data generation process.</p>
<p><img width="100%" src="http://freerangestats.io/img/0132-comparisons.svg"></p>
<p>Finally, the question I thought this blog was going to be all about – quasi-poisson versus quasi-binomial families with a log link when using logistic regression to get relative risks? There's an oldish but good thread on this on <a rel="nofollow" href="http://grokbase.com/t/r/r-help/109dad6km5/r-relative-risk-regression-with-survey-data" target="_blank">R-help</a>.</p>
<p>My own experience so far with these two options is that they give similar results, but that the <code>quasipoisson(log)</code> method is less likely to fall over (ie fail to converge to a solution) than is <code>quasibinomial(log)</code>. I don't know why this is. <code>quasibinomial</code> will be modelling variance as proportional to <code>mu(1 - mu)</code> (where <code>mu</code> is the expected value of the response, given the linear predictor) whereas <code>quasipoisson</code> will be modelling it as proportional to <code>mu</code>. In effect, this will mean the <code>quasipoisson</code> expects high variance when the probability of <code>y</code> is close to 1 and will down-weight those points; whereas <code>quasibinomial</code> will do the opposite. In data like that I've been using today, the expected values don't get that high so it makes very little difference.</p>
<p>In the simulations I've done for this blog, I got around the problem of the occasional non-convergence of the <code>quasibinomial</code> model by first fitting it with the <code>quasipoisson</code> option and then using the estimated coefficient values from that model as starting values for the <code>quasibinomial</code> model. This seems to work well; we get the natural (or so it seems to me) treatment of variance as being proportional to <code>mu(1-mu)</code> of the binomial approach while still being robust enough to work without human intervention on a wide range of samples.</p>
<p>[As an aside, the use of <em>quasi</em> Poisson is particularly important if we are modelling binary outcomes as we are here. With the response constrained to being 0 or 1, the variance will often be less (and nearly always different) from being <em>exactly</em> <code>mu</code> which would be expected for the straight Poisson family. So we will have under-dispersion (or less likely, over-dispersion) relative to the Poisson distribution. Use of <code>quasipoisson</code> allows this dispersion parameter to be estimated rather than forced onto the model.]</p>
<p>Here's a comparison of the confidence interval coverage of those two methods; we can see it's virtually identical in the long run, with this particular population and model:</p>
<p><img width="100%" src="http://freerangestats.io/img/0132-bin-pois.svg"></p>
<p>So, to conclude (bearing in mind that this is only with one set of simulated population data, but the principles seem plausible):</p>
<ul>
<li>ignoring the complex survey nature of the data and just using <code>glm()</code> out of the box can still mean that you get ok point estimates</li>
<li>however, your standard errors (and hence confidence interval widths, and statistical inferences in general) run the risk of understating the uncertainty and overstating the information, particularly when the original data is clustered</li>
<li>there's not much to choose from in weighing up the use of <code>quasibinomial(log)</code> or <code>quasipoisson(log)</code> when setting out get get relative risks from a generalized linear model with a binary response. They give similar results, and similarly successful confidence intervals.</li>
</ul>
<p>Here's the last chunk of code – for those more systematic checks of &quot;naiveâ€� versus survey methods, and quasipoisson versus quasibinomial:</p>
<figure>
<pre><code data-lang="r"><span>#------------systematically compare lots of different samples' point estimates--------
</span><span>
</span><span>compare_mse</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>npsu</span><span>,</span><span> </span><span>seed</span><span> </span><span>=</span><span> </span><span>NULL</span><span>){</span><span>
</span><span>des</span><span> </span><span>&lt;-</span><span> </span><span>samp</span><span>(</span><span>population</span><span>,</span><span> </span><span>npsu</span><span> </span><span>=</span><span> </span><span>npsu</span><span>,</span><span> </span><span>seed</span><span> </span><span>=</span><span> </span><span>seed</span><span>)</span><span>
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>des</span><span>$</span><span>variables</span><span>
</span><span># The quasibinomial response isn't robust enough for either glm or svyglm, so we get some starting
</span><span> </span><span># values with quasipoisson to ensure we can always get a response:
</span><span> </span><span>samp_model_pois_svy</span><span> </span><span>&lt;-</span><span> </span><span>svyglm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>region</span><span> </span><span>+</span><span> </span><span>shape</span><span> </span><span>+</span><span> </span><span>colour</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasipoisson</span><span>(</span><span>log</span><span>),</span><span> </span><span>design</span><span> </span><span>=</span><span> </span><span>des</span><span>)</span><span>
</span><span>samp_model_bin</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>region</span><span> </span><span>+</span><span> </span><span>shape</span><span> </span><span>+</span><span> </span><span>colour</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasibinomial</span><span>(</span><span>log</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>data</span><span>,</span><span>
</span><span>start</span><span> </span><span>=</span><span> </span><span>coef</span><span>(</span><span>samp_model_pois_svy</span><span>))</span><span>
</span><span>samp_model_bin_svy</span><span> </span><span>&lt;-</span><span> </span><span>svyglm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>region</span><span> </span><span>+</span><span> </span><span>shape</span><span> </span><span>+</span><span> </span><span>colour</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasibinomial</span><span>(</span><span>log</span><span>),</span><span> </span><span>design</span><span> </span><span>=</span><span> </span><span>des</span><span>,</span><span>
</span><span>start</span><span> </span><span>=</span><span> </span><span>coef</span><span>(</span><span>samp_model_pois_svy</span><span>))</span><span>
</span><span>return</span><span>(</span><span>
</span><span>c</span><span>(</span><span>survey</span><span> </span><span>=</span><span> </span><span>mean</span><span>((</span><span>coef</span><span>(</span><span>samp_model_bin_svy</span><span>)[</span><span>-1</span><span>]</span><span> </span><span>-</span><span> </span><span>coef</span><span>(</span><span>full_pop_model_bin</span><span>)[</span><span>-1</span><span>])</span><span> </span><span>^</span><span> </span><span>2</span><span>),</span><span>
</span><span>naive</span><span> </span><span>=</span><span> </span><span>mean</span><span>((</span><span>coef</span><span>(</span><span>samp_model_bin</span><span>)[</span><span>-1</span><span>]</span><span> </span><span>-</span><span> </span><span>coef</span><span>(</span><span>full_pop_model_bin</span><span>)[</span><span>-1</span><span>])</span><span> </span><span>^</span><span> </span><span>2</span><span>))</span><span>
</span><span>)}</span><span>
</span><span>mses</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>1</span><span>:</span><span>100</span><span>,</span><span> </span><span>function</span><span>(</span><span>i</span><span>){</span><span>compare_mse</span><span>(</span><span>npsu</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>seed</span><span> </span><span>=</span><span> </span><span>i</span><span>)})</span><span>
</span><span>mses_df</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>mses</span><span>))</span><span>
</span><span>mse</span><span> </span><span>&lt;-</span><span> </span><span>round</span><span>(</span><span>apply</span><span>(</span><span>mses_df</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>mean</span><span>),</span><span> </span><span>3</span><span>)</span><span>
</span><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>mses_df</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>naive</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>survey</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_abline</span><span>(</span><span>intercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>slope</span><span> </span><span>=</span><span> </span><span>1</span><span>,</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>geom_point</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;Mean square error of coefficients&quot;</span><span>,</span><span>
</span><span>&quot;Two methods of modelling, compared to correct (full\npopulation) values for all coefficients except the intercept.&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>paste0</span><span>(</span><span>&quot;Complex survey methods\nAverage mean squared error: &quot;</span><span>,</span><span> </span><span>mse</span><span>[</span><span>1</span><span>]),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;Naive methods.\nAverage mean squared error: &quot;</span><span>,</span><span> </span><span>mse</span><span>[</span><span>2</span><span>]))</span><span>
</span><span>#========compare confidence interval coverage========
</span><span>
</span><span>#------------systematically compare lots of different samples' point estimates--------
</span><span>
</span><span>ci_rate</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>correct_mod</span><span>,</span><span> </span><span>ci_mod</span><span>){</span><span>
</span><span>X</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>cbind</span><span>(</span><span>coef</span><span>(</span><span>correct_mod</span><span>),</span><span> </span><span>confint</span><span>(</span><span>ci_mod</span><span>)))</span><span>
</span><span>names</span><span>(</span><span>X</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;correct&quot;</span><span>,</span><span> </span><span>&quot;lower&quot;</span><span>,</span><span> </span><span>&quot;upper&quot;</span><span>)</span><span>
</span><span>return</span><span>(</span><span>sum</span><span>(</span><span>with</span><span>(</span><span>X</span><span>,</span><span> </span><span>correct</span><span> </span><span>></span><span> </span><span>lower</span><span> </span><span>&amp;</span><span> </span><span>correct</span><span> </span><span></span><span> </span><span>upper</span><span>))</span><span> </span><span>/</span><span> </span><span>nrow</span><span>(</span><span>X</span><span>))</span><span>
</span><span>}</span><span>
</span><span>compare_ci</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>npsu</span><span>,</span><span> </span><span>seed</span><span> </span><span>=</span><span> </span><span>NULL</span><span>){</span><span>
</span><span>des</span><span> </span><span>&lt;-</span><span> </span><span>samp</span><span>(</span><span>population</span><span>,</span><span> </span><span>npsu</span><span> </span><span>=</span><span> </span><span>npsu</span><span>,</span><span> </span><span>seed</span><span> </span><span>=</span><span> </span><span>seed</span><span>)</span><span>
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>des</span><span>$</span><span>variables</span><span>
</span><span># The quasibinomial response isn't robust enough for either glm or svyglm, so we get some starting
</span><span> </span><span># values with quasipoisson to ensure we can always get a response:
</span><span> </span><span>samp_model_pois_svy</span><span> </span><span>&lt;-</span><span> </span><span>svyglm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>region</span><span> </span><span>+</span><span> </span><span>shape</span><span> </span><span>+</span><span> </span><span>colour</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasipoisson</span><span>(</span><span>log</span><span>),</span><span> </span><span>design</span><span> </span><span>=</span><span> </span><span>des</span><span>)</span><span>
</span><span>samp_model_bin</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>region</span><span> </span><span>+</span><span> </span><span>shape</span><span> </span><span>+</span><span> </span><span>colour</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasibinomial</span><span>(</span><span>log</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>data</span><span>,</span><span>
</span><span>start</span><span> </span><span>=</span><span> </span><span>coef</span><span>(</span><span>samp_model_pois_svy</span><span>))</span><span>
</span><span>samp_model_bin_svy</span><span> </span><span>&lt;-</span><span> </span><span>svyglm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>region</span><span> </span><span>+</span><span> </span><span>shape</span><span> </span><span>+</span><span> </span><span>colour</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasibinomial</span><span>(</span><span>log</span><span>),</span><span> </span><span>design</span><span> </span><span>=</span><span> </span><span>des</span><span>,</span><span>
</span><span>start</span><span> </span><span>=</span><span> </span><span>coef</span><span>(</span><span>samp_model_pois_svy</span><span>))</span><span>
</span><span>return</span><span>(</span><span>
</span><span>c</span><span>(</span><span>binomial_svy</span><span> </span><span>=</span><span> </span><span>ci_rate</span><span>(</span><span>full_pop_model_bin</span><span>,</span><span> </span><span>samp_model_bin_svy</span><span>),</span><span>
</span><span>poisson_svy</span><span> </span><span>=</span><span> </span><span>ci_rate</span><span>(</span><span>full_pop_model_bin</span><span>,</span><span> </span><span>samp_model_pois_svy</span><span>),</span><span>
</span><span>naive</span><span> </span><span>=</span><span> </span><span>ci_rate</span><span>(</span><span>full_pop_model_bin</span><span>,</span><span> </span><span>samp_model_bin</span><span>))</span><span>
</span><span>)}</span><span>
</span><span>cis</span><span> </span><span>&lt;-</span><span> </span><span>lapply</span><span>(</span><span>1</span><span>:</span><span>100</span><span>,</span><span> </span><span>function</span><span>(</span><span>i</span><span>){</span><span>compare_ci</span><span>(</span><span>npsu</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>seed</span><span> </span><span>=</span><span> </span><span>i</span><span>)})</span><span>
</span><span>cis_df</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>cis</span><span>))</span><span>
</span><span>successes</span><span> </span><span>&lt;-</span><span> </span><span>round</span><span>(</span><span>apply</span><span>(</span><span>cis_df</span><span>,</span><span> </span><span>2</span><span>,</span><span> </span><span>mean</span><span>)</span><span> </span><span>*</span><span> </span><span>100</span><span>)</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>cis_df</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>binomial_svy</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>naive</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_abline</span><span>(</span><span>intercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>slope</span><span> </span><span>=</span><span> </span><span>1</span><span>,</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>annotate</span><span>(</span><span>&quot;rect&quot;</span><span>,</span><span> </span><span>xmin</span><span> </span><span>=</span><span> </span><span>-</span><span>Inf</span><span>,</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>0.95</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>1</span><span>,</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.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>annotate</span><span>(</span><span>&quot;rect&quot;</span><span>,</span><span> </span><span>xmin</span><span> </span><span>=</span><span> </span><span>0.95</span><span>,</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>-</span><span>Inf</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>0.95</span><span>,</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.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_jitter</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>0.01</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;Confidence intervals' coverage&quot;</span><span>,</span><span>
</span><span>&quot;Two methods of modelling, showing proportion that\ncontain the correct value (should be 95%).&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>paste0</span><span>(</span><span>&quot;Complex survey methods.\nAverage coverage: &quot;</span><span>,</span><span> </span><span>successes</span><span>[</span><span>1</span><span>],</span><span> </span><span>&quot;%&quot;</span><span>),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;Naive methods.\nAverage coverage: &quot;</span><span>,</span><span> </span><span>successes</span><span>[</span><span>3</span><span>],</span><span> </span><span>&quot;%&quot;</span><span>),</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>str_wrap</span><span>(</span><span>&quot;Source for both diagrams: complex samples of around 2,100 each, drawn from a population of 1 million stratified by 14 unequal regions with 5 out of 100 potential primary sampling units chosen per region. Model has three categorical explanatory variables and a single binary (quasi-binomial family) response.
Points have been jittered.&quot;</span><span>,</span><span> </span><span>90</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span>
</span><span>grid.arrange</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>cis_df</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>binomial_svy</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>poisson_svy</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_abline</span><span>(</span><span>intercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>slope</span><span> </span><span>=</span><span> </span><span>1</span><span>,</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>annotate</span><span>(</span><span>&quot;rect&quot;</span><span>,</span><span> </span><span>xmin</span><span> </span><span>=</span><span> </span><span>-</span><span>Inf</span><span>,</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>0.95</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>1</span><span>,</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.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>annotate</span><span>(</span><span>&quot;rect&quot;</span><span>,</span><span> </span><span>xmin</span><span> </span><span>=</span><span> </span><span>0.95</span><span>,</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>-</span><span>Inf</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>0.95</span><span>,</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.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_jitter</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>0.01</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;Confidence intervals' coverage&quot;</span><span>,</span><span>
</span><span>&quot;Two methods of modelling, showing proportion that\ncontain the correct value (should be 95%).&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>paste0</span><span>(</span><span>&quot;Quasi-binomial family\nAverage coverage: &quot;</span><span>,</span><span> </span><span>successes</span><span>[</span><span>1</span><span>],</span><span> </span><span>&quot;%&quot;</span><span>),</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;Quasi-poisson family\nAverage coverage: &quot;</span><span>,</span><span> </span><span>successes</span><span>[</span><span>2</span><span>],</span><span> </span><span>&quot;%&quot;</span><span>),</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>str_wrap</span><span>(</span><span>&quot;Source: complex samples of around 2,100 each, drawn from a population of 1 million stratified by 14 unequal regions with 5 out of 100 potential primary sampling units chosen per region. Model has three categorical explanatory variables and a single binary response from either the quasi-binomial or quasi-poisson family.
Points have been jittered.&quot;</span><span>,</span><span> </span><span>90</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span></code></pre>
</figure>
<p>Finally and slightly off the point, for a great example of the importance of reporting <em>absolute</em> risks as well as relative risks, check out <a rel="nofollow" href="https://medium.com/wintoncentre/the-risks-of-alcohol-again-2ae8cb006a4a" target="_blank">this blog</a> on the recent Lancet article arguing &quot;no safe level of alcohol usageâ€�. The statistics in the original article look to be sound, but while the relative risk of any alcohol is indeed above 1, the actual increase in absolute risk is small. For example:</p>
<blockquote>
<p>&quot;So a total of 50,000 bottles of gin among these 1,600 people is associated with one extra health problem.â€�</p>
</blockquote>
<p>Read the full blog for more.</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://freerangestats.info/blog/2018/08/24/simulating-survey"> 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/gn9YoTAGAp0" height="1"></a></div>