<?xml version="1.0" encoding="UTF-8" ?>
<!-- RSS generated by PHPBoost on Sun, 17 May 2026 15:58:51 +0200 -->

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Easy Guides]]></title>
		<atom:link href="https://www.sthda.com/english/syndication/rss/wiki/20" rel="self" type="application/rss+xml"/>
		<link>https://www.sthda.com</link>
		<description><![CDATA[Last articles of the category: t test]]></description>
		<copyright>(C) 2005-2026 PHPBoost</copyright>
		<language>en</language>
		<generator>PHPBoost</generator>
		
		
		<item>
			<title><![CDATA[T test analysis : is it always correct to compare means ?]]></title>
			<link>https://www.sthda.com/english/wiki/t-test-analysis-is-it-always-correct-to-compare-means</link>
			<guid>https://www.sthda.com/english/wiki/t-test-analysis-is-it-always-correct-to-compare-means</guid>
			<description><![CDATA[<!-- START HTML -->

            
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#t-test">T test</a></li>
<li><a href="#t-test-assumptions-normality-and-equal-variances">T test assumptions : Normality and equal variances</a></li>
<li><a href="#how-to-test-the-normality-of-data">How to test the normality of data?</a></li>
<li><a href="#how-to-test-the-equality-of-variances">How to test the equality of variances ?</a></li>
<li><a href="#what-to-do-when-the-conditions-are-not-met-for-t-test">What to do when the conditions are not met for t test ?</a></li>
<li><a href="#rquery.t.test-smart-t.test-function">rquery.t.test : smart t.test function</a></li>
<li><a href="#one-sample-t-test-compare-an-observed-mean-with-a-theoretical-mean">One sample t-test : Compare an observed mean with a theoretical mean</a></li>
<li><a href="#independent-t-test-compare-the-means-of-two-unpaired-samples">Independent t-test : Compare the means of two unpaired samples</a></li>
<li><a href="#paired-t-test-compare-two-dependent-samples">Paired t test : Compare two dependent samples</a></li>
<li><a href="#online-t-test-calculator">Online t-test calculator</a></li>
<li><a href="#the-r-code-of-rquery.t.test-function">The R code of rquery.t.test function</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<div id="t-test" class="section level1">
<h1>T test</h1>
<p>Probably one of the most popular research questions is whether <strong>two independent samples</strong> differ from each other. <strong>Student’s t test</strong> is one of the common <strong>statistical test</strong> used for comparing the means of <strong>two independent</strong> or <strong>paired samples</strong>.</p>
<p><strong>t test formula</strong> is described in detail <a href="https://www.sthda.com/english/english/wiki/t-test-formula">here</a> and it can be easily computed using <strong>t.test()</strong> <strong>R</strong> function. However, <strong>one important question is</strong> :</p>
<p><span class="question">Is it always correct to compare means ?</span></p>
<p><span class="success">The answer is no, of course. This is explained in the next section</span></p>
<p>The purpose of this article is :</p>
<ul>
<li>Firstly to discuss about why we cannot always use <strong>t test</strong></li>
<li>Secondly to provide an easy to use R function (<strong>rquery.t.test()</strong>) that will guide the user step by step in order to perform <strong>t test</strong> satisfying the appropriate conditions.</li>
</ul>
</div>
<div id="t-test-assumptions-normality-and-equal-variances" class="section level1">
<h1>T test assumptions : Normality and equal variances</h1>
<p><strong>Statistical errors</strong> are common in scientific literature, and about 50% of the published articles have at least one error. Many of the statistical procedures including correlation, regression, <strong>t test</strong>, and analysis of variance assume that the data are <strong>normally distributed</strong>.</p>
<p><span class="success">These tests are called <strong>parametric tests</strong>, because their validity depends on the distribution of the data.</span></p>
<p>A frequent error is to use statistical tests that assume a <strong>normal distribution</strong> on data that are actually skewed.</p>
<p>As mentioned above, we <strong>can not always use</strong> <strong>Student’s t test</strong> to compare means. There are <a href="https://www.sthda.com/english/english/wiki/t-test-formula">different types of <strong>t-test</strong></a> : <a href="https://www.sthda.com/english/english/wiki/one-sample-t-test"><strong>one-sample t test</strong></a>, the <a href="https://www.sthda.com/english/english/wiki/independent-t-test"><strong>independent two samples t test</strong></a> and the <a href="https://www.sthda.com/english/english/wiki/paired-t-test"><strong>paired t test</strong></a>.</p>
<p>These different tests can be used only in certain conditions :</p>
<br/>
<div class="warning">
<p>Before using <strong>t test</strong>, you have to check :</p>
<ol style="list-style-type: decimal">
<li>For <a href="https://www.sthda.com/english/english/wiki/one-sample-t-test"><strong>one-sample t test</strong></a> :
<ul>
<li>Whether the data are normally distributed</li>
</ul></li>
<li>For <a href="https://www.sthda.com/english/english/wiki/independent-t-test"><strong>independent two samples t test</strong></a> :
<ul>
<li>Whether the two groups of samples (x and y), being compared, are <strong>normally distributed</strong>;</li>
<li>and whether the <strong>variances</strong> of the two samples are equal or not.</li>
</ul></li>
<li>For <a href="https://www.sthda.com/english/english/wiki/paired-t-test"><strong>paired t test</strong></a> :
<ul>
<li>Whether the difference d ( = x - y) is normally distributed</li>
</ul></li>
</ol>
</div>
<p><br/></p>
<p><span class="notice">These assumptions should be taken seriously to draw reliable conclusions.</span></p>
<p>The outcome of these <strong>preliminary test</strong> then determines which method should be used for assessing the main hypothesis. Unfortunately, these pretests are not performed automatically by the built-in <strong>t.test()</strong> R function. This is the reason why, I wrote the <strong>rquery.t.test()</strong> function which checks first all the t test assumptions and then decides which method to use (<strong>parametric</strong> or <strong>non-parametric</strong>) based on the result of the preliminary test.</p>
</div>
<div id="how-to-test-the-normality-of-data" class="section level1">
<h1>How to test the normality of data?</h1>
<p>With <strong>large enough sample sizes</strong> (n > 30) the violation of the normality assumption should not cause major problems. This implies that we can ignore the distribution of the data and use parametric tests if we are dealing with large sample sizes.</p>
<p><span class="success"><strong>The central limit theorem</strong> tells us that no matter what distribution things have, the sampling distribution tends to be normal if the sample is large enough (n > 30).</span></p>
<p>However, to be consistent, normality can be checked by visual inspection [<strong>normal plots (histogram)</strong>, <strong>Q-Q plot</strong> (quantile-quantile plot)] or by <strong>significance tests</strong>.</p>
<ul>
<li>The <strong>histogram</strong> plot (frequency distribution) provides a visual judgment about whether the distribution is bell shaped.</li>
<li>The <strong>significance test</strong> compares the sample distribution to a normal one in order to ascertain whether data show or not a serious deviation from normality.</li>
</ul>
<p>There are several methods for <strong>normality test</strong> such as <strong>Kolmogorov-Smirnov (K-S) normality test</strong> and <strong>Shapiro-Wilk’s test</strong>.</p>
<p><span class="warning"> The null hypothesis of these tests is that “sample distribution is normal”. If the test is <strong>significant</strong>, the distribution is non-normal.<span></p>
<p><strong>Shapiro-Wilk’s method</strong> is widely recommended for normality test and it provides better power than K-S. It is based on the correlation between the data and the corresponding normal scores.</p>
<p><span class="notice">Note that, normality test is sensitive to sample size. Small samples most often pass normality tests. Therefore, it’s important to combine visual inspection and significance test in order to take the right decision. </span></p>
<p><span class="question">Question : To test or not to test normality ?</span></p>
<p>Normality test and the others assumptions made by <strong>parametric tests</strong> should be pretested before continuing with the main test. For example, in medical research, normally distributed data are the exception rather than the rule. In such situations, the use of parametric methods is discouraged, and non-parametric tests (which are also referred to as distribution-free tests) such as the two-samples <strong>Wilcoxon test</strong> are recommended instead.</p>
<p><span class="success">In rquery.t.test() function, Shapiro-Wilk’s normality test is used and, histogram and Q-Q plots are automatically displayed for visual inspection.</span></p>
</div>
<div id="how-to-test-the-equality-of-variances" class="section level1">
<h1>How to test the equality of variances ?</h1>
<p>The standard <strong>two independent samples t test</strong> assumes also that the samples have equal variances. If the two samples, being compared, follow normal distribution, <strong>F test</strong> can be performed to <strong>compare the variances</strong>.</p>
<p><span class="warning"> The null hypothesis of <strong>F test</strong> is that the variances of the two populations are equal. If the test is significant, null hypothesis are rejected and then we can conclude that the variances are significantly different. </span></p>
</div>
<div id="what-to-do-when-the-conditions-are-not-met-for-t-test" class="section level1">
<h1>What to do when the conditions are not met for t test ?</h1>
<div class="success">
<p>The following <strong>two-stage procedure</strong> is wide accepted (view the figure below):</p>
<ol style="list-style-type: decimal">
<li>If normality is accepted, the t test is used;</li>
<li>If the samples being compared are not normally distributed, a <strong>non-parametric test</strong> like <strong>Wilcoxon test</strong> is recommended as an alternative to the <strong>t test</strong>.</li>
</ol>
</div>
<p><span class="warning"> If the two samples are normally distributed, but with <strong>unequal variances</strong>, the <a href="https://www.sthda.com/english/english/wiki/welch-t-test"><strong>Welch t test</strong></a> can be used (figure below). <a href="https://www.sthda.com/english/english/wiki/welch-t-test"><strong>Welch t-test</strong></a> is an adaptation of <strong>Student’s t test</strong> used when the equality of variances of the two samples cannot be assumed.</span></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/t-test-diagram.png" alt="t-test" /></p>
</div>
<div id="rquery.t.test-smart-t.test-function" class="section level1">
<h1>rquery.t.test : smart t.test function</h1>
<p>As mentioned above, this function is an improvement of the built-in <a href="https://www.sthda.com/english/english/wiki/t-test"><strong>t.test()</strong></a> R function. It can be used to perform one or two sample t-tests (paired and unpaired). Its advantage over the basic <strong>t.test()</strong> function is that : it checks automatically the distribution of the data and the equality of the two sample variances (in the case of independent t test).</p>
<p>Before calculating <strong>t-test</strong>, the <strong>rquery.t.test</strong> function performs the following steps :</p>
<ol style="list-style-type: decimal">
<li><p>First, The <strong>Shapiro-Wilk test</strong> is used to perform <strong>normality test</strong>. If the samples are not normally distributed, the <strong>rquery.t.test</strong> function will warn and suggest you to do a <strong>wilcoxon test</strong>.</p></li>
<li>An <strong>F test</strong> comparing the variances of the two samples is used to check the assumption of <strong>homogeneity of variances</strong> (in the case of independent t test) :
</li>
</ol>
<ul>
<li>If the two <strong>variances</strong> are assumed equal : The classic <a href="https://www.sthda.com/english/english/wiki/t-test-formula"><strong>Student’s t-test</strong></a> is performed</li>
<li>If the two <strong>variances</strong> are significantly different: the <a href="https://www.sthda.com/english/english/wiki/welch-t-test"><strong>Welch t-test</strong></a> is automatically applied.</li>
</ul>
<p><span class="warning">Note that the R code of <strong>rquery.t.test()</strong> function is provided at the end of this document.</span></p>
<p>A simplified format of the function is :</p>
<pre class="r"><code>rquery.t.test(x, y = NULL, paired = FALSE, graph = TRUE, ...)</code></pre>
<br/>
<div class="warning">
<p>The result of <strong>rquery.t.test()</strong> function is identical to that given by the built-in <strong>t.test()</strong> R function. It contains components including :</p>
<ul>
<li><strong>statistic</strong> : the value of the <strong>t-test statistics</strong></li>
<li><strong>parameter</strong> : the <strong>degrees of freedom</strong> for the <strong>t-test statistics</strong></li>
<li><strong>p.value</strong> : the <strong>p-value</strong> for the test</li>
</ul>
To read more, go to the chapter : <a href="https://www.sthda.com/english/english/wiki/t-test">Student’s t-test in R</a>.
</div>
</div>
<div id="one-sample-t-test-compare-an-observed-mean-with-a-theoretical-mean" class="section level1">
<h1>One sample t-test : Compare an observed mean with a theoretical mean</h1>
<pre class="r"><code>source(&amp;#39;https://www.sthda.com/upload/rquery_t_test.r&amp;#39;)
set.seed(123456789)
x<-rnorm(100) # generate some data
rquery.t.test(x, mu=0)</code></pre>
<pre><code>
    One Sample t-test

data:  x
t = 0.2606, df = 99, p-value = 0.7949
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -0.1657  0.2159
sample estimates:
mean of x 
  0.02506 </code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/rquery-t-test-tutorial-one-sample-t-test.png" title="T test data analysis : tutorial on how to make easily independent and paired t test" alt="T test data analysis : tutorial on how to make easily independent and paired t test" width="480" style="margin-bottom:10px;" /></p>
<p>Read more by following this link : <a href="https://www.sthda.com/english/english/wiki/one-sample-t-test">one sample t test</a></p>
</div>
<div id="independent-t-test-compare-the-means-of-two-unpaired-samples" class="section level1">
<h1>Independent t-test : Compare the means of two unpaired samples</h1>
<p><strong>Case 1 - The two groups of samples are normally distributed and have equal variances</strong>:</p>
<pre class="r"><code>source(&amp;#39;https://www.sthda.com/upload/rquery_t_test.r&amp;#39;)
set.seed(123456789)
x<-rnorm(100, mean=2, sd=0.9)
y<-rnorm(100, mean=4, sd=1)
rquery.t.test(x, y)</code></pre>
<pre><code>
    Two Sample t-test

data:  x and y
t = -16.85, df = 198, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -2.449 -1.935
sample estimates:
mean of x mean of y 
    2.023     4.215 </code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/rquery-t-test-tutorial-independent-t-test.png" title="T test data analysis : tutorial on how to make easily independent and paired t test" alt="T test data analysis : tutorial on how to make easily independent and paired t test" width="432" style="margin-bottom:10px;" /></p>
<p><span class="success">The <a href="https://www.sthda.com/english/english/wiki/independent-t-test"><strong>classic two samples t-test</strong></a> is automatically used.</span></p>
<p><strong>Case 2 - The two samples are normally distributed but the variances are unequal</strong>:</p>
<pre class="r"><code>source(&amp;#39;https://www.sthda.com/upload/rquery_t_test.r&amp;#39;)
set.seed(123456789)
x<-rnorm(100, mean=2, sd=0.9)
y<-rnorm(100, mean=2, sd=3)
rquery.t.test(x, y)</code></pre>
<pre><code>
    Welch Two Sample t-test

data:  x and y
t = -2.043, df = 116.3, p-value = 0.04326
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.22327 -0.01913
sample estimates:
mean of x mean of y 
    2.023     2.644 </code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/rquery-t-test-tutorial-welch-t-test.png" title="T test data analysis : tutorial on how to make easily independent and paired t test" alt="T test data analysis : tutorial on how to make easily independent and paired t test" width="432" style="margin-bottom:10px;" /></p>
<p><span class="success">The <a href="https://www.sthda.com/english/english/wiki/welch-t-test"><strong>Weltch t-test</strong></a> is automatically used.</span></p>
<p><strong>Case 3 - The samples are not normally distributed</strong>:</p>
<pre class="r"><code>source(&amp;#39;https://www.sthda.com/upload/rquery_t_test.r&amp;#39;)
set.seed(123456789)
x<-rnorm(100, mean=2, sd=0.9)
x<-c(x, 10,20) # add some outliers
y<-rnorm(100, mean=4, sd=1)
rquery.t.test(x, y)</code></pre>
<pre><code>Warning: x or y is not normally distributed : Shapiro test p-value : 2e-17 (for x) and 0.5 (for y).
 Use a non parametric test like Wilcoxon test.</code></pre>
<pre><code>
    Welch Two Sample t-test

data:  x and y
t = -8.374, df = 142.2, p-value = 4.809e-14
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -2.395 -1.480
sample estimates:
mean of x mean of y 
    2.277     4.215 </code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/rquery-t-test-tutorial-not-normally-distributed.png" title="T test data analysis : tutorial on how to make easily independent and paired t test" alt="T test data analysis : tutorial on how to make easily independent and paired t test" width="432" style="margin-bottom:10px;" /></p>
<p><span class="success">A warning message is automatically displayed and the <strong>rquery.t.test</strong> function will suggest you to perform a non-parametric test like <strong>wilcoxon test</strong>.</span></p>
</div>
<div id="paired-t-test-compare-two-dependent-samples" class="section level1">
<h1>Paired t test : Compare two dependent samples</h1>
<pre class="r"><code>source(&amp;#39;https://www.sthda.com/upload/rquery_t_test.r&amp;#39;)
set.seed(123456789)
x<-rnorm(30, mean=10, sd=2)
y<-rnorm(30, mean=50, sd=3)
rquery.t.test(x, y, paired=TRUE)</code></pre>
<pre><code>
    Paired t-test

data:  x and y
t = -56.5, df = 29, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -41.63 -38.72
sample estimates:
mean of the differences 
                 -40.17 </code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/rquery-t-test-tutorial-paired-t-test.png" title="T test data analysis : tutorial on how to make easily independent and paired t test" alt="T test data analysis : tutorial on how to make easily independent and paired t test" width="480" style="margin-bottom:10px;" /></p>
<p><span class="success">Read more on <a href="https://www.sthda.com/english/english/wiki/paired-t-test">paired t test</a></span></p>
</div>
<div id="online-t-test-calculator" class="section level1">
<h1>Online t-test calculator</h1>
<br/>
<div class="block">
<i class="fa - fa-cogs fa-4x valign_middle"></i> Note that an online software is available <a href="https://www.sthda.com/english/english/rsthda/rsthda.php">here</a> to calculate <strong>Student’s t-test</strong> without any installation.
</div>
<p><br/></p>
</div>
<div id="the-r-code-of-rquery.t.test-function" class="section level1">
<h1>The R code of rquery.t.test function</h1>
<pre class="r"><code>#++++++++++++++++++++++++
# rquery.t.test
#+++++++++++++++++++++++
# Description : Performs one or two samples t-test

# x : a (non-empty) numeric vector of data values.
# y : an optional (non-empty) numeric vector of data values
# paired : if TRUE, paired t-test is performed
# graph : if TRUE, the distribution of the data is shown
  # for the inspection of normality
# ... : further arguments to be passed to the built-in t.test() R function

# 1. shapiro.test is used to check normality
# 2. F-test is performed to check equality of variances
# If the variances are different, then Welch t-test is used

rquery.t.test<-function(x, y = NULL, paired = FALSE, graph = TRUE, ...)
{
  # I. Preliminary test : normality and variance tests
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  var.equal = FALSE # by default
  
  # I.1 One sample t test
  if(is.null(y)){
    if(graph) par(mfrow=c(1,2))
    shapiro.px<-normaTest(x, graph, 
                          hist.title="X - Histogram",
                          qq.title="X - Normal Q-Q Plot")
    if(shapiro.px < 0.05)
      warning("x is not normally distributed :",
              " Shapiro-Wilk test p-value : ", shapiro.px, 
              ".\n Use a non-parametric test like Wilcoxon test.")
  }
  
  # I.2 Two samples t test
  if(!is.null(y)){
    
      # I.2.a unpaired t test
      if(!paired){
          if(graph) par(mfrow=c(2,2))
          # normality test
          shapiro.px<-normaTest(x, graph, 
                                hist.title="X - Histogram",
                                qq.title="X - Normal Q-Q Plot")
          shapiro.py<-normaTest(y, graph,
                                hist.title="Y - Histogram",
                                qq.title="Y - Normal Q-Q Plot")
          if(shapiro.px < 0.05 | shapiro.py < 0.05){
              warning("x or y is not normally distributed :",
                      " Shapiro test p-value : ", shapiro.px,
                      " (for x) and ", shapiro.py, " (for y)",
                      ".\n Use a non parametric test like Wilcoxon test.")
            }
          # Check for equality of variances
          if(var.test(x,y)$p.value >= 0.05) var.equal=TRUE
        } 
      
      # I.2.b Paired t-test
      else {
        if(graph) par(mfrow=c(1,2))
        d = x-y 
        shapiro.pd<-normaTest(d, graph, 
                              hist.title="D - Histogram",
                              qq.title="D - Normal Q-Q Plot")
        if(shapiro.pd < 0.05 )
          warning("The difference d ( = x-y) is not normally distributed :",
                  " Shapiro-Wilk test p-value : ", shapiro.pd, 
                  ".\n Use a non-parametric test like Wilcoxon test.")
      } 
      
   }
  
  # II. Student&amp;#39;s t-test
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  res <- t.test(x, y, paired=paired, var.equal=var.equal, ...)
  return(res)
}


#+++++++++++++++++++++++
# Helper function
#+++++++++++++++++++++++

# Performs normality test using Shapiro Wilk&amp;#39;s method
# The histogram and Q-Q plot of the data are plotted

# x : a (non-empty) numeric vector of data values.
# graph : possible values are TRUE or FALSE. If TRUE,
  # the histogram and the Q-Q plot of the data are displayed
# hist.title : title of the histogram
# qq.title : title of the Q-Q plot
normaTest<-function(x, graph=TRUE, 
                    hist.title="Histogram", 
                    qq.title="Normal Q-Q Plot",...)
  {  
  # Significance test
  #++++++++++++++++++++++
  shapiro.p<-signif(shapiro.test(x)$p.value,1) 
  
  if(graph){
    # Plot : Visual inspection
    #++++++++++++++++
    h<-hist(x, col="lightblue", main=hist.title, 
            xlab="Data values", ...)
    m<-round(mean(x),1)
    s<-round(sd(x),1)
    mtext(paste0("Mean : ", m, "; SD : ", s),
          side=3, cex=0.8)
    # add normal curve
    xfit<-seq(min(x),max(x),length=40)
    yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
    yfit <- yfit*diff(h$mids[1:2])*length(x)
    lines(xfit, yfit, col="red", lwd=2)
    # qq plot
    qqnorm(x, pch=19, frame.plot=FALSE,main=qq.title)
    qqline(x)
    mtext(paste0("Shapiro-Wilk, p-val : ", shapiro.p),
          side=3, cex=0.8)
  }
  return(shapiro.p)
}</code></pre>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed with R (ver. 3.1.0). </span></p>
<p><br/></p>
<p><strong>References</strong></p>
<ol style="list-style-type: decimal">
<li>Asghar Ghasemi, Saleh Zahediasl; Normality Tests for Statistical Analysis: A Guide for Non-Statisticians; Int J Endocrinol Metab. 2012;10(2):486-489.</li>
<li>Rochon J1, Gondan M, Kieser M.; To test or not to test: Preliminary assessment of normality when comparing two independent samples; BMC Med Res Methodol. 2012 Jun 19;12:81.</li>
</ol>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Fri, 06 Feb 2015 21:27:54 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[t test calculator : The 3 best one you should know]]></title>
			<link>https://www.sthda.com/english/wiki/t-test-calculator-the-3-best-one-you-should-know</link>
			<guid>https://www.sthda.com/english/wiki/t-test-calculator-the-3-best-one-you-should-know</guid>
			<description><![CDATA[<!-- START HTML -->
<style>#whatis img{background-color:#fff;background-color:#FFF;border-radius:3px;border:1px solid #CCC;box-shadow:2px 2px 12px -5px #999;margin:0 5px;margin-bottom:5px;padding:5px;text-align:center}</style>
<!-- END HTML --><br />
<br />
<!-- START HTML -->
<div id="whatis">
<!-- END HTML --><br />
<span style="color:#0000FF;"><h2 class="formatter-title">The 3 best online <strong>t test calculators</strong> you should know</h2></span><br />
<br />
<br />
<h2 class="formatter-title">1. RSTHDA unpaired  t-test calculator</h2><br />
<br />
<a href="https://www.sthda.com/english/english/rsthda/unpaired-t-test.php"><img src="https://www.sthda.com/english/upload/rsthda_independent_t_test.png" alt="" /></a><br />
<br />
<strong>Source : RSTHDA; <a href="https://www.sthda.com/english/english/rsthda/unpaired-t-test.php">Demo</a></strong><br />
<br />
<br />
<br />
<h2 class="formatter-title">2. Student T-Test Calculator for 2 Independent Means</h2><br />
<br />
<a href="http://www.socscistatistics.com/tests/studentttest/Default2.aspx"><img src="https://www.sthda.com/english/upload/t_test_calculator_1.png" alt="" /></a><br />
<br />
<strong>Source : Social Science Statistics; <a href="http://www.socscistatistics.com/tests/studentttest/Default2.aspx">Demo</a></strong><br />
<br />
<br />
<h2 class="formatter-title">3. Student's T-Test</h2><br />
<br />
<a href="http://studentsttest.com/">Demo</a><br />
<br />
<br />
<br />
<!-- START HTML -->
</div>
<!-- END HTML -->]]></description>
			<pubDate>Sun, 16 Nov 2014 17:34:06 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[t test]]></title>
			<link>https://www.sthda.com/english/wiki/t-test</link>
			<guid>https://www.sthda.com/english/wiki/t-test</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#what-is-t-test">What is t test</a></li>
<li><a href="#t.test-r-function-to-calculate-t-test">t.test : R function to calculate t test</a></li>
<li><a href="#results-of-t-test-r-function">Results of t test R function</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>


<div id="what-is-t-test" class="section level1">
<h1>What is t test</h1>
<p><strong>t test</strong> is used to compare either the means of two sets of data or to compare an <strong>observed mean m</strong> to a <strong>theoretical value mu</strong>. There are different types of <strong>t test</strong> :</p>
<ul>
<li>The <strong>one-sample t test</strong> used to compare an observed mean with a theoretical mean.</li>
<li>The <strong>independent t test</strong> (or <strong>two sample t test</strong>) used to compare the means of two unrelated groups of samples.</li>
<li>The <strong>paired t test</strong> used to compare two sets of paired samples.</li>
</ul>
<p><br/>
<span class="success">The different <strong>t test formula</strong> are described in detail by following this link: <a href="https://www.sthda.com/english/english/wiki/t-test-formula">t test formula</a>. </span></p>
<p><br/>
The objective of this chapter is to show you how to <strong>calculate t test</strong> in <strong>R</strong>.</p>
<br/>
<div class="block">
<i class="fa - fa-cogs fa-4x valign_middle"></i> Note that an online <strong>t test calculator</strong> is available <a href="https://www.sthda.com/english/english/rsthda/rsthda.php">here</a> to calculate <strong>Student’s t-test</strong> without any installation.
</div>
</div>
<div id="t.test-r-function-to-calculate-t-test" class="section level1">
<h1>t.test : R function to calculate t test</h1>
<p>The <strong>R</strong> function to use for <strong>t test statistics</strong> is <strong>t.test()</strong>. It can be used to <strong>calculate</strong> the different types of <strong>Student t test</strong> mentioned above.</p>
<p>A simplified format of the function is:</p>
<pre class="r"><code># One sample t test :
# Comparison of an observed mean with a
# a theoretical mean
t.test(x, mu=0)

# Independent t test
# Comparison of the means of two independent samples (x &amp; y)
t.test(x, y)

# Paired t test
t.test(x, y, paired=TRUE)</code></pre>
<p><span class="warning">x and y are two <strong>numeric</strong> vectors.</span></p>
<p><span class="notice">The argument <strong>“alternative =”</strong> can be used to specify the alternative hypothesis. It must be one of <strong>“two.side”</strong> (<strong>2 tailed t test</strong>, default), <strong>“greater”</strong> or <strong>“less”</strong> (<strong>one sided t test</strong>) (see the <strong>R</strong> code below ).</span></p>
<pre class="r"><code>t.test(x, y, alternative=c("two.sided", "less", "greater"))</code></pre>
</div>
<div id="results-of-t-test-r-function" class="section level1">
<h1>Results of t test R function</h1>
<p>The result of <strong>t.test()</strong> function is a list of class “htest” including the following components :</p>
<ul>
<li><strong>statistic</strong> : the value of the <strong>t test statistics</strong></li>
<li><strong>parameter</strong> : the <strong>degrees of freedom</strong> for the <strong>t test statistics</strong></li>
<li><strong>p.value</strong> : the <strong>p-value</strong> for the test</li>
<li><strong>conf.int</strong> : a <strong>confidence interval</strong> for the mean appropriate to the specified <strong>alternative hypothesis</strong>.</li>
<li><strong>estimate</strong> : the means of the two groups being compared (in the case of <strong>independent t test</strong>) or difference in means (in the case of <strong>paired t test</strong>).</li>
</ul>
<p>The format of the <strong>R</strong> code to use to get these values is as follow:</p>
<pre class="r"><code>test<-t.test(x,y)
res$p.value # get the p-value
res$parameter # get the degrees of freedom
res$statistic # get the t test statistics</code></pre>
<p><span class="success">Many articles are included in this chapter. Scroll down to the bottom of this page to see some examples on how to <strong>perform t test</strong> in R.</span></p>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been done with R (ver. 3.1.0). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Sun, 16 Nov 2014 14:54:25 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Welch t-test]]></title>
			<link>https://www.sthda.com/english/wiki/welch-t-test</link>
			<guid>https://www.sthda.com/english/wiki/welch-t-test</guid>
			<description><![CDATA[<!-- START HTML -->

            
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">



<div id="TOC">
<ul>
<li><a href="#what-is-welch-t-test">What is Welch t-test?</a></li>
<li><a href="#welch-t-test-formula">Welch t-test formula</a></li>
<li><a href="#online-students-t-test-calculator">Online Student’s t-test calculator</a></li>
</ul>
</div>

<div id="what-is-welch-t-test" class="section level1">
<h1>What is Welch t-test</h1>
<p>The <strong>Welch t-test</strong> is an adaptation of <a href="https://www.sthda.com/english/english/wiki/student-t-test-formulas"><strong>Student’s t-test</strong></a>. It is used to compare the means of two groups of samples when the <strong>variances are different</strong>.</p>
</div>
<div id="welch-t-test-formula" class="section level1">
<h1>Welch t-test formula</h1>
<p><strong>Welch t-statistic</strong> is calculated as follow :</p>
<p><span class="math">\[ 
t = \frac{m_A - m_B}{\sqrt{ \frac{S_A^2}{n_A} + \frac{S_B^2}{n_B} }} 
\]</span></p>
<ul>
<li>A and B represent the two groups to compare.
</li>
<li><span class="math">\(m_A\)</span> and <span class="math">\(m_B\)</span> represent the means of groups A and B, respectively.</li>
<li><span class="math">\(n_A\)</span> and <span class="math">\(n_B\)</span> represent the sizes of group A and B, respectively.</li>
<li><span class="math">\(S_A\)</span> and <span class="math">\(S_B\)</span> are the standar deviation of the the two groups A and B, rspectively.</li>
</ul>
<p><span class="warning">Unlike the classic <a href="https://www.sthda.com/english/english/wiki/student-t-test-formulas"><strong>Student’s t-test</strong></a>, <strong>Welch t-test formula</strong> involves the variance of each of the two groups (<span class="math">\(S_A^2\)</span> and <span class="math">\(S_B^2\)</span>) being compared. In other words, it does not use the <strong>common variance</strong>.</span></p>
<p>The <strong>degrees of freedom</strong> of <strong>Welch t-test</strong> is calculated as follow :</p>
<p><span class="math">\[
df = (\frac{S_A^2}{n_A}+ \frac{S_B^2}{n_B^2}) / (\frac{S_A^4}{n_A^2(n_B-1)} + \frac{S_B^4}{n_B^2(n_B-1)} )
\]</span></p>
<p><br/></p>
<p>Once the <strong>t value</strong> is determined, you have to read in the <a href="https://www.sthda.com/english/english/wiki/student-t-table"><strong>t table</strong></a> the <strong>critical value of Student’s t distribution</strong> corresponding to the <strong>significance level alpha</strong> of your choice (5%).</p>
<p><br/> <span class="success"> If the absolute value of the <strong>t statistic</strong> (|t|) is greater than the critical value, then the difference is significant. Otherwise it isn’t. The <strong>level of significance</strong> or (<strong>p-value</strong>) corresponds to the risk indicated by the <a href="https://www.sthda.com/english/english/wiki/student-t-table"><strong>t table</strong></a> for the calculated |t| value.</span></p>
</div>
<div id="online-students-t-test-calculator" class="section level1">
<h1>Online Student’s t-test calculator</h1>
<br/>
<div class="block">
An <a href="https://www.sthda.com/english/english/rsthda/rsthda.php"><strong>online Student’s t-test calculator</strong></a> is available <a href="https://www.sthda.com/english/english/rsthda/rsthda.php">here</a> without any installation.
</div>
<p><br/></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Fri, 14 Nov 2014 21:42:38 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Matrix of student t-test]]></title>
			<link>https://www.sthda.com/english/wiki/matrix-of-student-t-test</link>
			<guid>https://www.sthda.com/english/wiki/matrix-of-student-t-test</guid>
			<description><![CDATA[<!-- START HTML -->

            
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">



<div id="TOC">
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#custom-function-for-multiple-t-test">Custom function for multiple t-test</a></li>
<li><a href="#data-for-t-test">Data for t-test</a></li>
<li><a href="#compute-the-matrix-of-t-test">Compute the matrix of t-test</a></li>
<li><a href="#keep-only-the-lower-triangle-of-the-matrix">Keep only the lower triangle of the matrix</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<div id="introduction" class="section level1">
<h1>Introduction</h1>
<p><strong>student t-test</strong> is used to compare the mean of two groups of samples. The aim of this article is to show how to perform a matrix of <strong>t-test</strong> in <strong>R</strong>. In this case a <strong>t-test</strong> is computed between each variable and the others. The result is a matrix of <strong>p-value</strong> containing the <strong>significance levels</strong> between all possible pairs of variables.</p>


<div class="block">
<i class="fa - fa-cogs fa-4x valign_middle"></i> Note that online software is also available <a href="https://www.sthda.com/english/english/rsthda/rsthda.php">here</a> to compute <strong>t-test</strong> without any installation.
</div>
</div>
<div id="custom-function-for-multiple-t-test" class="section level1">
<h1>Custom function for multiple t-test</h1>
<p>We will use a the following custom R function :</p>
<pre class="r"><code># matrix of t-test
# mat : data.frame or matrix
# ... : further arguments to pass to the t.test function
multi.ttest <- function(mat, ...) {
  mat <- as.matrix(mat)
  n <- ncol(mat)
  p.mat<- matrix(NA, n, n)
  diag(p.mat) <- 1
  for (i in 1:(n - 1)) {
    for (j in (i + 1):n) {
      test <- t.test(mat[, i], mat[, j], ...)
      p.mat[i, j] <- p.mat[j, i] <- test$p.value
    }
  }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  signif(p.mat,4)
}</code></pre>
</div>
<div id="data-for-t-test" class="section level1">
<h1>Data for t-test</h1>
<p>The <code>mtcars</code> data is used in the following examples :</p>
<pre class="r"><code>head(mtcars)</code></pre>
<pre><code>                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1</code></pre>
<p>We will use the first seven columns for the <strong>t-test</strong>.</p>
</div>
<div id="compute-the-matrix-of-t-test" class="section level1">
<h1>Compute the matrix of t-test</h1>
<pre class="r"><code>p.mat<-multi.ttest(mtcars[,1:7])
p.mat</code></pre>
<pre><code>           mpg       cyl      disp        hp      drat        wt      qsec
mpg  1.000e+00 9.508e-15 7.978e-11 1.030e-11 3.164e-16 1.028e-16 5.107e-02
cyl  9.508e-15 1.000e+00 1.774e-11 8.322e-13 2.280e-09 9.118e-11 3.731e-35
disp 7.978e-11 1.774e-11 1.000e+00 1.546e-03 1.347e-11 1.293e-11 6.336e-11
hp   1.030e-11 8.322e-13 1.546e-03 1.000e+00 5.275e-13 4.919e-13 7.245e-12
drat 3.164e-16 2.280e-09 1.347e-11 5.275e-13 1.000e+00 6.025e-02 5.915e-33
wt   1.028e-16 9.118e-11 1.293e-11 4.919e-13 6.025e-02 1.000e+00 7.269e-39
qsec 5.107e-02 3.731e-35 6.336e-11 7.245e-12 5.915e-33 7.269e-39 1.000e+00</code></pre>
<p>The result is a matrix of <strong>p-value</strong> between all possible pairs of variables.</p>
</div>
<div id="keep-only-the-lower-triangle-of-the-matrix" class="section level1">
<h1>Keep only the lower triangle of the matrix</h1>
<p>The <strong>R</strong> function <strong>lower.tri()</strong> is used to hide the lower triangle of the matrix of the <strong>p-value</strong></p>
<pre class="r"><code># Hide upper triangle
upper<-p.mat
upper[upper.tri(p.mat)]<-""
upper<-as.data.frame(upper)
upper</code></pre>
<pre><code>           mpg       cyl      disp        hp      drat        wt qsec
mpg          1                                                       
cyl  9.508e-15         1                                             
disp 7.978e-11 1.774e-11         1                                   
hp    1.03e-11 8.322e-13  0.001546         1                         
drat 3.164e-16  2.28e-09 1.347e-11 5.275e-13         1               
wt   1.028e-16 9.118e-11 1.293e-11 4.919e-13   0.06025         1     
qsec   0.05107 3.731e-35 6.336e-11 7.245e-12 5.915e-33 7.269e-39    1</code></pre>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.1.0). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Mon, 03 Nov 2014 11:03:22 +0100</pubDate>
			
		</item>
		
	</channel>
</rss>
