{"id":10861,"date":"2019-11-28T23:45:24","date_gmt":"2019-11-28T21:45:24","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?post_type=dt_lessons&#038;p=10861"},"modified":"2019-11-28T23:46:10","modified_gmt":"2019-11-28T21:46:10","slug":"t-test-in-r","status":"publish","type":"dt_lessons","link":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/","title":{"rendered":"T-test in R"},"content":{"rendered":"<div id=\"rdoc\">\n<p>The <strong>t-test<\/strong> is used to compare two means.<\/p>\n<p>This chapter describes the different types of t-test, including:<\/p>\n<ul>\n<li><em>one-sample t-tests<\/em>,<\/li>\n<li><em>independent samples t-tests: Student\u2019s t-test and Welch\u2019s t-test<\/em><\/li>\n<li><em>paired samples t-test<\/em>.<\/li>\n<\/ul>\n<p>You will learn how to:<\/p>\n<ul>\n<li><strong>Compute the different t-tests in R<\/strong>. The pipe-friendly function <code>t_test()<\/code> [rstatix package] will be used.<\/li>\n<li><strong>Check t-test assumptions<\/strong><\/li>\n<li><strong>Calculate and report t-test effect size<\/strong> using <em>Cohen\u2019s d<\/em>. The <code>d<\/code> statistic redefines the difference in means as the number of standard deviations that separates those means. T-test conventional effect sizes, proposed by Cohen, are: 0.2 (small effect), 0.5 (moderate effect) and 0.8 (large effect) <span class=\"citation\">(Cohen 1998)<\/span>.<\/li>\n<\/ul>\n<p>Contents:<\/p>\n<div id=\"TOC\">\n<ul>\n<li><a href=\"#prerequisites\">Prerequisites<\/a><\/li>\n<li><a href=\"#one-sample-t-test\">One-Sample t-test<\/a>\n<ul>\n<li><a href=\"#demo-data\">Demo data<\/a><\/li>\n<li><a href=\"#summary-statistics\">Summary statistics<\/a><\/li>\n<li><a href=\"#visualization\">Visualization<\/a><\/li>\n<li><a href=\"#assumptions-and-preliminary-tests\">Assumptions and preliminary tests<\/a><\/li>\n<li><a href=\"#computation\">Computation<\/a><\/li>\n<li><a href=\"#effect-size\">Effect size<\/a><\/li>\n<li><a href=\"#report\">Report<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#independent-samples-t-test\">Independent samples t-test<\/a>\n<ul>\n<li><a href=\"#demo-data-1\">Demo data<\/a><\/li>\n<li><a href=\"#summary-statistics-1\">Summary statistics<\/a><\/li>\n<li><a href=\"#visualization-1\">Visualization<\/a><\/li>\n<li><a href=\"#assumptions-and-preliminary-tests-1\">Assumptions and preliminary tests<\/a><\/li>\n<li><a href=\"#computation-1\">Computation<\/a><\/li>\n<li><a href=\"#effect-size-1\">Effect size<\/a><\/li>\n<li><a href=\"#report-1\">Report<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#pstt\">Paired samples t-test<\/a>\n<ul>\n<li><a href=\"#demo-data-2\">Demo data<\/a><\/li>\n<li><a href=\"#summary-statistics-2\">Summary statistics<\/a><\/li>\n<li><a href=\"#visualization-2\">Visualization<\/a><\/li>\n<li><a href=\"#assumptions-and-preliminary-tests-2\">Assumptions and preliminary tests<\/a><\/li>\n<li><a href=\"#computation-2\">Computation<\/a><\/li>\n<li><a href=\"#effect-size-2\">Effect size<\/a><\/li>\n<li><a href=\"#report-2\">Report<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#summary\">Summary<\/a><\/li>\n<li><a href=\"#references\">References<\/a><\/li>\n<\/ul>\n<\/div>\n<div class='dt-sc-hr-invisible-medium  '><\/div>\n<div class='dt-sc-ico-content type1'><div class='custom-icon' ><a href='https:\/\/www.datanovia.com\/en\/product\/practical-statistics-in-r-for-comparing-groups-numerical-variables\/' target='_blank'><span class='fa fa-book'><\/span><\/a><\/div><h4><a href='https:\/\/www.datanovia.com\/en\/product\/practical-statistics-in-r-for-comparing-groups-numerical-variables\/' target='_blank'> Related Book <\/a><\/h4>Practical Statistics in R II - Comparing Groups: Numerical Variables<\/div>\n<div class='dt-sc-hr-invisible-medium  '><\/div>\n<div id=\"prerequisites\" class=\"section level2\">\n<h2>Prerequisites<\/h2>\n<p>Make sure you have installed the following R packages:<\/p>\n<ul>\n<li><code>tidyverse<\/code> for data manipulation and visualization<\/li>\n<li><code>ggpubr<\/code> for creating easily publication ready plots<\/li>\n<li><code>rstatix<\/code> provides pipe-friendly R functions for easy statistical analyses.<\/li>\n<li><code>datarium<\/code>: contains required data sets for this chapter.<\/li>\n<\/ul>\n<p>Start by loading the following required packages:<\/p>\n<pre class=\"r\"><code>library(tidyverse)\r\nlibrary(ggpubr)\r\nlibrary(rstatix)<\/code><\/pre>\n<\/div>\n<div id=\"one-sample-t-test\" class=\"section level2\">\n<h2>One-Sample t-test<\/h2>\n<p>The <strong>one-sample t-test<\/strong>, also known as the <em>single-parameter t test<\/em> or <em>single-sample t-test<\/em>, is used to compare the mean of one sample to a known standard (or theoretical \/ hypothetical) mean.<\/p>\n<p>Generally, the theoretical mean comes from:<\/p>\n<ul>\n<li>a previous experiment. For example, comparing whether the mean weight of mice differs from 200 mg, a value determined in a previous study.<\/li>\n<li>or from an experiment where you have control and treatment conditions. If you express your data as \u201cpercent of control\u201d, you can test whether the average value of treatment condition differs significantly from 100.<\/li>\n<\/ul>\n<div id=\"demo-data\" class=\"section level3\">\n<h3>Demo data<\/h3>\n<p>Demo dataset: <code>mice<\/code> [in datarium package]. Contains the weight of 10 mice:<\/p>\n<pre class=\"r\"><code># Load and inspect the data\r\ndata(mice, package = \"datarium\")\r\nhead(mice, 3)<\/code><\/pre>\n<pre><code>## # A tibble: 3 x 2\r\n##   name  weight\r\n##   &lt;chr&gt;  &lt;dbl&gt;\r\n## 1 M_1     18.9\r\n## 2 M_2     19.5\r\n## 3 M_3     23.1<\/code><\/pre>\n<\/div>\n<div id=\"summary-statistics\" class=\"section level3\">\n<h3>Summary statistics<\/h3>\n<p>Compute some summary statistics: count (number of subjects), mean and sd (standard deviation)<\/p>\n<pre class=\"r\"><code>mice %&gt;% get_summary_stats(weight, type = \"mean_sd\")<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 4\r\n##   variable     n  mean    sd\r\n##   &lt;chr&gt;    &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;\r\n## 1 weight      10  20.1  1.90<\/code><\/pre>\n<\/div>\n<div id=\"visualization\" class=\"section level3\">\n<h3>Visualization<\/h3>\n<p>Create a boxplot to visualize the distribution of mice weights. Add also jittered points to show individual observations. The big dot represents the mean point.<\/p>\n<pre class=\"r\"><code>bxp &lt;- ggboxplot(\r\n  mice$weight, width = 0.5, add = c(\"mean\", \"jitter\"), \r\n  ylab = \"Weight (g)\", xlab = FALSE\r\n  )\r\nbxp<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-one-sample-box-plot-1.png\" width=\"364.8\" \/><\/p>\n<\/div>\n<div id=\"assumptions-and-preliminary-tests\" class=\"section level3\">\n<h3>Assumptions and preliminary tests<\/h3>\n<p>The one-sample t-test assumes the following characteristics about the data:<\/p>\n<ul>\n<li><strong>No significant outliers<\/strong> in the data<\/li>\n<li><strong>Normality<\/strong>. the data should be approximately normally distributed<\/li>\n<\/ul>\n<p>In this section, we\u2019ll perform some preliminary tests to check whether these assumptions are met.<\/p>\n<div id=\"identify-outliers\" class=\"section level4\">\n<h4>Identify outliers<\/h4>\n<p>Outliers can be easily identified using boxplot methods, implemented in the R function <code>identify_outliers()<\/code> [rstatix package].<\/p>\n<pre class=\"r\"><code>mice %&gt;% identify_outliers(weight)<\/code><\/pre>\n<pre><code>## # A tibble: 0 x 4\r\n## # \u2026 with 4 variables: name &lt;chr&gt;, weight &lt;dbl&gt;, is.outlier &lt;lgl&gt;, is.extreme &lt;lgl&gt;<\/code><\/pre>\n<div class=\"success\">\n<p>There were no extreme outliers.<\/p>\n<\/div>\n<div class=\"warning\">\n<p>Note that, in the situation where you have extreme outliers, this can be due to: 1) data entry errors, measurement errors or unusual values.<\/p>\n<p>In this case, you could consider running the non parametric Wilcoxon test.<\/p>\n<\/div>\n<\/div>\n<div id=\"check-normality-assumption\" class=\"section level4\">\n<h4>Check normality assumption<\/h4>\n<p>The normality assumption can be checked by computing the Shapiro-Wilk test. If the data is normally distributed, the p-value should be greater than 0.05.<\/p>\n<pre class=\"r\"><code>mice %&gt;% shapiro_test(weight)<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 3\r\n##   variable statistic     p\r\n##   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;\r\n## 1 weight       0.923 0.382<\/code><\/pre>\n<div class=\"success\">\n<p>From the output, the p-value is greater than the significance level 0.05 indicating that the distribution of the data are not significantly different from the normal distribution. In other words, we can assume the normality.<\/p>\n<\/div>\n<p>You can also create a QQ plot of the <code>weight<\/code> data. QQ plot draws the correlation between a given data and the normal distribution.<\/p>\n<pre class=\"r\"><code>ggqqplot(mice, x = \"weight\")<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-qqplot-1.png\" width=\"288\" \/><\/p>\n<div class=\"success\">\n<p>All the points fall approximately along the (45-degree) reference line, for each group. So we can assume normality of the data.<\/p>\n<\/div>\n<div class=\"warning\">\n<p>Note that, if your sample size is greater than 50, the normal QQ plot is preferred because at larger sample sizes the Shapiro-Wilk test becomes very sensitive even to a minor deviation from normality.<\/p>\n<p>If the data are not normally distributed, it\u2019s recommended to use a non-parametric test such as the <em>one-sample Wilcoxon signed-rank test<\/em>. This test is similar to the one-sample t-test, but focuses on the median rather than the mean.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"computation\" class=\"section level3\">\n<h3>Computation<\/h3>\n<p>We want to know, whether the average weight of the mice differs from the 25g (two-tailed test)?<\/p>\n<pre class=\"r\"><code>stat.test &lt;- mice %&gt;% t_test(weight ~ 1, mu = 25)\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 7\r\n##   .y.    group1 group2         n statistic    df       p\r\n## * &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;      &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;   &lt;dbl&gt;\r\n## 1 weight 1      null model    10     -8.10     9 0.00002<\/code><\/pre>\n<p>The results above show the following components:<\/p>\n<ul>\n<li><code>.y.<\/code>: the outcome variable used in the test.<\/li>\n<li><code>group1,group2<\/code>: generally, the compared groups in the pairwise tests. Here, we have null model (one-sample test).<\/li>\n<li><code>statistic<\/code>: test statistic (t-value) used to compute the p-value.<\/li>\n<li><code>df<\/code>: degrees of freedom.<\/li>\n<li><code>p<\/code>: p-value.<\/li>\n<\/ul>\n<div class=\"warning\">\n<p>You can obtain a detailed result by specifying the option <code>detailed = TRUE<\/code> in the function <code>t_test()<\/code>.<\/p>\n<\/div>\n<\/div>\n<div id=\"effect-size\" class=\"section level3\">\n<h3>Effect size<\/h3>\n<p>To calculate an effect size, called <code>Cohen's d<\/code>, for the one-sample t-test you need to divide the mean difference by the standard deviation of the difference, as shown below. Note that, here: <code>sd(x-mu) = sd(x)<\/code>.<\/p>\n<p>Cohen\u2019s d formula:<\/p>\n<p><code>d = abs(mean(x) - mu)\/sd(x)<\/code>, where:<\/p>\n<ul>\n<li><code>x<\/code> is a numeric vector containing the data.<\/li>\n<li><code>mu<\/code> is the mean against which the mean of x is compared (default value is mu = 0).<\/li>\n<\/ul>\n<p>Calculation:<\/p>\n<pre class=\"r\"><code>mice %&gt;% cohens_d(weight ~ 1, mu = 25)<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 6\r\n##   .y.    group1 group2     effsize     n magnitude\r\n## * &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;        &lt;dbl&gt; &lt;int&gt; &lt;ord&gt;    \r\n## 1 weight 1      null model    10.6    10 large<\/code><\/pre>\n<div class=\"success\">\n<p>Recall that, t-test conventional effect sizes, proposed by Cohen J. (1998), are: 0.2 (small effect), 0.5 (moderate effect) and 0.8 (large effect) (Cohen 1998). As the effect size, d, is 2.56 you can conclude that there is a large effect.<\/p>\n<\/div>\n<\/div>\n<div id=\"report\" class=\"section level3\">\n<h3>Report<\/h3>\n<p>We could report the result as follow:<\/p>\n<p>A one-sample t-test was computed to determine whether the recruited mice average weight was different to the population normal mean weight (25g).<\/p>\n<p>The mice weight value were normally distributed, as assessed by Shapiro-Wilk\u2019s test (p &gt; 0.05) and there were no extreme outliers in the data, as assessed by boxplot method.<\/p>\n<p>The measured mice mean weight (20.14 +\/- 1.94) was statistically significantly lower than the population normal mean weight 25 (<code>t(9) = -8.1, p &lt; 0.0001, d = 2.56<\/code>); where t(9) is shorthand notation for a t-statistic that has 9 degrees of freedom.<\/p>\n<p>Create a box plot with p-value:<\/p>\n<pre class=\"r\"><code>bxp + labs(\r\n  subtitle = get_test_label(stat.test, detailed = TRUE)\r\n  )<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-one-sample-box-plot-with-p-value-1.png\" width=\"336\" \/><\/p>\n<p>Create a density plot with p-value:<\/p>\n<ul>\n<li>Red line corresponds to the observed mean<\/li>\n<li>Blue line corresponds to the theoretical mean<\/li>\n<\/ul>\n<pre class=\"r\"><code>ggdensity(mice, x = \"weight\", rug = TRUE, fill = \"lightgray\") +\r\n  scale_x_continuous(limits = c(15, 27)) +\r\n  stat_central_tendency(type = \"mean\", color = \"red\", linetype = \"dashed\") +\r\n  geom_vline(xintercept = 25, color = \"blue\", linetype = \"dashed\") + \r\n  labs(subtitle = get_test_label(stat.test,  detailed = TRUE))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-one-sample-density-with-p-value-1.png\" width=\"384\" \/><\/p>\n<\/div>\n<\/div>\n<div id=\"independent-samples-t-test\" class=\"section level2\">\n<h2>Independent samples t-test<\/h2>\n<p>The <strong>independent samples t-test<\/strong> (or unpaired samples t-test) is used to compare the mean of two independent groups.<\/p>\n<p>For example, you might want to compare the average weights of individuals grouped by gender: male and female groups, which are two unrelated\/independent groups.<\/p>\n<p>The independent samples t-test comes in two different forms:<\/p>\n<ul>\n<li>the standard <em>Student\u2019s t-test<\/em>, which assumes that the variance of the two groups are equal.<\/li>\n<li>the <em>Welch\u2019s t-test<\/em>, which is less restrictive compared to the original Student\u2019s test. This is the test where you do not assume that the variance is the same in the two groups, which results in the fractional degrees of freedom.<\/li>\n<\/ul>\n<div class=\"warning\">\n<p>By default, R computes the Weltch t-test, which is the safer one. The two methods give very similar results unless both the group sizes and the standard deviations are very different.<\/p>\n<\/div>\n<div id=\"demo-data-1\" class=\"section level3\">\n<h3>Demo data<\/h3>\n<p>Demo dataset: <code>genderweight<\/code> [in datarium package] containing the weight of 40 individuals (20 women and 20 men).<\/p>\n<p>Load the data and show some random rows by groups:<\/p>\n<pre class=\"r\"><code># Load the data\r\ndata(\"genderweight\", package = \"datarium\")\r\n# Show a sample of the data by group\r\nset.seed(123)\r\ngenderweight %&gt;% sample_n_by(group, size = 2)<\/code><\/pre>\n<pre><code>## # A tibble: 4 x 3\r\n##   id    group weight\r\n##   &lt;fct&gt; &lt;fct&gt;  &lt;dbl&gt;\r\n## 1 6     F       65.0\r\n## 2 15    F       65.9\r\n## 3 29    M       88.9\r\n## 4 37    M       77.0<\/code><\/pre>\n<\/div>\n<div id=\"summary-statistics-1\" class=\"section level3\">\n<h3>Summary statistics<\/h3>\n<p>Compute some summary statistics by groups: mean and sd (standard deviation)<\/p>\n<pre class=\"r\"><code>genderweight %&gt;%\r\n  group_by(group) %&gt;%\r\n  get_summary_stats(weight, type = \"mean_sd\")<\/code><\/pre>\n<pre><code>## # A tibble: 2 x 5\r\n##   group variable     n  mean    sd\r\n##   &lt;fct&gt; &lt;chr&gt;    &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;\r\n## 1 F     weight      20  63.5  2.03\r\n## 2 M     weight      20  85.8  4.35<\/code><\/pre>\n<\/div>\n<div id=\"visualization-1\" class=\"section level3\">\n<h3>Visualization<\/h3>\n<p>Visualize the data using box plots. Plot weight by groups.<\/p>\n<pre class=\"r\"><code>bxp &lt;- ggboxplot(\r\n  genderweight, x = \"group\", y = \"weight\", \r\n  ylab = \"Weight\", xlab = \"Groups\", add = \"jitter\"\r\n  )\r\nbxp<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-box-plot-two-samples-1.png\" width=\"364.8\" \/><\/p>\n<\/div>\n<div id=\"assumptions-and-preliminary-tests-1\" class=\"section level3\">\n<h3>Assumptions and preliminary tests<\/h3>\n<p>The two-samples independent t-test assume the following characteristics about the data:<\/p>\n<ul>\n<li><strong>Independence of the observations<\/strong>. Each subject should belong to only one group. There is no relationship between the observations in each group.<\/li>\n<li><strong>No significant outliers<\/strong> in the two groups<\/li>\n<li><strong>Normality<\/strong>. the data for each group should be approximately normally distributed.<\/li>\n<li><strong>Homogeneity of variances<\/strong>. the variance of the outcome variable should be equal in each group.<\/li>\n<\/ul>\n<p>In this section, we\u2019ll perform some preliminary tests to check whether these assumptions are met.<\/p>\n<div id=\"identify-outliers-by-groups\" class=\"section level4\">\n<h4>Identify outliers by groups<\/h4>\n<pre class=\"r\"><code>genderweight %&gt;%\r\n  group_by(group) %&gt;%\r\n  identify_outliers(weight)<\/code><\/pre>\n<pre><code>## # A tibble: 2 x 5\r\n##   group id    weight is.outlier is.extreme\r\n##   &lt;fct&gt; &lt;fct&gt;  &lt;dbl&gt; &lt;lgl&gt;      &lt;lgl&gt;     \r\n## 1 F     20      68.8 TRUE       FALSE     \r\n## 2 M     31      95.1 TRUE       FALSE<\/code><\/pre>\n<div class=\"success\">\n<p>There were no extreme outliers.<\/p>\n<\/div>\n<\/div>\n<div id=\"check-normality-by-groups\" class=\"section level4\">\n<h4>Check normality by groups<\/h4>\n<pre class=\"r\"><code># Compute Shapiro wilk test by goups\r\ndata(genderweight, package = \"datarium\")\r\ngenderweight %&gt;%\r\n  group_by(group) %&gt;%\r\n  shapiro_test(weight)<\/code><\/pre>\n<pre><code>## # A tibble: 2 x 4\r\n##   group variable statistic     p\r\n##   &lt;fct&gt; &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;\r\n## 1 F     weight       0.938 0.224\r\n## 2 M     weight       0.986 0.989<\/code><\/pre>\n<pre class=\"r\"><code># Draw a qq plot by group\r\nggqqplot(genderweight, x = \"weight\", facet.by = \"group\")<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-independent-samples-normality-assumption-1.png\" width=\"480\" \/><\/p>\n<div class=\"success\">\n<p>From the output above, we can conclude that the data of the two groups are normally distributed.<\/p>\n<\/div>\n<\/div>\n<div id=\"check-the-equality-of-variances\" class=\"section level4\">\n<h4>Check the equality of variances<\/h4>\n<p>This can be done using the Levene\u2019s test. If the variances of groups are equal, the p-value should be greater than 0.05.<\/p>\n<pre class=\"r\"><code>genderweight %&gt;% levene_test(weight ~ group)<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 4\r\n##     df1   df2 statistic      p\r\n##   &lt;int&gt; &lt;int&gt;     &lt;dbl&gt;  &lt;dbl&gt;\r\n## 1     1    38      6.12 0.0180<\/code><\/pre>\n<div class=\"success\">\n<p>The p-value of the Levene\u2019s test is significant, suggesting that there is a significant difference between the variances of the two groups. Therefore, we\u2019ll use the Weltch t-test, which doesn\u2019t assume the equality of the two variances.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"computation-1\" class=\"section level3\">\n<h3>Computation<\/h3>\n<p>We want to know, whether the average weights are different between groups.<\/p>\n<p>Recall that, by default, R computes the Weltch t-test, which is the safer one:<\/p>\n<pre class=\"r\"><code>stat.test &lt;- genderweight %&gt;% \r\n  t_test(weight ~ group) %&gt;%\r\n  add_significance()\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 9\r\n##   .y.    group1 group2    n1    n2 statistic    df        p p.signif\r\n##   &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt; &lt;chr&gt;   \r\n## 1 weight F      M         20    20     -20.8  26.9 4.30e-18 ****<\/code><\/pre>\n<p>If you want to assume the equality of variances (Student t-test), specify the option <code>var.equal = TRUE<\/code>:<\/p>\n<pre class=\"r\"><code>stat.test2 &lt;- genderweight %&gt;%\r\n  t_test(weight ~ group, var.equal = TRUE) %&gt;%\r\n  add_significance()\r\nstat.test2<\/code><\/pre>\n<p>The output is similar to the result of one-sample test. Recall that, more details can be obtained by specifying the option <code>detailed = TRUE<\/code> in the function <code>t_test()<\/code>. The p-value of the comparison is significant (p &lt; 0.0001).<\/p>\n<\/div>\n<div id=\"effect-size-1\" class=\"section level3\">\n<h3>Effect size<\/h3>\n<div id=\"cohens-d-for-student-t-test\" class=\"section level4\">\n<h4>Cohen\u2019s d for Student t-test<\/h4>\n<p>This effect size is calculated by dividing the mean difference between the groups by the pooled standard deviation.<\/p>\n<p>Cohen\u2019s d formula:<\/p>\n<p><code>d = (mean1 - mean2)\/pooled.sd<\/code>, where:<\/p>\n<ul>\n<li><code>pooled.sd<\/code> is the common standard deviation of the two groups. <code>pooled.sd = sqrt([var1*(n1-1) + var2*(n2-1)]\/[n1 + n2 -2])<\/code>;<\/li>\n<li><code>var1<\/code> and <code>var2<\/code> are the variances (squared standard deviation) of group1 and 2, respectively.<\/li>\n<li><code>n1<\/code> and <code>n2<\/code> are the sample counts for group 1 and 2, respectively.<\/li>\n<li><code>mean1<\/code> and <code>mean2<\/code> are the means of each group, respectively.<\/li>\n<\/ul>\n<p>Calculation:<\/p>\n<pre class=\"r\"><code>genderweight %&gt;%  cohens_d(weight ~ group, var.equal = TRUE)<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 7\r\n##   .y.    group1 group2 effsize    n1    n2 magnitude\r\n## * &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;    &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;ord&gt;    \r\n## 1 weight F      M         6.57    20    20 large<\/code><\/pre>\n<div class=\"success\">\n<p>There is a large effect size, d = 6.57.<\/p>\n<\/div>\n<\/div>\n<div id=\"cohens-d-for-welch-t-test\" class=\"section level4\">\n<h4>Cohen\u2019s d for Welch t-test<\/h4>\n<p>The Welch test is a variant of t-test used when the equality of variance can\u2019t be assumed. The effect size can be computed by dividing the mean difference between the groups by the \u201caveraged\u201d standard deviation.<\/p>\n<p>Cohen\u2019s d formula:<\/p>\n<p><code>d = (mean1 - mean2)\/sqrt((var1 + var2)\/2)<\/code>, where:<\/p>\n<ul>\n<li><code>mean1<\/code> and <code>mean2<\/code> are the means of each group, respectively<\/li>\n<li><code>var1<\/code> and <code>var2<\/code> are the variance of the two groups.<\/li>\n<\/ul>\n<p>Calculation:<\/p>\n<pre class=\"r\"><code>genderweight %&gt;% cohens_d(weight ~ group, var.equal = FALSE)<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 7\r\n##   .y.    group1 group2 effsize    n1    n2 magnitude\r\n## * &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;    &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;ord&gt;    \r\n## 1 weight F      M         6.57    20    20 large<\/code><\/pre>\n<div class=\"warning\">\n<p>Note that, when group sizes are equal and group variances are homogeneous, Cohen\u2019s d for the standard Student and Welch t-tests are identical.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"report-1\" class=\"section level3\">\n<h3>Report<\/h3>\n<p>We could report the result as follow:<\/p>\n<p>The mean weight in female group was 63.5 (SD = 2.03), whereas the mean in male group was 85.8 (SD = 4.3). A Welch two-samples t-test showed that the difference was statistically significant, t(26.9) = -20.8, p &lt; 0.0001, d = 6.57; where, t(26.9) is shorthand notation for a Welch t-statistic that has 26.9 degrees of freedom.<\/p>\n<pre class=\"r\"><code>stat.test &lt;- stat.test %&gt;% add_xy_position(x = \"group\")\r\nbxp + \r\n  stat_pvalue_manual(stat.test, tip.length = 0) +\r\n  labs(subtitle = get_test_label(stat.test, detailed = TRUE))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-two-sample-box-plot-with-p-values-1.png\" width=\"412.8\" \/><\/p>\n<\/div>\n<\/div>\n<div id=\"pstt\" class=\"section level2\">\n<h2>Paired samples t-test<\/h2>\n<p>The <strong>paired sample t-test<\/strong> is used to compare the means of two related groups of samples. Put into another words, it\u2019s used in a situation where you have two pairs of values measured for the same samples.<\/p>\n<p>For example, you might want to compare the average weight of 20 mice before and after treatment. The data contain 20 sets of values before treatment and 20 sets of values after treatment from measuring twice the weight of the same mice. In such situations, paired t-test can be used to compare the mean weights before and after treatment.<\/p>\n<div id=\"demo-data-2\" class=\"section level3\">\n<h3>Demo data<\/h3>\n<p>Here, we\u2019ll use a demo dataset <code>mice2<\/code> [datarium package], which contains the weight of 10 mice before and after the treatment.<\/p>\n<pre class=\"r\"><code># Wide format\r\ndata(\"mice2\", package = \"datarium\")\r\nhead(mice2, 3)<\/code><\/pre>\n<pre><code>##   id before after\r\n## 1  1    187   430\r\n## 2  2    194   404\r\n## 3  3    232   406<\/code><\/pre>\n<pre class=\"r\"><code># Transform into long data: \r\n# gather the before and after values in the same column\r\nmice2.long &lt;- mice2 %&gt;%\r\n  gather(key = \"group\", value = \"weight\", before, after)\r\nhead(mice2.long, 3)<\/code><\/pre>\n<pre><code>##   id  group weight\r\n## 1  1 before    187\r\n## 2  2 before    194\r\n## 3  3 before    232<\/code><\/pre>\n<\/div>\n<div id=\"summary-statistics-2\" class=\"section level3\">\n<h3>Summary statistics<\/h3>\n<p>Compute some summary statistics (mean and sd) by groups:<\/p>\n<pre class=\"r\"><code>mice2.long %&gt;%\r\n  group_by(group) %&gt;%\r\n  get_summary_stats(weight, type = \"mean_sd\")<\/code><\/pre>\n<pre><code>## # A tibble: 2 x 5\r\n##   group  variable     n  mean    sd\r\n##   &lt;chr&gt;  &lt;chr&gt;    &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;\r\n## 1 after  weight      10  400.  30.1\r\n## 2 before weight      10  201.  20.0<\/code><\/pre>\n<\/div>\n<div id=\"visualization-2\" class=\"section level3\">\n<h3>Visualization<\/h3>\n<pre class=\"r\"><code>bxp &lt;- ggpaired(mice2.long, x = \"group\", y = \"weight\", \r\n         order = c(\"before\", \"after\"),\r\n         ylab = \"Weight\", xlab = \"Groups\")\r\nbxp<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-paired-t-test-box-plot-1.png\" width=\"364.8\" \/><\/p>\n<\/div>\n<div id=\"assumptions-and-preliminary-tests-2\" class=\"section level3\">\n<h3>Assumptions and preliminary tests<\/h3>\n<p>The paired samples t-test assume the following characteristics about the data:<\/p>\n<ul>\n<li><strong>the two groups are paired<\/strong>. In our example, this is the case since the data have been collected from measuring twice the weight of the same mice.<\/li>\n<li><strong>No significant outliers<\/strong> in the difference between the two related groups<\/li>\n<li><strong>Normality<\/strong>. the difference of pairs follow a normal distribution.<\/li>\n<\/ul>\n<p>In this section, we\u2019ll perform some preliminary tests to check whether these assumptions are met.<\/p>\n<p>First, start by computing the difference between groups:<\/p>\n<pre class=\"r\"><code>mice2 &lt;- mice2 %&gt;% mutate(differences = before - after)\r\nhead(mice2, 3)<\/code><\/pre>\n<pre><code>##   id before after differences\r\n## 1  1    187   430        -242\r\n## 2  2    194   404        -210\r\n## 3  3    232   406        -174<\/code><\/pre>\n<div id=\"identify-outliers-1\" class=\"section level4\">\n<h4>Identify outliers<\/h4>\n<pre class=\"r\"><code>mice2 %&gt;% identify_outliers(differences)<\/code><\/pre>\n<pre><code>## [1] id          before      after       differences is.outlier  is.extreme \r\n## &lt;0 rows&gt; (or 0-length row.names)<\/code><\/pre>\n<div class=\"success\">\n<p>There were no extreme outliers.<\/p>\n<\/div>\n<\/div>\n<div id=\"check-normality-assumption-1\" class=\"section level4\">\n<h4>Check normality assumption<\/h4>\n<pre class=\"r\"><code># Shapiro-Wilk normality test for the differences\r\nmice2 %&gt;% shapiro_test(differences) <\/code><\/pre>\n<pre><code>## # A tibble: 1 x 3\r\n##   variable    statistic     p\r\n##   &lt;chr&gt;           &lt;dbl&gt; &lt;dbl&gt;\r\n## 1 differences     0.968 0.867<\/code><\/pre>\n<pre class=\"r\"><code># QQ plot for the difference\r\nggqqplot(mice2, \"differences\")<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-paired-samples-normality-assumption-1.png\" width=\"288\" \/><\/p>\n<div class=\"success\">\n<p>From the output above, it can be assumed that the differences are normally distributed.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"computation-2\" class=\"section level3\">\n<h3>Computation<\/h3>\n<p>We want to know, if there is any significant difference in the mean weights after treatment?<\/p>\n<pre class=\"r\"><code>stat.test &lt;- mice2.long  %&gt;% \r\n  t_test(weight ~ group, paired = TRUE) %&gt;%\r\n  add_significance()\r\nstat.test<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 9\r\n##   .y.    group1 group2    n1    n2 statistic    df             p p.signif\r\n##   &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;  &lt;int&gt; &lt;int&gt;     &lt;dbl&gt; &lt;dbl&gt;         &lt;dbl&gt; &lt;chr&gt;   \r\n## 1 weight after  before    10    10      25.5     9 0.00000000104 ****<\/code><\/pre>\n<p>The output is similar to that of a one-sample t-test. Again, more details can be obtained by specifying the option <code>detailed = TRUE<\/code> in the function <code>t_test()<\/code>.<\/p>\n<\/div>\n<div id=\"effect-size-2\" class=\"section level3\">\n<h3>Effect size<\/h3>\n<p>The effect size for a paired-samples t-test can be calculated by dividing the mean difference by the standard deviation of the difference, as shown below.<\/p>\n<p>Cohen\u2019s formula:<\/p>\n<p><code>d = mean(D)\/sd(D)<\/code>, where <code>D<\/code> is the differences of the paired samples values.<\/p>\n<p>Calculation:<\/p>\n<pre class=\"r\"><code>mice2.long  %&gt;% cohens_d(weight ~ group, paired = TRUE)<\/code><\/pre>\n<pre><code>## # A tibble: 1 x 7\r\n##   .y.    group1 group2 effsize    n1    n2 magnitude\r\n## * &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;    &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;ord&gt;    \r\n## 1 weight after  before    8.08    10    10 large<\/code><\/pre>\n<div class=\"success\">\n<p>There is a large effect size, Cohen\u2019s d = 8.07.<\/p>\n<\/div>\n<\/div>\n<div id=\"report-2\" class=\"section level3\">\n<h3>Report<\/h3>\n<p>We could report the result as follow: The average weight of mice was significantly increased after treatment, t(9) = 25.5, p &lt; 0.0001, d = 8.07.<\/p>\n<pre class=\"r\"><code>stat.test &lt;- stat.test %&gt;% add_xy_position(x = \"group\")\r\nbxp + \r\n  stat_pvalue_manual(stat.test, tip.length = 0) +\r\n  labs(subtitle = get_test_label(stat.test, detailed= TRUE))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/r-statistics-2-comparing-groups-means\/figures\/031-t-test-paired-t-test-box-plot-with-p-values-1.png\" width=\"364.8\" \/><\/p>\n<\/div>\n<\/div>\n<div id=\"summary\" class=\"section level2\">\n<h2>Summary<\/h2>\n<p>This chapter describes how to compare two means in R using t-test. Quick start R codes, to compute the different t-tests, are:<\/p>\n<pre class=\"r\"><code># One-sample t-test\r\nmice %&gt;% t_test(weight ~ 1, mu = 25)\r\n# Independent samples t-test\r\ngenderweight %&gt;% t_test(weight ~ group)\r\n# Paired sample t-test\r\nmice2.long %&gt;% t_test(weight ~ group, paired = TRUE)<\/code><\/pre>\n<p>Note that, to compute one-sided t-tests, you can specify the option <code>alternative<\/code>, which possible values can be \u201cgreater\u201d, \u201cless\u201d or \u201ctwo.sided\u201d.<\/p>\n<p>We also explain the assumptions made by the t-test and provide practical examples of R codes to check whether the test assumptions are met.<\/p>\n<p>The <strong>t-test assumptions<\/strong> can be summarized as follow:<\/p>\n<ul>\n<li>One-sample t-test:\n<ul>\n<li>No significant outliers in the data<\/li>\n<li>the data should be normally distributed.<\/li>\n<\/ul>\n<\/li>\n<li>Independent sample t-test:\n<ul>\n<li>No significant outliers in the groups<\/li>\n<li>the two groups of samples (A and B), being compared, should be normally distributed.<\/li>\n<li>the variances of the two groups should not be significantly different. This assumption is made only by the original Student\u2019s t-test. It is relaxed in the Welch\u2019s t-test.<\/li>\n<\/ul>\n<\/li>\n<li>Paired sample t-test:\n<ul>\n<li>No significant outliers in the differences between groups<\/li>\n<li>the difference of pairs should follow a normal distribution.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Assessing normality<\/strong>. With large enough samples size (n &gt; 30) the violation of the normality assumption should not cause major problems (according to the central limit theorem). This implies that we can ignore the distribution of the data and use parametric tests. However, to be consistent, the Shapiro-Wilk test can be used to ascertain whether data show or not a serious deviation from normality (See Chapter @ref(normality-test-in-r)).<\/p>\n<p><strong>Assessing equality of variances<\/strong>. Homogeneity of variances can be checked using the Levene\u2019s test. Note that, by default, the <code>t_test()<\/code> function does not assume equal variances; instead of the standard Student\u2019s t-test, it uses the Welch t-test by default, which is the considered the safer one. To use Student\u2019s t-test, set <code>var.equal = TRUE<\/code>. The two methods give very similar results unless both the group sizes and the standard deviations are very different.<\/p>\n<div class=\"warning\">\n<p>In the situations where the assumptions are violated, non-parametric tests, such as Wilcoxon test, are recommended.<\/p>\n<\/div>\n<\/div>\n<div id=\"references\" class=\"section level2 unnumbered\">\n<h2>References<\/h2>\n<div id=\"refs\" class=\"references\">\n<div id=\"ref-cohen1998\">\n<p>Cohen, J. 1998. <em>Statistical Power Analysis for the Behavioral Sciences<\/em>. 2nd ed. Hillsdale, NJ: Lawrence Erlbaum Associates.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This chapter describes how to compute and interpret the different t-test in R including: one-sample t-test, independent samples t-test and paired samples t-test.<\/p>\n","protected":false},"author":1,"featured_media":9096,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","class_list":["post-10861","dt_lessons","type-dt_lessons","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>T-test in R: The Ultimate Guide - Datanovia<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"T-test in R: The Ultimate Guide - Datanovia\" \/>\n<meta property=\"og:description\" content=\"This chapter describes how to compute and interpret the different t-test in R including: one-sample t-test, independent samples t-test and paired samples t-test.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-28T21:46:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26231190_568106810195934_3157394220874143808_n.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/\",\"name\":\"T-test in R: The Ultimate Guide - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26231190_568106810195934_3157394220874143808_n.jpg\",\"datePublished\":\"2019-11-28T21:45:24+00:00\",\"dateModified\":\"2019-11-28T21:46:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26231190_568106810195934_3157394220874143808_n.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26231190_568106810195934_3157394220874143808_n.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Lessons\",\"item\":\"https:\/\/www.datanovia.com\/en\/lessons\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"T-test in R\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"name\":\"Datanovia\",\"description\":\"Data Mining and Statistics for Decision Support\",\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.datanovia.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\",\"name\":\"Datanovia\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"width\":98,\"height\":99,\"caption\":\"Datanovia\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"T-test in R: The Ultimate Guide - Datanovia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/","og_locale":"en_US","og_type":"article","og_title":"T-test in R: The Ultimate Guide - Datanovia","og_description":"This chapter describes how to compute and interpret the different t-test in R including: one-sample t-test, independent samples t-test and paired samples t-test.","og_url":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/","og_site_name":"Datanovia","article_modified_time":"2019-11-28T21:46:10+00:00","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26231190_568106810195934_3157394220874143808_n.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/","url":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/","name":"T-test in R: The Ultimate Guide - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26231190_568106810195934_3157394220874143808_n.jpg","datePublished":"2019-11-28T21:45:24+00:00","dateModified":"2019-11-28T21:46:10+00:00","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26231190_568106810195934_3157394220874143808_n.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/X26231190_568106810195934_3157394220874143808_n.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/lessons\/t-test-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"Lessons","item":"https:\/\/www.datanovia.com\/en\/lessons\/"},{"@type":"ListItem","position":3,"name":"T-test in R"}]},{"@type":"WebSite","@id":"https:\/\/www.datanovia.com\/en\/#website","url":"https:\/\/www.datanovia.com\/en\/","name":"Datanovia","description":"Data Mining and Statistics for Decision Support","publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.datanovia.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.datanovia.com\/en\/#organization","name":"Datanovia","url":"https:\/\/www.datanovia.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","width":98,"height":99,"caption":"Datanovia"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/"}}]}},"multi-rating":{"mr_rating_results":[]},"_links":{"self":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/10861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons"}],"about":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/types\/dt_lessons"}],"author":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/comments?post=10861"}],"version-history":[{"count":0,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/10861\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/9096"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=10861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}