{"id":739,"date":"2016-10-22T10:16:15","date_gmt":"2016-10-22T14:16:15","guid":{"rendered":"http:\/\/www.clayford.net\/statistics\/?p=739"},"modified":"2020-09-13T10:17:56","modified_gmt":"2020-09-13T14:17:56","slug":"profile-likelihood-ratio-confidence-intervals","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/profile-likelihood-ratio-confidence-intervals\/","title":{"rendered":"Profile likelihood ratio confidence intervals"},"content":{"rendered":"<p>When you fit a generalized linear model (GLM) in R and call <code>confint<\/code> on the model object, you get confidence intervals for the model coefficients. But you also get an interesting message:<\/p>\n<p><code>Waiting for profiling to be done...<\/code><\/p>\n<p>What&#39;s that all about? What exactly is being profiled? Put simply, it&#39;s telling you that it&#39;s calculating a <em>profile likelihood ratio confidence interval<\/em>.<\/p>\n<p>The typical way to calculate a 95% confidence interval is to multiply the standard error of an estimate by some normal quantile such as 1.96 and add\/subtract that product to\/from the estimate to get an interval. In the context of GLMs, we sometimes call that a Wald confidence interval. <\/p>\n<p>Another way to determine an upper and lower bound of plausible values for a model coefficient is to find the minimum and maximum value of the set of all coefficients that satisfy the following:<\/p>\n<p>\\[-2\\log\\left(\\frac{L(\\beta_{0}, \\beta_{1}|y_{1},&hellip;,y_{n})}{L(\\hat{\\beta_{0}}, \\hat{\\beta_{1}}|y_{1},&hellip;,y_{n})}\\right) < \\chi_{1,1-\\alpha}^{2}\\]<\/p>\n<p>Inside the parentheses is a ratio of <em>likelihoods<\/em>. In the denominator is the likelihood of the model we fit. In the numerator is the likelihood of the same model but with <em>different coefficients<\/em>. (More on that in a moment.) We take the log of the ratio and multiply by -2. This gives us a likelihood ratio test (LRT) statistic. This statistic is typically used to test whether a coefficient is equal to some value, such as 0, with the null likelihood in the numerator (model without coefficient, that is, equal to 0) and the alternative or estimated likelihood in the denominator (model with coefficient). If the LRT statistic is less than \\(\\chi_{1,0.95}^{2} \\approx 3.84\\), we fail to reject the null. The coefficient is statisically not much different from 0. That means the likelihood ratio is close to 1. The likelihood of the model without the coefficient is almost as high the model with it. On the other hand, if the ratio is small, that means the likelihood of the model without the coefficient is much smaller than the likelihood of the model with the coefficient. This leads to a larger LRT statistic since it&#39;s being log transformed, which leads to a value larger than 3.84 and thus rejection of the null.<\/p>\n<p>Now in the formula above, we are seeking all such coefficients in the numerator that would make it a true statement. You might say we&#39;re &ldquo;profiling&rdquo; many different null values and their respective LRT test statistics. <em>Do they fit the profile of a plausible coefficient value in our model?<\/em> The smallest value we can get without violating the condition becomes our lower bound, and likewise with the largest value. When we&#39;re done we&#39;ll have a range of plausible values for our model coefficient that gives us some indication of the uncertainly of our estimate. <\/p>\n<p>Let&#39;s load some data and fit a binomial GLM to illustrate these concepts. The following R code comes from the help page for <code>confint.glm<\/code>. This is an example from the classic <a href=\"http:\/\/www.stats.ox.ac.uk\/pub\/MASS4\/\">Modern Applied Statistics with S<\/a>. <code>ldose<\/code> is a dosing level and <code>sex<\/code> is self-explanatory. <code>SF<\/code> is number of successes and failures, where success is number of dead worms. We&#39;re interested in learning about the effects of dosing level and sex on number of worms killed. Presumably this worm is a pest of some sort.<\/p>\n<pre><code class=\"r\"># example from Venables and Ripley (2002, pp. 190-2.)\r\nldose &lt;- rep(0:5, 2)\r\nnumdead &lt;- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)\r\nsex &lt;- factor(rep(c(&quot;M&quot;, &quot;F&quot;), c(6, 6)))\r\nSF &lt;- cbind(numdead, numalive = 20-numdead)\r\nbudworm.lg &lt;- glm(SF ~ sex + ldose, family = binomial)\r\nsummary(budworm.lg)\r\n<\/code><\/pre>\n<pre>## \r\n## Call:\r\n## glm(formula = SF ~ sex + ldose, family = binomial)\r\n## \r\n## Deviance Residuals: \r\n##      Min        1Q    Median        3Q       Max  \r\n## -1.10540  -0.65343  -0.02225   0.48471   1.42944  \r\n## \r\n## Coefficients:\r\n##             Estimate Std. Error z value Pr(&gt;|z|)    \r\n## (Intercept)  -3.4732     0.4685  -7.413 1.23e-13 ***\r\n## sexM          1.1007     0.3558   3.093  0.00198 ** \r\n## ldose         1.0642     0.1311   8.119 4.70e-16 ***\r\n## ---\r\n## Signif. codes:  0 &#39;***&#39; 0.001 &#39;**&#39; 0.01 &#39;*&#39; 0.05 &#39;.&#39; 0.1 &#39; &#39; 1\r\n## \r\n## (Dispersion parameter for binomial family taken to be 1)\r\n## \r\n##     Null deviance: 124.8756  on 11  degrees of freedom\r\n## Residual deviance:   6.7571  on  9  degrees of freedom\r\n## AIC: 42.867\r\n## \r\n## Number of Fisher Scoring iterations: 4\r\n<\/pre>\n<p>The coefficient for <code>ldose<\/code> looks significant. Let&#39;s determine a confidence interval for the coefficient using the <code>confint<\/code> function. We call <code>confint<\/code> on our model object, <code>budworm.lg<\/code> and use the <code>parm<\/code> argument to specify that we only want to do it for <code>ldose<\/code>:<\/p>\n<pre><code class=\"r\">confint(budworm.lg, parm = &quot;ldose&quot;)\r\n<\/code><\/pre>\n<pre>## Waiting for profiling to be done...\r\n<\/pre>\n<pre>##     2.5 %    97.5 % \r\n## 0.8228708 1.3390581\r\n<\/pre>\n<p>We get our &ldquo;waiting&rdquo; message though there really was no wait. If we fit a larger model and request multiple confidence intervals, then there might actually be a waiting period of a few seconds. The lower bound is about 0.8 and the upper bound about 1.32. We might say every increase in dosing level increase the log odds of killing worms by at least 0.8. We could also exponentiate to get a CI for an odds ratio estimate:<\/p>\n<pre><code class=\"r\">exp(confint(budworm.lg, parm = &quot;ldose&quot;))\r\n<\/code><\/pre>\n<pre>## Waiting for profiling to be done...\r\n<\/pre>\n<pre>##    2.5 %   97.5 % \r\n## 2.277027 3.815448\r\n<\/pre>\n<p>The odds of &ldquo;success&rdquo; (killing worms) is at least 2.3 times higher at one dosing level versus the next lower dosing level.<\/p>\n<p>To better understand the profile likelihood ratio confidence interval, let&#39;s do it &ldquo;manually&rdquo;. Recall the denominator in the formula above was the likelihood of our fitted model. We can extract that with the <code>logLik<\/code> function:<\/p>\n<pre><code class=\"r\">den &lt;- logLik(budworm.lg)\r\nden\r\n<\/code><\/pre>\n<pre>## &#39;log Lik.&#39; -18.43373 (df=3)\r\n<\/pre>\n<p>The numerator was the likelihood of a model with a <em>different<\/em> coefficient. Here&#39;s the likelihood of a model with a coefficient of 1.05:<\/p>\n<pre><code class=\"r\">num &lt;- logLik(glm(SF ~ sex + offset(1.05*ldose), family = binomial))\r\nnum\r\n<\/code><\/pre>\n<pre>## &#39;log Lik.&#39; -18.43965 (df=2)\r\n<\/pre>\n<p>Notice we used the <code>offset<\/code> function. That allows us to fix the coefficient to 1.05 and not have it estimated.<\/p>\n<p>Since we already extracted the <em>log<\/em> likelihoods, we need to subtract them. Remember this rule from algebra?<\/p>\n<p>\\[\\log\\frac{M}{N} = \\log M &#8211; \\log N\\]<\/p>\n<p>So we subtract the denominator from the numerator, multiply by -2, and check if it&#39;s less than 3.84, which we calculate with <code>qchisq(p = 0.95, df = 1)<\/code><\/p>\n<pre><code class=\"r\">-2*(num - den)\r\n<\/code><\/pre>\n<pre>## &#39;log Lik.&#39; 0.01184421 (df=2)\r\n<\/pre>\n<pre><code class=\"r\">-2*(num - den) &lt; qchisq(p = 0.95, df = 1)\r\n<\/code><\/pre>\n<pre>## [1] TRUE\r\n<\/pre>\n<p>It is. 1.05 seems like a plausible value for the <code>ldose<\/code> coefficient. That makes sense since the estimated value was 1.0642. Let&#39;s try it with a larger value, like 1.5:<\/p>\n<pre><code class=\"r\">num &lt;- logLik(glm(SF ~ sex + offset(1.5*ldose), family = binomial))\r\n-2*(num - den) &lt; qchisq(p = 0.95, df = 1)\r\n<\/code><\/pre>\n<pre>## [1] FALSE\r\n<\/pre>\n<p>FALSE. 1.5 seems too big to be a plausible value for the <code>ldose<\/code> coefficient. <\/p>\n<p>Now that we have the general idea, we can program a <code>while<\/code> loop to check different values until we exceed our threshold of 3.84. <\/p>\n<pre><code class=\"r\">cf &lt;- budworm.lg$coefficients[3]  # fitted coefficient 1.0642\r\ncut &lt;- qchisq(p = 0.95, df = 1)   # about 3.84\r\ne &lt;- 0.001                        # increment to add to coefficient\r\nLR &lt;- 0                           # to kick start our while loop \r\nwhile(LR &lt; cut){\r\n  cf &lt;- cf + e\r\n  num &lt;- logLik(glm(SF ~ sex + offset(cf*ldose), family = binomial))\r\n  LR &lt;- -2*(num - den)\r\n}\r\n(upper &lt;- cf)\r\n<\/code><\/pre>\n<pre>##    ldose \r\n## 1.339214\r\n<\/pre>\n<p>To begin we save the original coefficient to <code>cf<\/code>, store the cutoff value to <code>cut<\/code>, define our increment of 0.001 as <code>e<\/code>, and set <code>LR<\/code> to an initial value of 0. In the loop we increment our coefficient estimate which is used in the <code>offset<\/code> function in the estimation step. There we extract the log likelihood and then calculate <code>LR<\/code>. If <code>LR<\/code> is less than <code>cut<\/code> (3.84), the loop starts again with a new coefficient that is 0.001 higher. We see that our upper bound of 1.339214 is very close to what we got above using <code>confint<\/code> (1.3390581). If we set <code>e<\/code> to smaller values we&#39;ll get closer.<\/p>\n<p>We can find the LR profile lower bound in a similar way. Instead of adding the increment we subtract it:<\/p>\n<pre><code class=\"r\">cf &lt;- budworm.lg$coefficients[3]  # reset cf\r\nLR &lt;- 0                           # reset LR \r\nwhile(LR &lt; cut){\r\n  cf &lt;- cf - e\r\n  num &lt;- logLik(glm(SF ~ sex + offset(cf*ldose), family = binomial))\r\n  LR &lt;- -2*(num - den)\r\n}\r\n(lower &lt;- cf)\r\n<\/code><\/pre>\n<pre>##    ldose \r\n## 0.822214\r\n<\/pre>\n<p>The result, 0.822214, is very close to the lower bound we got from <code>confint<\/code> (0.8228708).<\/p>\n<p>This is a <em>very<\/em> basic implementation of calculating a likelihood ratio confidence interval. It is only meant to give a general sense of what&#39;s happening when you see that message <code>Waiting for profiling to be done...<\/code>. I hope you found it helpful. To see how R does it, enter <code>getAnywhere(profile.glm)<\/code> in the console and inspect the code. It&#39;s not for the faint of heart.<\/p>\n<p>I have to mention the book <a href=\"https:\/\/www.amazon.com\/Analysis-Categorical-Chapman-Statistical-Science\/dp\/1439855676\">Analysis of Categorical Data with R<\/a>, from which I gained a better understanding of the material in this post. The authors have kindly shared their R code at the following web site if you want to have a look: <a href=\"http:\/\/www.chrisbilder.com\/categorical\/\">http:\/\/www.chrisbilder.com\/categorical\/<\/a><\/p>\n<p>To see how they &ldquo;manually&rdquo; calculate likelihood ratio confidence intervals, go to the following R script and see the section &ldquo;Examples of how to find profile likelihood ratio intervals without confint()&rdquo;: <a href=\"http:\/\/www.chrisbilder.com\/categorical\/Chapter2\/Placekick.R\">http:\/\/www.chrisbilder.com\/categorical\/Chapter2\/Placekick.R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you fit a generalized linear model (GLM) in R and call confint on the model object, you get confidence&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/profile-likelihood-ratio-confidence-intervals\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,7,12,13],"tags":[54,64,33],"class_list":["post-739","post","type-post","status-publish","format-standard","hentry","category-hypothesis-testing","category-probability","category-regression","category-using-r","tag-glm","tag-likelihood-profile","tag-logistic-regression"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=739"}],"version-history":[{"count":2,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/739\/revisions"}],"predecessor-version":[{"id":742,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/739\/revisions\/742"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}