<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/relative-risk-ratios-and-odds-ratios-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/17/risk-ratios"> free range statistics - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This post tries to explain the difference between odds ratios and relative risk ratios; and how just a few letters in the code fitting a generalized linear model mean the difference between extracting one or the other. There are plenty of other explanations available (for example, <a rel="nofollow" href="https://www.theanalysisfactor.com/the-difference-between-relative-risk-and-odds-ratios/" target="_blank">here</a> and <a rel="nofollow" href="https://www.mdedge.com/jfponline/article/65515/relative-risks-and-odds-ratios-whats-difference" target="_blank">here</a>), but there is also still plenty of <a rel="nofollow" href="https://www.researchgate.net/post/When_should_we_use_a_risk_ratio_odds_ratio_and_relative_ratio_in_the_meta-analysis_to_compare_between_2_predictors_variables" target="_blank">confusion</a> about the differences. I wanted to illustrate the issues with a concrete but simulated example and actual code that could be used as a foundation in the wild.</p>
<h2>Concepts</h2>
<p>Odds ratios and relative risk are commonly used to contrast the prevalence of some indicator (eg disease) in different categories of population. They seem to get particular emphasis in medical and epidemiological literature but are used broadly.</p>
<p>This diagram demonstrates with some simulated data the core concepts:</p>
<p><img width="100%" src="http://freerangestats.io/img/0131-data.svg"></p>
<ul>
<li>Tigers have a 1/4 (0.25) probability of being diseased, which is “1 to 3” odds of being diseased.</li>
<li>Lions have a 1/2 (0.5) probability of being diseased, which is “1 to 1” or “even” odds.</li>
<li>Bears have a 1/10 (0.1) probability of being diseased, which is “1 to 9” odds.</li>
</ul>
<p>I find the relative probabilities very intuitive – Tigers have 2.5 times the probability of being diseased as Bears, and Lions have 5 times the probability.</p>
<p>Odds ratios – not so much. Even though I’m a statistician with a particular interest in games of chance (which form the genealogy of odds), I don’t really intuit how to interpret the Tiger to Bears odds ratio of 3 times the odds of being diseased. In fact, there’s evidence that when scientific papers report odds ratios, they are <a rel="nofollow" href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC1112884/" target="_blank">commonly misinterpreted as being risk ratios</a> (“People of type X are twice as likely to have the disease compared to people of type Y” being a common formulation).</p>
<p>When the probabilities are very close to zero, then odds ratios are similar to risk ratios. The farther they get from zero, the more they differ, and the more dangerous it is if people misinterpret an odds ratio as a risk ratio.</p>
<p>The best argument for reporting odds ratios is that they are symmetric – whether you are reporting on presence or absence, the odds ratio is unaffected. In our example above, if we looked at the probability of not being diseased, the Tigers:Bears odds ratio becomes 3:1 / 9:1 which is still a 3, whereas the relative probability is (3/4) / (9/10) which is 5/6 or 0.833. So while it’s nice and intuitive to say Tigers are 2.5 times as likely as Bears to be diseased, they are 0.833 times as likely as Bears to be not-diseased. This can lead to confusion. The odds ratio is three both ways (or at least, 3 in one direction and 1/3 in the other).</p>
<p>Most situations where we want these statistics, I think relative risk ratios are much better, more intutitive, and easier to explain. I don’t think the asymmetry matters much in most communications, whereas the misunderstanding of odds ratios often does. Frequent misunderstanding of the target audience is a high price to pay for symmetry. And I’m not sure why symmetry is necessary anyway.</p>
<p>A balanced (in my view) and non-paywalled article on the topic (which has generated many) is <a rel="nofollow" href="https://jamanetwork.com/journals/jamapediatrics/fullarticle/381459" target="_blank">this one by Cummings in JAMA Pediatrics</a>.</p>
<p>Before we go onto to getting these estimates from a fitted model, here’s the R code that created that simulated data and the graphic:</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>viridis</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>765</span><span>)</span><span>
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>100000</span><span>
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>animal_</span><span> </span><span>=</span><span> </span><span>sample</span><span>(</span><span>c</span><span>(</span><span>&quot;lion&quot;</span><span>,</span><span> </span><span>&quot;tiger&quot;</span><span>,</span><span> </span><span>&quot;bear&quot;</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>0.2</span><span>,</span><span> </span><span>0.3</span><span>,</span><span> </span><span>0.5</span><span>))</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>diseased</span><span> </span><span>=</span><span> </span><span>case_when</span><span>(</span><span>
</span><span>animal_</span><span> </span><span>==</span><span> </span><span>&quot;lion&quot;</span><span> </span><span>~</span><span> </span><span>sample</span><span>(</span><span>0</span><span>:</span><span>1</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>0.5</span><span>,</span><span> </span><span>0.5</span><span>)),</span><span>
</span><span>animal_</span><span> </span><span>==</span><span> </span><span>&quot;tiger&quot;</span><span> </span><span>~</span><span> </span><span>sample</span><span>(</span><span>0</span><span>:</span><span>1</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>0.75</span><span>,</span><span> </span><span>0.25</span><span>)),</span><span>
</span><span>animal_</span><span> </span><span>==</span><span> </span><span>&quot;bear&quot;</span><span> </span><span>~</span><span> </span><span>sample</span><span>(</span><span>0</span><span>:</span><span>1</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>0.9</span><span>,</span><span> </span><span>0.1</span><span>))</span><span>
</span><span>))</span><span> </span><span>true_props</span><span> </span><span>&lt;-</span><span> </span><span>data</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>animal_</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>diseased</span><span> </span><span>=</span><span> </span><span>round</span><span>(</span><span>mean</span><span>(</span><span>diseased</span><span>),</span><span> </span><span>2</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>odds</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;1:9&quot;</span><span>,</span><span> </span><span>&quot;1:1&quot;</span><span>,</span><span> </span><span>&quot;1:3&quot;</span><span>),</span><span>
</span><span>lab</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;Probability: &quot;</span><span>,</span><span> </span><span>diseased</span><span>,</span><span> </span><span>&quot;\nOdds: &quot;</span><span>,</span><span> </span><span>odds</span><span>))</span><span>
</span><span>ggplot</span><span>(</span><span>data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>as.logical</span><span>(</span><span>diseased</span><span>),</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>animal_</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>geom_text</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>true_props</span><span>,</span><span> </span><span>aes</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>lab</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>diseased</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;white&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;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Proportion&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;Diseased or not:&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>reverse</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>+</span><span>
</span><span>coord_flip</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Simulated data for demonstrating odds and risk ratios&quot;</span><span>,</span><span>
</span><span>&quot;Risk ratio: tiger / bear = 2.5, lion / bear = 5.0\nOdds ratio: tiger / bear = 3.0, lion / bear = 9.0&quot;</span><span>)</span></code></pre>
</figure>
<h2>Obtaining odds and risk ratios from a generalized linear model</h2>
<p>Putting that aside, how do we get these estimates from a model?</p>
<h3>Odds ratios</h3>
<p>Frankly, I suspect the more material reason for the prevalence of odds ratios is that they fall easily out of the results of a logistic regression (generalized linear model with the canonical logit link function relating the mean of the response to the linear predictor – where the logit function is the logarithm of the odds). For a categorical explanatory variable that has been represented as dummy indicator variables, <em>e</em> to the power of the coefficient for the dummy variable will give an estimate of the odds ratio.</p>
<p>Here’s an example of extracting estimates of odds ratios from our data:</p>
<figure>
<pre><code data-lang="r"><span># Odds ratio:
</span><span>model_2</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>diseased</span><span> </span><span>~</span><span> </span><span>animal_</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasibinomial</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>data</span><span>)</span><span>
</span><span>exp</span><span>(</span><span>coef</span><span>(</span><span>model_2</span><span>))[</span><span>-1</span><span>]</span><span>
</span><span># animal_lion animal_tiger # 9.286664 2.997837 </span><span>#</span></code></pre>
</figure>
<p>These odds ratios are of the given animal (Lion or Tiger) relative to the disease rate of the reference level, which in this case is Bear. So these are estimates of the ratios depicted in the original diagram.</p>
<p>You would get the same point estimate if you used <code>family = binomial</code>, or <code>family = quasi(link = 'logit', variance = &quot;mu(1-mu)&quot;)</code>; it’s the logit link that’s important here.</p>
<p>Note that these estimates are biased; despite being based on large samples of 100,000 animals they haven’t converged on the true odds ratios of 9 and 3. The estimates of the original coefficients are unbiased; but the non-linear transformation of <code>exp(coef(model))</code> inevitably introduces bias. A statistic that is unbiased on one scale will be biased if you transform it to another scale; such is life.</p>
<p>Using <code>binomial</code> versus <code>quasibinomial</code> does make a difference to confidence intervals, but in our current case it’s not material. The confidence intervals at least contain the correct values; although the point estimate is biased (and also not in the centre of the confidence interval, again due to the non-linear transformation), the confidence interval is still valid for the transformed scale:</p>
<figure>
<pre><code data-lang="r"><span>exp</span><span>(</span><span>confint</span><span>(</span><span>model_2</span><span>))[</span><span>-1</span><span>,</span><span> </span><span>]</span><span>
</span><span># 2.5 % 97.5 %
# animal_lion 8.920358 9.669207
# animal_tiger 2.881889 3.118685
</span><span>#</span></code></pre>
</figure>
<h3>Risk ratios</h3>
<p>At a minimum, the <em>only</em> change that needs to be done to get risk ratios is to change the link function that relates the mean value of the response variable to the linear predictor. For estimates of odds ratios, this is logit (ie the logarithm of the odds of the mean); for estimates of relative risk ratios, this becomes logarithm. We can specify this manually, or just use a built-in family for our generalized linear model for which the logarithm is the canonical link fucntion, and hence the default. The <code>quasipoisson</code> is a great choice here:</p>
<figure>
<pre><code data-lang="r"><span># Risk ratio:
</span><span>model_1</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>diseased</span><span> </span><span>~</span><span> </span><span>animal_</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>quasipoisson</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>data</span><span>)</span><span>
</span><span>exp</span><span>(</span><span>coef</span><span>(</span><span>model_1</span><span>))[</span><span>-1</span><span>]</span><span>
</span><span># animal_lion animal_tiger # 5.112507 2.504792 </span><span>#</span></code></pre>
</figure>
<p>Again, these point estimates are biased because of the non-linear <code>exp()</code> transformation of the unbiased original coefficients. But the confidence intervals contain the correct values (which recall are 5.0 and 2.5):</p>
<figure>
<pre><code data-lang="r"><span>exp</span><span>(</span><span>confint</span><span>(</span><span>model_1</span><span>))[</span><span>-1</span><span>,</span><span> </span><span>]</span><span>
</span><span># 2.5 % 97.5 %
# animal_lion 4.961963 5.268162
# animal_tiger 2.426520 2.585758
</span><span>#</span></code></pre>
</figure>
<p>If we’d used <code>poisson</code> as our family instead of <code>quasipoisson</code>, the dispersion parameter is forced to be 1.0, instead of 0.7 which it is estimated to be by the <code>quasipoisson</code> model. The net effect of that “underdispersion” in the data in this case is that the confidence intervals with <code>family = poisson</code> are <em>larger</em> than in the <code>quasipoisson</code> instance. This won’t always apply, but it often will when we are modelling data such as this which is, in fact under-dispersed compared to a pure poisson distribution (because none of the counts can exceed 1).</p>
<p>These examples have avoided complications of other explanatory variables, but the point of using a generalized linear model for this (rather than observing proportions directly) is that we can add in other variables and report on relative risks (or odds, if we must) “while controlling for …” those other variables.</p>
<p>The approach also <a rel="nofollow" href="http://grokbase.com/t/r/r-help/109dad6km5/r-relative-risk-regression-with-survey-data" target="_blank">nicely extends to complex surveys</a>; we can use <code>svyglm()</code> from Lumley’s <code>survey</code> package, with any of the <code>family</code> specifications above, according to our needs and get good results.</p>
<h2>Summary</h2>
<p>So, key points;</p>
<ul>
<li>carefully choose which (or both) of odds or risk ratios to use</li>
<li>explicitly communicate what you are reporting, and try to counter in your communication the likely misunderstandings particularly with odds ratios</li>
<li><code>exp(coef(model))</code> for a glm fit with <code>family = quasibinomial</code> will give odds ratios</li>
<li><code>exp(coef(model))</code> for a glm fit with <code>family = quasipoisson</code> will give relative risk ratios</li>
<li>remember that point estimates from this approach are biased, and like all point estimates give an undue impression of precision; always include a confidence interval or credibility interval as well.</li>
</ul>
	<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/17/risk-ratios"> 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/HA7iPhqWxP8" height="1"></a></div>