<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/raccoon-ch-2-1-t-test-anova-1-sample-t-test/"></div></div><div><a data-url="https://www.r-bloggers.com/raccoon-ch-2-1-t-test-anova-1-sample-t-test/" data-counturl="https://www.r-bloggers.com/raccoon-ch-2-1-t-test-anova-1-sample-t-test/" data-count="horizontal" data-text="Raccoon | Ch. 2.1 – T-test, Anova: 1-sample t-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://www.quantide.com/raccoon-ch-2-1-1-sample-t-test-with-r/"> R blog | Quantide - R training &amp; consulting</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>This article is part of Quantide’s web book “<a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-statistical-models-with-r-web-book/">Raccoon – Statistical Models with R</a>“. Raccoon is Quantide’s third web book after “<a target="_blank" rel="nofollow" href="http://www.quantide.com/rabbit-introduction-to-r/">Rabbit – Introduction to R</a>” and “<a target="_blank" rel="nofollow" href="http://www.quantide.com/ramarro-r-for-developers/">Ramarro – R for Developers</a>“. See the <a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-statistical-models-r-free-web-book/">full project here</a>.</em></p>
<p><em>The second chapter of Racooon is focused on T-test and Anova. Through example it shows theory and R code of:</em></p>
<ul>
<li>1-sample t-test</li>
<li>2-sample t-test</li>
<li>Paired t</li>
<li>1-way Anova</li>
<li>2-way Anova</li>
<li>Unbalanced anova with one obs dropped</li>
<li>Fixed effects Nested Anova</li>
</ul>
<p><em>This post is the first section of the chapter, about 1-sample t-test. </em></p>
<p><em>Throughout the web-book we will widely use the <strong>package qdata</strong>, containing about 80 datasets. You may find it here: <a target="_blank" rel="nofollow" href="https://github.com/quantide/qdata">https://github.com/quantide/qdata</a>.</em></p>
<p> </p>
<h2>Ch. 2.1 – T-test, Anova: 1-sample t-test</h2>

<pre>require(nortest)
require(car)
require(ggplot2)
require(dplyr)
require(gridExtra)
require(tidyr)
require(qdata)</pre>

<h2>Example: Reaction temperature</h2>
<h3>Data description</h3>
<p>A chemical theory suggests that the temperature at which a certain chemical reaction occurs is 180 °C. <code>reaction</code> contains the results of 10 independent measurements.</p>
<h3>Data loading</h3>
<p>Let us load and have a look at the data</p>
<pre>data(reaction)
head(reaction)</pre>

<pre>## [1] 180.6 179.4 181.3 178.5 179.8 176.9</pre>

<pre>str(reaction)</pre>

<pre>##  num [1:10] 181 179 181 178 180 ...</pre>

<h3>Descriptives</h3>
<p>First of all, let us calculate the main descriptive statistics on <code>reaction</code>:</p>
<pre># Add an index variable to reaction 
reaction &lt;- data.frame(reaction=reaction, index=1:10)

summary_stat &lt;- reaction %>%  summarise(n=n(),
  min=min(reaction),
  first_qu=quantile(reaction, 0.25),
  mean=mean(reaction),
  median=median(reaction),
  third_qu=quantile(reaction, 0.75),
  max=max(reaction),
  sd=sd(reaction))

print(summary_stat)</pre>

<pre>##    n   min first_qu   mean median third_qu   max       sd
## 1 10 176.9  178.725 179.49  179.6   180.55 181.3 1.427079</pre>
<p>Sample mean and median are close to the hypothesized value of 180, and the data point values are close to 180 too.<br>
Let us plot data (with collection time if meaningful) and add, with <code>geom_hline()</code> a reference line for temperature of 180 °C (red) and for the mean temperature (179.36 °C, green)</p>
<pre>ggp &lt;- ggplot(data = reaction, mapping=aes(x=index, y=reaction)) + 
  geom_point() + 
  ylim(c(177,183)) +
  geom_hline(yintercept = 180, color=&quot;red&quot;) +
  geom_hline(yintercept = mean(reaction$reaction), color=&quot;darkgreen&quot;)

print(ggp)</pre>
<p><img sizes="(max-width: 399px) 100vw, 399px" alt="anova1" class="wp-image-2887" src="https://i1.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova1-300x300.png?resize=399%2C399" data-recalc-dims="1"></p>
<p>Plot of reaction values</p>
<p>The plot shows the temperature (y-axis) on the index of observations (x-axis). Our objective is to verify if the distance between the two lines (red and green) may be due to chance or if it is unlikely from a statistical point of view. Remember that the main objective of a statistical test is not to accept the null hypothesis, but to find if enough evidence appear to refuse it.<br>
Now let us plot data without time (no index) by building a stripchart, which directly shows the points and is a good alternative to the boxplot:</p>
<pre>reaction$index &lt;- rep(0, times=10)
ggp &lt;- ggplot(data = reaction, mapping=aes(x=reaction, y=index)) + 
  geom_point() + ylim(c(-2,2)) +
  geom_vline(xintercept = 180, color=&quot;red&quot;) +
  geom_vline(xintercept = mean(reaction$reaction), color=&quot;darkgreen&quot;) +
  ylab(&quot;&quot;) +
  theme(axis.text.y=element_blank(), axis.ticks=element_blank()) +
  annotate(&quot;text&quot;, label = &quot;reaction mean&quot;, x = 178.8, y = 2, size = 4, colour = &quot;darkgreen&quot;) +
  annotate(&quot;text&quot;, label = &quot;target&quot;, x = 180.3, y = 2, size = 4, colour = &quot;red&quot;)

print(ggp)</pre>

<h3><img sizes="(max-width: 400px) 100vw, 400px" alt="anova2" class="wp-image-2888" src="https://i0.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova2-300x300.png?resize=400%2C400" data-recalc-dims="1"></h3>
<p>Stripchart of reaction values</p>
<div>
<h3>Inference and models</h3>
<div>
<h4>One-sample Student’s \(t\)-test</h4>
<p>Standard instruments, as one-sample Student’s \(t\)-test, do not use linear models at appearance. Is it true?</p>
<p><span>First let us check if the normality assumption of the \(t\)-test are plausible: let us check if data does not depart too much from normality by using the R function <code>ad.test</code> of the <code>nortest</code> require:</span></p>
<pre>ad.test(reaction$reaction)</pre>

<pre>## 
##  Anderson-Darling normality test
## 
## data:  reaction$reaction
## A = 0.25437, p-value = 0.6473</pre>
</div>
</div>
<p>Anderson-Darling’s test tests the normality vs. the non-normality of data distribution. The p-value of the test is fully larger than 0.05, the usual standard for the first type <span><span data-mathml="&lt;math xmlns=" tabindex="0">“><span><span><span>α</span></span></span></span></span> error probability: therefore there is no evidence of non-normality.<br>
The <em>Normal Probability Plot</em> confirms the Anderson-Darling’s test result:</p>
<p> </p>
<pre>ggp &lt;- ggplot(data = reaction, mapping = aes(sample = reaction)) + 
  stat_qq(color=&quot;darkblue&quot;, size=2) +
  geom_abline(mapping = aes(intercept=mean(reaction),slope=sd(reaction)), color=&quot;red&quot;, linetype=2)

print(ggp)</pre>
<p><img sizes="(max-width: 322px) 100vw, 322px" alt="anova3" class="wp-image-3227" src="https://i0.wp.com/www.quantide.com/wp-content/uploads/2016/10/anova3-300x300.png?resize=322%2C322" data-recalc-dims="1"></p>
<p>Normal probability plot of reaction</p>
<p>The plot compares the observed quantiles with the corresponding quantiles of a standard Normal distribution.<br>
If data points come from a Normal distribution, then they will tendto follow a straight line.<br>
In this case this plot is “nice”.<br>
Now, let us test the hypothesis \(H_0\) that the “true mean is equal to 180 °C”.</p>
<div>
<pre>t.test(reaction$reaction, mu = 180)</pre>
<p></p>
<pre>## 
##  One Sample t-test
## 
## data:  reaction$reaction
## t = -1.1301, df = 9, p-value = 0.2876
## alternative hypothesis: true mean is not equal to 180
## 95 percent confidence interval:
##  178.4691 180.5109
## sample estimates:
## mean of x 
##    179.49</pre>
<p>
We cannot refuse \((H_0)\) since the p-value is 0.2876.</p>
<h3>Linear models</h3>
</div>
<div>
<p>How can we think to an alternative which uses a model? Since normality assumption seems satisfied, we could fit a linear model with reaction as response variable and the only intercept as parameter (the model will then report only the mean):</p>
</div>
<pre>mod &lt;- lm(formula = reaction~1, data = reaction)</pre>
<p>In above line of code, the <code>formula</code> argument of <code>lm</code> function gives the “structure” of the relation between dependent and independent variables: the <code>~</code> symbol separates the dependent variable from the model formulation, and the <code>reaction ~ 1</code> formula means that the dependent variable <code>reaction</code> is modeled by the intercept term only (the <code>1</code>) , or, equivalently in this case, by the mean.<br>
The output of linear model <code>mod</code> is</p>
<pre>mod</pre>

<pre>## 
## Call:
## lm(formula = reaction ~ 1, data = reaction)
## 
## Coefficients:
## (Intercept)  
##       179.5</pre>
<p>By applying the <code>summary()</code> R function to <code>lm</code>-type objects we get more information:</p>
<pre>summary(mod)</pre>

<pre>## 
## Call:
## lm(formula = reaction ~ 1, data = reaction)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -2.590 -0.765  0.110  1.060  1.810 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 179.4900     0.4513   397.7   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.427 on 9 degrees of freedom</pre>
<p>The very small p-value (\(&lt;2^{-16}\)) tells us that the intercept is not 0; also, the intercept estimate (i.e., the mean) is close to 180.</p>
<pre>reaction_z &lt;- reaction$reaction-180
fm &lt;- lm(reaction_z~1)</pre>
<p>or, equivalently:</p>
<pre>fm &lt;- lm(I(reaction-180)~1, data=reaction)</pre>
<p>If the null hypothesis is true, then <code>reaction-180</code> has 0 mean.<br>
The output of the linear model <code>fm</code> is</p>
<pre>summary(fm)</pre>

<pre>## 
## Call:
## lm(formula = I(reaction - 180) ~ 1, data = reaction)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -2.590 -0.765  0.110  1.060  1.810 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  -0.5100     0.4513   -1.13    0.288
## 
## Residual standard error: 1.427 on 9 degrees of freedom</pre>
<p><span>The estimated coefficient of the model, in this case the mean of reaction minus 180, is -0.51, indeed</span></p>
<pre>mean(reaction$reaction) - 180</pre>

<pre>## [1] -0.51</pre>
<p>The p-value, 0.288, is the same of the \(t\)-test: in this case it means that the intercept is not significantly different from 0 (that is the mean of original data can be 180).<br>
This particularly simple regression model is then equivalent to one-sample \(t\)-test.<br>
Let us check for normality of residuals, even if in this particular case is redundant, since it is equivalent to check the normality of <code>reaction</code>.</p>
<pre>res &lt;- data.frame(res=fm$residuals)
ad.test(res$res)</pre>

<pre>## 
##  Anderson-Darling normality test
## 
## data:  res$res
## A = 0.25437, p-value = 0.6473</pre>

<pre>ggp &lt;- ggplot(data = res, mapping = aes(sample = res)) + 
  stat_qq(color=&quot;darkblue&quot;, size=2) +
  geom_abline(mapping = aes(intercept=mean(res),slope=sd(res)), color=&quot;red&quot;, linetype=2)

print(ggp)</pre>
<p><img sizes="(max-width: 400px) 100vw, 400px" alt="anova4" class="wp-image-2890" src="https://i1.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova4-300x300.png?resize=400%2C400" data-recalc-dims="1"></p>
<p>Normal probability plot of residuals</p>
<p>Indeed, the p-value of Anderson-Darling’s test is the same as before. The q-q plot also confirms that the normality assumption is plausible.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-ch-2-1-1-sample-t-test-with-r/">Raccoon | Ch. 2.1 – T-test, Anova: 1-sample t-test</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.quantide.com/">Quantide – R training &amp; consulting</a>.</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/raccoon-ch-2-1-t-test-anova-1-sample-t-test/"></div></div><div><a data-url="https://www.r-bloggers.com/raccoon-ch-2-1-t-test-anova-1-sample-t-test/" data-counturl="https://www.r-bloggers.com/raccoon-ch-2-1-t-test-anova-1-sample-t-test/" data-count="vertical" data-text="Raccoon | Ch. 2.1 – T-test, Anova: 1-sample t-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://www.quantide.com/raccoon-ch-2-1-1-sample-t-test-with-r/"> R blog | Quantide - R training &amp; consulting</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/lkFldePag8g"></a></div>