<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/a-little-function-to-help-generate-iccs-in-simple-clustered-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.rdatagen.net/post/a-little-function-to-help-generate-iccs-in-simple-clustered-data/"> ouR data generation</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In health services research, experiments are often conducted at the provider or site level rather than the patient level. However, we might still be interested in the outcome at the patient level. For example, we could be interested in understanding the effect of a training program for physicians on their patients. It would be very difficult to randomize patients to be exposed or not to the training if a group of patients all see the same doctor. So the experiment is set up so that only some doctors get the training and others serve as the control; we still compare the outcome at the patient level.</p>
<p>Typically, when conducting an experiment we assume that individual outcomes are not related to each other (other than the common effect of the exposure). With site-level randomization, we can’t make that assumption – groups of patients are all being treated by the same doctor. In general, even before the intervention, there might be variation across physicians. At the same time, patients within a practice will vary. So, we have two sources of variation: <em>between</em> practice and <em>within</em> practice variation that explain overall variation.</p>
<p>I touched on this when I discussed issues related to <a rel="nofollow" href="https://www.rdatagen.net/post/icc-for-gamma-distribution/" target="_blank">Gamma distributed clustered data</a>. A key concept is the intra-class coefficient or ICC, which is a measure of how <em>between</em> variation relates to overall variation. The ICC ranges from 0 (where there is no <em>between</em> variation – all site averages are the same) to 1 (where there is no variation within a site – all patients within the site have the same outcomes). Take a look at the earlier post for a bit more detail.</p>
<p>My goal here is to highlight a little function recently added to <code>simstudy</code> (v0.1.9, now available on <code>CRAN</code>). In the course of exploring study designs for cluster randomized trials, it is often useful to understand what happens (to sample size requirements, for example) when the ICC changes. When generating the data, it is difficult to control the ICC directly – we do this by controlling the variation. With normally distributed data, the ICC is an obvious function of the variances used to generate the data, so the connection is pretty clear. But, when the outcomes have binary, Poisson, or Gamma distributions (or anything else really), the connection between variation and the ICC is not always so obvious. Figuring out how to specify the data to generate a particular ICC might require quite a bit of trial and error.</p>
<p>The new function, <code>iccRE</code> (short for ICC random effects), allows users to specify target ICCs for a desired distribution (along with relevant parameters). The function returns the corresponding random effect variances that would be specified at the cluster level to generate the desired ICC(s).</p>
<p>Here’s an example for three possible ICCs in the context of the normal distribution:</p>
<pre><code>library(simstudy)

targetICC &lt;- c(0.05, 0.075, 0.10)

setVars &lt;- iccRE(ICC = targetICC, dist = &quot;normal&quot;, varWithin = 4)

round(setVars, 4)</code></pre>
<pre><code>## [1] 0.2105 0.3243 0.4444</code></pre>
<p>In the case when the target ICC is 0.075:</p>
<p><span>\[ ICC = \frac{\sigma_b^2}{\sigma_b ^2 + \sigma_w ^2} = \frac{0.324}{0.324 + 4} \approx 0.075\]</span></p>
<div>
<h3>Simulating from the normal distribution</h3>
<p>If we specify the variance for the site-level random effect to be 0.2105 in conjunction with the individual-level (within) variance of 4, the observed ICC from the simulated data will be approximately 0.05:</p>
<pre><code>set.seed(73632)

# specify between site variation

d &lt;- defData(varname = &quot;a&quot;, formula = 0, variance = 0.2105, id = &quot;grp&quot;)
d &lt;- defData(d, varname = &quot;size&quot;, formula = 1000, dist = &quot;nonrandom&quot;)

a &lt;- defDataAdd(varname = &quot;y1&quot;, formula = &quot;30 + a&quot;, 
                variance = 4, dist = &quot;normal&quot;)

dT &lt;- genData(10000, d)

# add patient level data

dCn05 &lt;- genCluster(dtClust = dT, cLevelVar = &quot;grp&quot;, 
                 numIndsVar = &quot;size&quot;, level1ID = &quot;id&quot;)

dCn05 &lt;- addColumns(a, dCn05)
dCn05</code></pre>
<pre><code>##             grp          a size       id       y1
##        1:     1 -0.3255465 1000        1 32.08492
##        2:     1 -0.3255465 1000        2 27.21180
##        3:     1 -0.3255465 1000        3 28.37411
##        4:     1 -0.3255465 1000        4 27.70485
##        5:     1 -0.3255465 1000        5 32.11814
##       ---                                        
##  9999996: 10000  0.3191311 1000  9999996 30.15837
##  9999997: 10000  0.3191311 1000  9999997 32.66302
##  9999998: 10000  0.3191311 1000  9999998 28.34583
##  9999999: 10000  0.3191311 1000  9999999 28.56443
## 10000000: 10000  0.3191311 1000 10000000 30.06957</code></pre>
<p>The <em>between</em> variance can be roughly estimated as the variance of the group means, and the <em>within</em> variance can be estimated as the average of the variances calculated for each group (this works well here, because we have so many clusters and patients per cluster):</p>
<pre><code>between &lt;- dCn05[, mean(y1), keyby = grp][, var(V1)] 
within &lt;- dCn05[, var(y1), keyby = grp][, mean(V1)]

total &lt;- dCn05[, var(y1)]

round(c(between, within, total), 3)</code></pre>
<pre><code>## [1] 0.212 3.996 4.203</code></pre>
<p>The ICC is the ratio of the <em>between</em> variance to the <em>total</em>, which is also the sum of the two component variances:</p>
<pre><code>round(between/(total), 3)</code></pre>
<pre><code>## [1] 0.05</code></pre>
<pre><code>round(between/(between + within), 3)</code></pre>
<pre><code>## [1] 0.05</code></pre>
<p>Setting the site-level variance at 0.4444 gives us the ICC of 0.10:</p>
<pre><code>d &lt;- defData(varname = &quot;a&quot;, formula = 0, variance = 0.4444, id = &quot;grp&quot;)
d &lt;- defData(d, varname = &quot;size&quot;, formula = 1000, dist = &quot;nonrandom&quot;)

a &lt;- defDataAdd(varname = &quot;y1&quot;, formula = &quot;30 + a&quot;, 
                variance = 4, dist = &quot;normal&quot;)

dT &lt;- genData(10000, d)

dCn10 &lt;- genCluster(dtClust = dT, cLevelVar = &quot;grp&quot;, 
                 numIndsVar = &quot;size&quot;, level1ID = &quot;id&quot;)

dCn10 &lt;- addColumns(a, dCn10)

between &lt;- dCn10[, mean(y1), keyby = grp][, var(V1)] 
within &lt;- dCn10[, var(y1), keyby = grp][, mean(V1)]

round(between / (between + within), 3)</code></pre>
<pre><code>## [1] 0.102</code></pre>
</div>
<div>
<h3>Other distributions</h3>
<p>The ICC is a bit more difficult to interpret using other distributions where the variance is a function of the mean, such as with the binomial, Poisson, or Gamma distributions. However, we can still use the notion of <em>between</em> and <em>within</em>, but it may need to be transformed to another scale.</p>
<p>In the case of <strong>binary</strong> outcomes, we have to imagine an underlying or latent continuous process that takes place on the logistic scale. (I talked a bit about this <a rel="nofollow" href="https://www.rdatagen.net/post/ordinal-regression/" target="_blank">here</a>.)</p>
<pre><code>### binary

(setVar &lt;- iccRE(ICC = 0.05, dist = &quot;binary&quot;))</code></pre>
<pre><code>## [1] 0.173151</code></pre>
<pre><code>d &lt;- defData(varname = &quot;a&quot;, formula = 0, variance = 0.1732, id = &quot;grp&quot;)
d &lt;- defData(d, varname = &quot;size&quot;, formula = 1000, dist = &quot;nonrandom&quot;)

a &lt;- defDataAdd(varname = &quot;y1&quot;, formula = &quot;-1 + a&quot;, dist = &quot;binary&quot;, 
                link = &quot;logit&quot;)

dT &lt;- genData(10000, d)

dCb05 &lt;- genCluster(dtClust = dT, cLevelVar = &quot;grp&quot;, numIndsVar = &quot;size&quot;, 
                 level1ID = &quot;id&quot;)
dCb05 &lt;- addColumns(a, dCb05)

dCb05</code></pre>
<pre><code>##             grp           a size       id y1
##        1:     1 -0.20740274 1000        1  0
##        2:     1 -0.20740274 1000        2  0
##        3:     1 -0.20740274 1000        3  0
##        4:     1 -0.20740274 1000        4  1
##        5:     1 -0.20740274 1000        5  0
##       ---                                   
##  9999996: 10000 -0.05448775 1000  9999996  0
##  9999997: 10000 -0.05448775 1000  9999997  1
##  9999998: 10000 -0.05448775 1000  9999998  0
##  9999999: 10000 -0.05448775 1000  9999999  0
## 10000000: 10000 -0.05448775 1000 10000000  0</code></pre>
<p>The ICC for the binary distribution is on the logistic scale, and the <em>within</em> variance is constant. The <em>between</em> variance is estimated on the log-odds scale:</p>
<pre><code>within &lt;- (pi ^ 2) / 3
means &lt;- dCb05[,mean(y1), keyby = grp]
between &lt;- means[, log(V1/(1-V1)), keyby = grp][abs(V1) != Inf, var(V1)]

round(between / (between + within), 3)</code></pre>
<pre><code>## [1] 0.051</code></pre>
<p>The ICC for the <strong>Poisson</strong> distribution is interpreted on the scale of the count measurements, even though the random effect variance is on the log scale. If you want to see the details behind the random effect variance derivation, see this <a rel="nofollow" href="https://onlinelibrary.wiley.com/doi/abs/10.1002/sim.7532" target="_blank">paper</a> by <em>Austin et al.</em>, which was based on original work by <em>Stryhn et al.</em> that can be found <a rel="nofollow" href="http://www.sciquest.org.nz/node/64294" target="_blank">here</a>.</p>
<pre><code>(setVar &lt;- iccRE(ICC = 0.05, dist = &quot;poisson&quot;, lambda = 30))</code></pre>
<pre><code>## [1] 0.0017513</code></pre>
<pre><code>d &lt;- defData(varname = &quot;a&quot;, formula = 0, variance = 0.0018, id = &quot;grp&quot;)
d &lt;- defData(d, varname = &quot;size&quot;, formula = 1000, dist = &quot;nonrandom&quot;)

a &lt;- defDataAdd(varname = &quot;y1&quot;, formula = &quot;log(30) + a&quot;, 
                dist = &quot;poisson&quot;, link = &quot;log&quot;)

dT &lt;- genData(10000, d)

dCp05 &lt;- genCluster(dtClust = dT, cLevelVar = &quot;grp&quot;, 
                 numIndsVar = &quot;size&quot;, level1ID = &quot;id&quot;)

dCp05 &lt;- addColumns(a, dCp05)
dCp05</code></pre>
<pre><code>##             grp           a size       id y1
##        1:     1 0.035654485 1000        1 26
##        2:     1 0.035654485 1000        2 36
##        3:     1 0.035654485 1000        3 31
##        4:     1 0.035654485 1000        4 34
##        5:     1 0.035654485 1000        5 21
##       ---                                   
##  9999996: 10000 0.002725561 1000  9999996 26
##  9999997: 10000 0.002725561 1000  9999997 25
##  9999998: 10000 0.002725561 1000  9999998 27
##  9999999: 10000 0.002725561 1000  9999999 28
## 10000000: 10000 0.002725561 1000 10000000 37</code></pre>
<p>The variance components and ICC for the Poisson can be estimated using the same approach as the normal distribution:</p>
<pre><code>between &lt;- dCp05[, mean(y1), keyby = grp][, var(V1)] 
within &lt;- dCp05[, var(y1), keyby = grp][, mean(V1)]

round(between / (between + within), 3)</code></pre>
<pre><code>## [1] 0.051</code></pre>
<p>Finally, here are the results for the <strong>Gamma</strong> distribution, which I talked about in great length in an <a rel="nofollow" href="https://www.rdatagen.net/post/icc-for-gamma-distribution/" target="_blank">earlier post</a>:</p>
<pre><code>(setVar &lt;- iccRE(ICC = 0.05, dist = &quot;gamma&quot;, disp = 0.25 ))</code></pre>
<pre><code>## [1] 0.01493805</code></pre>
<pre><code>d &lt;- defData(varname = &quot;a&quot;, formula = 0, variance = 0.0149, id = &quot;grp&quot;)
d &lt;- defData(d, varname = &quot;size&quot;, formula = 1000, dist = &quot;nonrandom&quot;)

a &lt;- defDataAdd(varname = &quot;y1&quot;, formula = &quot;log(30) + a&quot;, variance = 0.25, 
                dist = &quot;gamma&quot;, link = &quot;log&quot;)

dT &lt;- genData(10000, d)

dCg05 &lt;- genCluster(dtClust = dT, cLevelVar = &quot;grp&quot;, numIndsVar = &quot;size&quot;,
                 level1ID = &quot;id&quot;)

dCg05 &lt;- addColumns(a, dCg05)
dCg05</code></pre>
<pre><code>##             grp          a size       id       y1
##        1:     1 0.09466305 1000        1 14.31268
##        2:     1 0.09466305 1000        2 39.08884
##        3:     1 0.09466305 1000        3 28.08050
##        4:     1 0.09466305 1000        4 53.27853
##        5:     1 0.09466305 1000        5 37.93855
##       ---                                        
##  9999996: 10000 0.25566417 1000  9999996 14.16145
##  9999997: 10000 0.25566417 1000  9999997 42.54838
##  9999998: 10000 0.25566417 1000  9999998 76.33642
##  9999999: 10000 0.25566417 1000  9999999 34.16727
## 10000000: 10000 0.25566417 1000 10000000 21.06282</code></pre>
<p>The ICC for the Gamma distribution is on the log scale:</p>
<pre><code>between &lt;- dCg05[, mean(log(y1)), keyby = grp][, var(V1)] 
within &lt;- dCg05[, var(log(y1)), keyby = grp][, mean(V1)]

round(between / (between + within), 3)</code></pre>
<pre><code>## [1] 0.05</code></pre>
<p>It is possible to think about the ICC in the context of covariates, but interpretation is less straightforward. The ICC itself will likely vary across different levels of the covariates. For this reason, I like to think of the ICC in the marginal context.</p>
<p>I leave you with some visuals of clustered binary data with ICC’s ranging from 0 to 0.075, both on the log-odds and probability scales:</p>
<p><img src="https://i0.wp.com/www.rdatagen.net/post/2018-05-24-a-little-function-to-help-generate-iccs-in-simple-clustered-data_files/figure-html/unnamed-chunk-12-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.rdatagen.net/post/a-little-function-to-help-generate-iccs-in-simple-clustered-data/"> ouR data generation</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/J8V-j2cm7-I" height="1"></a></div>