tTests.R

Plain text source: tTests.R


# -*- Mode:R; Coding:us-ascii-unix; fill-column:160 -*-

################################################################################################################################################################
##
# @file      tTests.R
# @author    Mitch Richling <https://www.mitchr.me>
# @Copyright Copyright 2015 by Mitch Richling.  All rights reserved.
# @brief     t-test.@EOL
# @Keywords  t test ttest t-test base r
#

################################################################################################################################################################
popsz  <- 1000
mean12 <- 1
mean3  <- -1
group1 <- rnorm(popsz, mean=mean12)
group2 <- rnorm(popsz, mean=mean12)
group3 <- rnorm(popsz, mean=mean3)
allDat <- stack(list(group1=group1, group2=group2, group3=group3))
names(allDat) <- c('v', 'group')

################################################################################################################################################################
ggplot(data=allDat, aes(x=v, col=group)) + geom_density()
plot of chunk auto-report
################################################################################################################################################################
# Welch Two Sample t-test -- i.e. when you don't know the variance of the two populations is equal

t.test(group1, group2)
	Welch Two Sample t-test

data:  group1 and group2
t = 0.17237, df = 1996.1, p-value = 0.8632
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.08000203  0.09542031
sample estimates:
mean of x mean of y 
0.9938927 0.9861836 
################################################################################################################################################################
# Two Sample t-test -- i.e. when you DO know the variance of the two populations is equal

t.test(group1, group2, var.equal=TRUE)
	Two Sample t-test

data:  group1 and group2
t = 0.17237, df = 1998, p-value = 0.8632
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.08000198  0.09542026
sample estimates:
mean of x mean of y 
0.9938927 0.9861836 
################################################################################################################################################################
# Do a paired T-test -- i.e. when the measurements in each group are related pairwise.  For example, the data could be temperature measurements taken with two
# thermometers each hour.

t.test(group1, group2, paired=TRUE)
	Paired t-test

data:  group1 and group2
t = 0.17503, df = 999, p-value = 0.8611
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.07872017  0.09413845
sample estimates:
mean of the differences 
            0.007709139 
################################################################################################################################################################
# One Sample t-test for equality -- i.e. you want to know if the sample mean is equal to a hypothesized population mean

t.test(group1, mu=mean12)
	One Sample t-test

data:  group1
t = -0.19019, df = 999, p-value = 0.8492
alternative hypothesis: true mean is not equal to 1
95 percent confidence interval:
 0.9308782 1.0569072
sample estimates:
mean of x 
0.9938927 
t.test(group2, mu=mean12)
	One Sample t-test

data:  group2
t = -0.44383, df = 999, p-value = 0.6573
alternative hypothesis: true mean is not equal to 1
95 percent confidence interval:
 0.9250956 1.0472716
sample estimates:
mean of x 
0.9861836 
t.test(group3, mu=mean12)
	One Sample t-test

data:  group3
t = -64.352, df = 999, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 1
95 percent confidence interval:
 -1.1005414 -0.9762251
sample estimates:
mean of x 
-1.038383 
################################################################################################################################################################
# One Sample t-test inequality -- i.e. you want to know if the sample mean is less than a hypothesized population mean

t.test(group3, mu=mean12, alternative="greater")
	One Sample t-test

data:  group3
t = -64.352, df = 999, p-value = 1
alternative hypothesis: true mean is greater than 1
95 percent confidence interval:
 -1.090533       Inf
sample estimates:
mean of x 
-1.038383 
################################################################################################################################################################
# Wilcoxon single sample signed rank test -- i.e. you want to know if the sample mean is equal to a hypothesized population mean

wilcox.test(group1, mu=mean12)
	Wilcoxon signed rank test with continuity correction

data:  group1
V = 247560, p-value = 0.7688
alternative hypothesis: true location is not equal to 1
################################################################################################################################################################
# Wilcoxon two sample independent signed rank test -- i.e. when the measurements in each group are related pairwise.  This test is also known as the
# "independent 2-group Mann-Whitney U Test".  T-test above.

wilcox.test(group1, group2)
	Wilcoxon rank sum test with continuity correction

data:  group1 and group2
W = 498880, p-value = 0.9308
alternative hypothesis: true location shift is not equal to 0
################################################################################################################################################################
# Wilcoxon two sample paired signed rank test -- i.e. when the measurements in each group are related pairwise.  See the paired T-test above.

wilcox.test(group1, group2, paired=TRUE)
	Wilcoxon signed rank test with continuity correction

data:  group1 and group2
V = 253160, p-value = 0.7503
alternative hypothesis: true location shift is not equal to 0
################################################################################################################################################################
## The test functions have a formula interface.  Be careful to make sure that your grouping factor has precisely 2 levels.

t.test(v ~ group, subset(allDat, group %in% c('group1', 'group2')))
	Welch Two Sample t-test

data:  v by group
t = 0.17237, df = 1996.1, p-value = 0.8632
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.08000203  0.09542031
sample estimates:
mean in group group1 mean in group group2 
           0.9938927            0.9861836 

The R session information (including the OS info, R version and all packages used):

    options(width=80)
    sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 8 (jessie)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] graphics  grDevices datasets  utils     grid      stats     base     

other attached packages:
[1] RColorBrewer_1.1-2 reshape2_1.4.1     ggplot2_2.1.0      dplyr_0.4.3       
[5] data.table_1.9.6   gridExtra_2.2.1    knitr_1.13         lattice_0.20-33   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5      magrittr_1.5     munsell_0.4.3    colorspace_1.2-6
 [5] R6_2.1.2         stringr_1.0.0    highr_0.6        plyr_1.8.3      
 [9] tools_3.3.0      parallel_3.3.0   gtable_0.2.0     DBI_0.4-1       
[13] assertthat_0.1   digest_0.6.9     formatR_1.4      evaluate_0.9    
[17] labeling_0.3     stringi_1.0-1    methods_3.3.0    scales_0.4.0    
[21] chron_2.3-47    
    Sys.time()
[1] "2016-07-09 20:06:53 CDT"