<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/adding-polished-significance-summaries-to-papers-using-r/"></div></div><div><a data-url="https://www.r-bloggers.com/adding-polished-significance-summaries-to-papers-using-r/" data-counturl="https://www.r-bloggers.com/adding-polished-significance-summaries-to-papers-using-r/" data-count="horizontal" data-text="Adding polished significance summaries to papers using R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2016/10/adding-polished-significance-summaries-to-papers-using-r/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>When we teach “<a target="_blank" href="https://cran.r-project.org/"><code>R</code></a> for statistics” to groups of scientists (who tend to be quite well informed in statistics, and just need a bit of help with R) we take the time to re-work some tests of model quality with the appropriate significance tests. We organize the lesson in terms of a larger and more detailed version of the following list:</p>
<ul>
<li>To test the quality of a numeric model to numeric outcome: F-test (as in linear regression).</li>
<li>To test the quality of a numeric model to a categorical outcome: χ<sup>2</sup> or “Chi-squared” test (as in logistic regression).</li>
<li>To test the association of a categorical predictor to a categorical outcome: many tests including Fisher’s exact test and Barnard’s test.</li>
<li>To test the quality of a categorical predictor to a numeric outcome: t-Test, ANOVA, and Tukey’s “honest significant difference” test.</li>
</ul>
<p>The above tests are all in terms of checking model results, so we don’t allow re-scaling of the predictor as part of the test (as we would have in a Pearson correlation test, or an area under the curve test). There are, of course, many alternatives such as Wald’s test- but we try to start with a set of tests that are standard, well known, and well reported by <code>R</code>. An odd exception has always been the χ<sup>2</sup> test, which we will write a bit about in this note.<span></span></p>
<p>
<p>The χ<sup>2</sup> test is a very useful statistical test. In particular, under fairly mild assumptions, it is a usable probability model for the quality of fit of a logistic regression. It is based on a summary statistics called “deviance” (an odd name, but the quantity is strongly related to likelihood and to entropy). And, after a simple transform, it yields a quantity called “pseudo-R<sup>2</sup>” (see <a target="_blank" href="http://www.win-vector.com/blog/2011/09/the-simpler-derivation-of-logistic-regression/">The Simpler Derivation of Logistic Regression</a>) that reads like “fraction of variation explained.” It is great final test for well-tuned models designed to estimate probabilities (just as area under the curve is a good early test as it abstracts out scale and choice of decision threshold).</p>
<p>Yet the χ<sup>2</sup> test is under-emphasized and under-implemented in R. Consider the following trivial logistic regression model.</p>
<p><code>
<pre>
d &lt;- data.frame(x=c(1,2,3,4,5,6,7,7),
       y=c(TRUE,FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE))
model &lt;- glm(y~x,data=d,family=binomial)
summary(model)

## Call:
## glm(formula = y ~ x, family = binomial, data = d)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -1.37180  -1.09714  -0.00811   1.08024   1.42939  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  -0.7455     1.6672  -0.447    0.655
## x             0.1702     0.3429   0.496    0.620
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 11.090  on 7  degrees of freedom
## Residual deviance: 10.837  on 6  degrees of freedom
## AIC: 14.837
## 
## Number of Fisher Scoring iterations: 4
</pre>
<p></p>
<p>Notice that <code>R</code> reported the change in deviance (the measure of model quality, also note the AIC or Akaike information criteria is present), but no significance on the overall model fit is reported (n.b. this is different from coefficient significances). The significance is probably not there because <code>glm()</code> is a fully general generalized linear model fitter (it can fit much more than just logistic regressions) and the error model likely changes as you change the model type (controlled by the <code>family</code> and <code>link</code> controls).</p>
<p>But logistic regression really deserves to be front and center. This is why in Section 7.2 of <a target="_blank" href="http://www.practicaldatascience.com/"><em>Practical Data Science with R</em>, Nina Zumel, John Mount, Manning 2014</a> we take the time to define and show how to calculate the pseudo-R<sup>2</sup> and the significance for the reported deviance.</p>
<p>As we observed in <a target="_blank" href="http://www.win-vector.com/blog/2016/10/proofing-statistics-in-papers/">“Proofing statistics in papers”</a> having standard tests and standard reporting of tests is a great advantage. In this spirit we add an “APA-like” report of the χ<sup>2</sup> significance in our <code>sigr</code> package. The use is quick and decisive:</p>
<p><code>
<pre>
library('sigr') 
cat(formatChiSqTestFromModel(model,pLargeCutoff=1,format='html')$formatStr)
</pre>
<p><br>
<b>Chi-Square Test</b> summary: <i>pseudo-<i>R<sup>2</sup></i></i>=0.023 (<i>χ<sup>2</sup></i>(1,<i>N</i>=8)=0.25, <i>p</i>=0.615).</p>
<p>The <a target="_blank" href="https://github.com/WinVector/sigr"><code>sigr</code> package</a> isn’t up on CRAN, but can be installed using <code>devtools::install_github('WinVector/sigr')</code>. It includes documentation, and an example vignette. <code>sigr</code> can format directly to HTML, Latex, Markdown, and Word. It designed to be used with a <code>knitr</code> workflow, and when used with <code>knit</code> will automatically select the correct target formatting. Right now it generates reports for only linear and logistic regressions; we will likely fill out with a few more “most ofter used, so should have a nice neat format” tests going forward.</p>

<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/adding-polished-significance-summaries-to-papers-using-r/"></div></div><div><a data-url="https://www.r-bloggers.com/adding-polished-significance-summaries-to-papers-using-r/" data-counturl="https://www.r-bloggers.com/adding-polished-significance-summaries-to-papers-using-r/" data-count="vertical" data-text="Adding polished significance summaries to papers using R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>

<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2016/10/adding-polished-significance-summaries-to-papers-using-r/"> R – Win-Vector Blog</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/3Rtsd9wCiLM"></a></div></code></code>