<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-hidden-process-behind-binary-or-other-categorical-outcomes/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.rdatagen.net/post/ordinal-regression/"> ouR data generation</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I was thinking a lot about proportional-odds cumulative logit models last fall while designing a study to evaluate an intervention’s effect on meat consumption. After a fairly extensive pilot study, we had determined that participants can have quite a difficult time recalling precise quantities of meat consumption, so we were forced to move to a categorical response. (This was somewhat unfortunate, because we would not have continuous or even count outcomes, and as a result, might not be able to pick up small changes in behavior.) We opted for a question that was based on 30-day meat consumption: none, 1-3 times per month, 1 time per week, etc. – six groups in total. The question was how best to evaluate effectiveness of the intervention?</p>
<p>Since the outcome was categorical <em>and</em> ordinal – that is category 1 implied less meat consumption that category 2, category 2 implied less consumption that category 3, and so on – a model that estimates the cumulative probability of ordinal outcomes seemed like a possible way to proceed. Cumulative logit models estimate a number of parameters that represent the cumulative log-odds of an outcome; the parameters are the log-odds of categories 2 through 6 versus category 1, categories 3 through 6 versus 1 &amp; 2, etc. Maybe not the most intuitive way to interpret the data, but seems to plausibly fit the data generating process.</p>
<p>I was concerned about the proportionality assumption of the cumulative logit model, particularly when we started to consider adjusting for baseline characteristics (more on that in the next post). I looked more closely at the data generating assumptions of the cumulative logit model, which are quite frequently framed in the context of a continuous latent measure that follows a logistic distribution. I thought I’d describe that data generating process here to give an alternative view of discrete data models.</p>
<p>I know I have been describing a context that includes an outcome with multiple categories, but in this post I will focus on regular logistic regression with a binary outcome. This will hopefully allow me to establish the idea of a latent threshold. I think it will be useful to explain this simpler case first before moving on to the more involved case of an ordinal response variable, which I plan to tackle in the near future.</p>
<div>
<h3>A latent continuous process underlies the observed binary process</h3>
<p>For an event with a binary outcome (true or false, A or B, 0 or 1), the observed outcome may, at least in some cases, be conceived as the manifestation of an unseen, latent continuous outcome. In this conception, the observed (binary) outcome merely reflects whether or not the unseen continuous outcome has exceeded a specified threshold. Think of this threshold as a tipping point, above which the observable characteristic takes on one value (say false), below which it takes on a second value (say true).</p>
</div>
<div>
<h3>The logistic distribution</h3>
<p>Logistic regression models are used to estimate relationships of individual characteristics with categorical outcomes. The name of this regression model arises from the logistic distribution, which is a symmetrical continuous distribution. In a latent (or hidden) variable framework, the underlying, unobserved continuous measure is drawn from this logistic distribution. More specifically, the standard logistic distribution is typically assumed, with a location parameter of 0, and a scale parameter of 1. (The mean of this distribution is 0 and variance is approximately 3.29.)</p>
<p>Here is a plot of a logistic pdf, shown in relation to a standard normal pdf (with mean 0 and variance 1):</p>
<pre><code>library(ggplot2)
library(data.table)

my_theme &lt;- function() {
  theme(panel.background = element_rect(fill = &quot;grey90&quot;), 
        panel.grid = element_blank(), 
        axis.ticks = element_line(colour = &quot;black&quot;), 
        panel.spacing = unit(0.25, &quot;lines&quot;), 
        plot.title = element_text(size = 12, vjust = 0.5, hjust = 0), 
        panel.border = element_rect(fill = NA, colour = &quot;gray90&quot;))
}

x &lt;- seq(-6, 6, length = 1000)
yNorm &lt;- dnorm(x, 0, 1) 
yLogis &lt;- dlogis(x, location = 0, scale = 1)

dt &lt;- data.table(x, yNorm, yLogis)
dtm &lt;- melt(dt, id.vars = &quot;x&quot;, value.name = &quot;Density&quot;)

ggplot(data = dtm) +
  geom_line(aes(x = x, y = Density, color = variable)) +
  geom_hline(yintercept = 0, color = &quot;grey50&quot;) +
  my_theme() +
  scale_color_manual(values = c(&quot;red&quot;, &quot;black&quot;), 
                     labels=c(&quot;Normal&quot;, &quot;Logistic&quot;)) +
  theme(legend.position = c(0.8, 0.6),
        legend.title = element_blank())</code></pre>
<p><img src="https://i0.wp.com/www.rdatagen.net/post/2017-08-23-ordinal-regression_files/figure-html/unnamed-chunk-1-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h3>The threshold defines the probability</h3>
<p>Below, I have plotted the standardized logistic pdf with a threshold that defines a tipping point for a particular Group A. In this case the threshold is 1.5, so for everyone with a unseen value of <span>\(X &lt; 1.5\)</span>, the observed binary outcome <span>\(Y\)</span> will be 1. For those where <span>\(X \geq 1.5\)</span>, the observed binary outcome <span>\(Y\)</span> will be 0:</p>
<pre><code>xGrpA &lt;- 1.5

ggplot(data = dtm[variable == &quot;yLogis&quot;], aes(x = x, y = Density)) +
  geom_line() +
  geom_segment(x = xGrpA, y = 0, xend = xGrpA, yend = dlogis(xGrpA), lty = 2) +
  geom_area(mapping = aes(ifelse(x &lt; xGrpA, x, xGrpA)), fill = &quot;white&quot;) +
  geom_hline(yintercept = 0, color = &quot;grey50&quot;) +
  ylim(0, 0.3) +
  my_theme() +
  scale_x_continuous(breaks = c(-6, -3, 0, xGrpA, 3, 6))</code></pre>
<p><img src="https://i0.wp.com/www.rdatagen.net/post/2017-08-23-ordinal-regression_files/figure-html/unnamed-chunk-2-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>Since we have plot a probability density (pdf), the area under the entire curve is equal to 1. We are interested in the binary outcome <span>\(Y\)</span> defined by the threshold, so we can say that the area below the curve to the left of threshold (filled in white) represents <span>\(P(Y = 1|Group=A)\)</span>. The remaining area represents <span>\(P(Y = 0|Group=A)\)</span>. The area to the left of the threshold can be calculated in <code>R</code> using the <code>plogis</code> function:</p>
<pre><code>(p_A &lt;- plogis(xGrpA))</code></pre>
<pre><code>## [1] 0.8175745</code></pre>
<p>Here is the plot for a second group that has a threshold of 2.2:</p>
<p><img src="https://i0.wp.com/www.rdatagen.net/post/2017-08-23-ordinal-regression_files/figure-html/unnamed-chunk-4-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>The area under the curve to the left of the threshold is <span>\(P(X &lt; 2.2)\)</span>, which is also <span>\(P(Y = 1 | Group=B)\)</span>:</p>
<pre><code>(p_B &lt;- plogis(xGrpB))</code></pre>
<pre><code>## [1] 0.9002495</code></pre>
</div>
<div>
<h3>Log-odds and probability</h3>
<p>In logistic regression, we are actually estimating the log-odds of an outcome, which can be written as</p>
<p><span>\[log \left[ \frac{P(Y=1)}{P(Y=0)} \right]\]</span>.</p>
<p>In the case of Group A, log-odds of Y being equal to 1 is</p>
<pre><code>(logodds_A &lt;- log(p_A / (1 - p_A) ))</code></pre>
<pre><code>## [1] 1.5</code></pre>
<p>And for Group B,</p>
<pre><code>(logodds_B &lt;- log(p_B / (1 - p_B) ))</code></pre>
<pre><code>## [1] 2.2</code></pre>
<p>As you may have noticed, we’ve recovered the thresholds that we used to define the probabilities for the two groups. The threshold is actually the log-odds for a particular group.</p>
</div>
<div>
<h3>Logistic regression</h3>
<p>The logistic regression model that estimates the log-odds for each group can be written as</p>
<p><span>\[log \left[ \frac{P(Y=1)}{P(Y=0)} \right] = B_0 + B_1 * I(Grp = B) \quad ,\]</span></p>
<p>where <span>\(B_0\)</span> represents the threshold for Group A and <span>\(B_1\)</span> represents the shift in the threshold for Group B. In our example, the threshold for Group B is 0.7 units (2.2 – 1.5) to the right of the threshold for Group A. If we generate data for both groups, our estimates for <span>\(B_0\)</span> and <span>\(B_1\)</span> should be close to 1.5 and 0.7, respectively</p>
</div>
<div>
<h3>The process in action</h3>
<p>To put this all together in a simulated data generating process, we can see the direct link with the logistic distribution, the binary outcomes, and an interpretation of estimates from a logistic model. The only stochastic part of this simulation is the generation of continuous outcomes from a logistic distribution. Everything else follows from the pre-defined group assignments and the group-specific thresholds:</p>
<pre><code>n = 5000
set.seed(999)

# Stochastic step

xlatent &lt;- rlogis(n, location = 0, scale = 1)

# Deterministic part

grp &lt;- rep(c(&quot;A&quot;,&quot;B&quot;), each = n / 2)

dt &lt;- data.table(id = 1:n, grp, xlatent, y = 0)
dt[grp == &quot;A&quot; &amp; xlatent &lt;= xGrpA, y := 1]
dt[grp == &quot;B&quot; &amp; xlatent &lt;= xGrpB, y := 1]

# Look at the data

dt</code></pre>
<pre><code>##         id grp    xlatent y
##    1:    1   A -0.4512173 1
##    2:    2   A  0.3353507 1
##    3:    3   A -2.2579527 1
##    4:    4   A  1.7553890 0
##    5:    5   A  1.3054260 1
##   ---                      
## 4996: 4996   B -0.2574943 1
## 4997: 4997   B -0.9928283 1
## 4998: 4998   B -0.7297179 1
## 4999: 4999   B -1.6430344 1
## 5000: 5000   B  3.1379593 0</code></pre>
<p>The probability of a “successful” outcome (i.e <span>\(P(Y = 1\)</span>)) for each group based on this data generating process is pretty much equal to the areas under the respective densities to the left of threshold used to define success:</p>
<pre><code>dt[, round(mean(y), 2), keyby = grp]</code></pre>
<pre><code>##    grp   V1
## 1:   A 0.82
## 2:   B 0.90</code></pre>
<p>Now let’s estimate a logistic regression model:</p>
<pre><code>library(broom)
glmfit &lt;- glm(y ~ grp, data = dt, family = &quot;binomial&quot;)
tidy(glmfit, quick = TRUE)</code></pre>
<pre><code>##          term  estimate
## 1 (Intercept) 1.5217770
## 2        grpB 0.6888526</code></pre>
<p>The estimates from the model recover the logistic distribution thresholds for each group. The Group A threshold is estimated to be 1.52 (the intercept) and the Group B threshold is estimated to be 2.21 (intercept + grpB parameter). These estimates can be interpreted as the log-odds of success for each group, but also <em>as the threshold for the underlying continuous data generating process that determines the binary outcome <span>\(Y\)</span></em>. And we can interpret the parameter for <code>grpB</code> in the traditional way as the log-odds ratio comparing the log-odds of success for Group B with the log-odds of success for Group A, or <em>as the shift in the logistic threshold for Group A to the logistic threshold for Group B</em>.</p>
<p>In the next week or so, I will extend this to a discussion of an ordinal categorical outcome. I think the idea of shifting the thresholds underscores the proportionality assumption I alluded to earlier …</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/ordinal-regression/"> 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/-Hhy_lnLJiI" height="1"></a></div>