<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/chi-squared-test/"></div></div><div><a data-url="https://www.r-bloggers.com/chi-squared-test/" data-counturl="https://www.r-bloggers.com/chi-squared-test/" data-count="horizontal" data-text="Chi-Squared Test" 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://datascienceplus.com/chi-squared-test-in-r/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Before we build stats/machine learning models, it is a good practice to understand which predictors are significant and have an impact on the response variable. </p>
<p>In this post we deal with a particular case when both your response and predictor are categorical variables.</p>
<p>By the end of this you’d have gained an understanding of what predictive modelling is and what the significance and purpose of chi-square statistic is. We will go through a hypothetical case study to understand the math behind it. We will actually implement a chi-squared test in R and learn to interpret the results. Finally you’ll be solving a mini challenge before we discuss the answers.</p>
<blockquote><ul>
<li> Background Knowledge </li>
<li> Case Study – Effectiveness of a drug treatment </li>
<li> Purpose and math behind Chi-Sq statistic </li>
<li> Chi-Sq Test </li>
<li> R Code </li>
<li> Mini-Challenge </li>
</ul>
</blockquote>
<h2>Background knowledge – Predictive modelling</h2>
<p>For the sake of completeness, let’s begin by understanding how predictive modelling works so you can better appreciate the significance of chi-squared tests and how it fits in the process.</p>
<p>Predictive modelling is a technique where we use statistical modelling or machine learning algorithms to predict a response variable/s based on one or more predictors. </p>
<p>The predictors are typically features that influence the response in some way. The models work best if the features are meaningful and have a significant relationship with the response.</p>
<p>But, you normally wouldn’t know beforehand if the response is dependent on a given feature or not. We can use the chi-squared test to determine if they are dependent or not, provided, both response and predictors are categorical variables.</p>
<h2>Hypothetical Example: Effectiveness of a Drug Treatment</h2>
<p>Let’s consider a hypothetical case where we test the effectiveness of a drug for a certain medical condition. </p>
<p>Suppose we have 105 patients under study and 50 of them were treated with the drug. The remaining 55 patients were kept as control samples. The health condition of all patients was checked after a week. </p>
<p>The following table shows if their condition improved or not. Just by looking at it, can you tell if the drug had a positive effect on the patients.</p>
<p><a title="" data-rel="lightbox-0" target="_blank" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/08/chi-squared-table-1.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="chi-squared-table" class="wp-image-4484" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/08/chi-squared-table-1-490x302.png?resize=456%2C281" data-recalc-dims="1"></a></p>
<p>As you can see, 35 out of the 50 patients showed improvement. Suppose if the drug had no effect, the 50 would have been split the the same proportion as the patients who were not given the treatment. But in this case, about 70% of patients showed improvement, which is significantly higher than the control case.</p>
<p>Since both categorical variables have only 2 levels, it was sort of intuitive to day that the drug treatment and health condition are dependent. But, as the number of categories increase, we need to quantifiable statistic to definitively say if they are dependent or not.</p>
<p>One such a metric is the <strong>chi-squared statistic</strong>.</p>
<h2>Chi-Squared Statistic</h2>
<p>For sake of understanding, lets see how Chi-squared statistic is computed for the 2 by 2 case.<br>
To begin with, we will assume that the 2 variables are not related to each other. In that is the case, can you tell what would the expected value of each cell? For example, the first cell will take the value: 50 times 75 by 105, which equals 35.7.</p>
<p>All the expected values can be computed this way (shown in brackets).</p>
<p><a title="" data-rel="lightbox-1" target="_blank" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/08/chi-squared-expected-values-1.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="chi-squared-expected-values" class="wp-image-4485" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/08/chi-squared-expected-values-1-490x303.png?resize=456%2C282" data-recalc-dims="1"></a></p>
<p>Once that is done, the Chi-Sq statistic is computed as follows.<br>
$$\chi^2= \sum_{i=1}^{n} \frac{(O_i – E_i)^2}{E_i}$$<br>
<strong>Numeric Computation</strong><br>
\(\ Chi-Sq = ((35-29.04)^2 / 29.04) + ((15-20.95)^2 / 20.95) + \)<br>
\(\ ((26-31.95)^2 / 31.95) + ((29-23.04)^2/23.04) = 5.56 \)</p>
<p>This value will be larger if the difference between the actual and expected values widens. </p>
<p>Also, the more the categories in the variables the larger the chi-squared statistic should be.</p>
<h2>Chi-Squared Test</h2>
<p>In order to establish that 2 categorical variables are dependent, the chi-squared statistic should be above a certain cutoff. This cutoff increases as the number of classes within the variable increases. </p>
<p>Alternatively, you can just perform a chi-squared test and check the p-values.</p>
<p>Like all statistical tests, chi-squared test assumes a null hypothesis and an alternate hypothesis. The general practice is, if the p-value that comes out in the result is less than a pre-determined significance level, which is 0.05 usually, then we reject the null hypothesis.</p>
<p><em> H0: The The two variables are independent<br>
H1: The two variables are related.</em></p>
<p>The null hypothesis of the chi-squared test is that the two variables are independent and the alternate hypothesis is that they are related.</p>
<h2> R Code </h2>
<p>Let’s work it out in R by doing a chi-squared test on the treatment (X) and improvement (Y) columns in <a target="_blank" rel="nofollow" href="https://goo.gl/j6lRXD">treatment.csv</a><br>
First, read in the treatment.csv data.</p>
<pre>
df &lt;- read.csv(&quot;https://goo.gl/j6lRXD&quot;)
table(df$treatment, df$improvement)
<em> improved not-improved
not-treated 26 29
treated 35 15</em>
</pre>
<p>Let’s do the chi-squared test using the <code>chisq.test()</code> function. It takes the two vectors as the input. We also set `correct=FALSE` to turn off Yates’ continuity correction.</p>
<pre>
# Chi-sq test
chisq.test(df$treatment, df$improvement, correct=FALSE)
<em>	Pearson's Chi-squared test
data: df$treatment and df$improvement
X-squared = 5.5569, df = 1, p-value = 0.01841</em>
</pre>
<p>We have a chi-squared value of 5.55. Since we get a p-Value less than the significance level of 0.05, we reject the null hypothesis and conclude that the two variables are in fact dependent. Sweet!</p>
<h2>Mini-Challenge </h2>
<p>For this challenge, find out if the ‘cyl’ and ‘carb’ variables in ‘mtcars’ dataset are dependent or not. Go ahead, pause and try it out. I will be showing the answers in a few seconds. Good Luck!</p>
<p>So here is my answer:<br>
Let’s have a look the table of <code>mtcars$carb vs mtcars$cyl</code>.</p>
<pre>
table(mtcars$carb, mtcars$cyl)
<em> 4 6 8
1 5 2 0
2 6 0 4
3 0 0 3
4 0 4 6
6 0 1 0
8 0 0 1</em>
</pre>
<p>Since there are more levels, it’s much harder to make out if they are related. Let’s use the chi-squared test instead.</p>
<pre>
# Chi-sq test
chisq.test(mtcars$carb, mtcars$cyl)
<em>	Pearson's Chi-squared test
data: mtcars$carb and mtcars$cyl
X-squared = 24.389, df = 10, p-value = 0.006632</em>
</pre>
<p>We have a high chi-squared value and a p-value of less that 0.05 significance level. So we reject the null hypothesis and conclude that <code>carb</code> and <code>cyl</code> have a significant relationship.</p>
<p>Congratulations of you got it right!</p>
<p>If you liked this post, you might find my latest video course <a target="_blank" rel="nofollow" href="https://www.packtpub.com/big-data-and-business-intelligence/introduction-r-programming-video">‘Introduction to R Programming’</a> to be quite resourceful. Happy Learning!</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toMissing Value Treatment" target="_blank" rel="bookmark" href="http://datascienceplus.com/missing-value-treatment/">Missing Value Treatment</a></li>
<li><a title="Permanent Link toR for Publication by Page Piccinini" target="_blank" rel="bookmark" href="http://datascienceplus.com/r-for-publication-by-page-piccinini/">R for Publication by Page Piccinini</a></li>
<li><a title="Permanent Link toAssessing significance of slopes in regression models with interaction" target="_blank" rel="bookmark" href="http://datascienceplus.com/assessing-significance-of-slopes-in-regression-models-with-interaction/">Assessing significance of slopes in regression models with interaction</a></li>
<li><a title="Permanent Link toFirst steps with Non-Linear Regression in R" target="_blank" rel="bookmark" href="http://datascienceplus.com/first-steps-with-non-linear-regression-in-r/">First steps with Non-Linear Regression in R</a></li>
<li><a title="Permanent Link toStandard deviation vs Standard error" target="_blank" rel="bookmark" href="http://datascienceplus.com/standard-deviation-vs-standard-error/">Standard deviation vs Standard error</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=w41a58plz44:du38RN_ok80:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=w41a58plz44:du38RN_ok80:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=w41a58plz44:du38RN_ok80:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=w41a58plz44:du38RN_ok80:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<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/chi-squared-test/"></div></div><div><a data-url="https://www.r-bloggers.com/chi-squared-test/" data-counturl="https://www.r-bloggers.com/chi-squared-test/" data-count="vertical" data-text="Chi-Squared Test" 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://datascienceplus.com/chi-squared-test-in-r/"> DataScience+</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/CpC9wPiMW7s"></a></div>