R t test


t.test(...) function returns a t test result of two group data sets when the standard deviations are unknown. It's expression is:

t.test(x, y = NULL,
alternative = c("two.sided", "less", "greater"),
mu = 0, paired = FALSE, var.equal = FALSE,
conf.level = 0.95, ...)


x,y:Numeric vectors
alternative:Alternative Hypothesis
mu:True value of the mean
paired:Paired t-test or not
...
Suppose we have two datasets, let's do a two-sample t-test with hypothesis that the means of two samples are equal, H0: μ1 = μ2 against H1: μ1 ≠ μ2.

> x <- rnorm(1000, mean=2)
> y <- rnorm(1000, mean=4)
> z <- rnorm(3000, mean=2)

>ret <- t.test(x,y)

Welch Two Sample t-test
data:  x and y
t = -45.6653, df = 1997.725, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-2.170319 -1.991581
sample estimates:
mean of x mean of y
1.982353  4.063303

Since the p value < 2.2e-16, which is very small, the null hypothesis (the means of datasets x and y are equal) is rejected.

> t.text(x,z)

Welch Two Sample t-test
data:  x and z
t = -1.3574, df = 1676.653, p-value = 0.1748
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.12344264  0.02246504
sample estimates:
mean of x mean of y
1.982353  2.032842

Since the p value = 0.1748, which is greater than 0.05, the null hypothesis (the means of datasets x and z are equal) is accepted.

> t.test(uptake ~ Type,CO2)
        Welch Two Sample t-test
data:  uptake by Type
t = 6.5969, df = 78.533, p-value = 4.451e-09
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
8.839475 16.479572
sample estimates:
mean in group Quebec mean in group Mississippi
33.54286                  20.88333

Since the p value = 4.451e-09, which is very small, the null hypothesis (the means of CO2 uptake of Missippi and Quebec type grass are equal) is rejected.


We can also do a one sample t test, such as the dataset x with hypothesis H0: μ = 2 against H1: μ ≠ 2.

> t.test(x, mu=2)

One Sample t-test
data:  x
t = -0.5445, df = 999, p-value = 0.5862
alternative hypothesis: true mean is not equal to 2
95 percent confidence interval:
1.918752 2.045954
sample estimates:
mean of x
1.982353

Since the p value = 0.5862, which is much large than 0.05, the hypothesis is accepted that the mean is equal to 2.

Let's do a one sided t-test. For example, let's testing the dataset x with hypothesis H0: μ = 0 against H1: μ > 0.

> t.test(x, alternative=c("greater"))

One Sample t-test
data:  x
t = 61.163, df = 999, p-value < 2.2e-16
alternative hypothesis: true mean is greater than 0
95 percent confidence interval:
1.928992      Inf
sample estimates:
mean of x
1.982353

Since the p value < 2.2e-16, which is very small, the null hypothesis (the mean of dataset x is equal to zero) is rejected, and the alternative hypothesis that H1: μ > 0 is accepted.

> t.test(x, alternative=c("less"))
One Sample t-test
data:  x
t = 61.163, df = 999, p-value = 1
alternative hypothesis: true mean is less than 0
95 percent confidence interval:
-Inf 2.035714
sample estimates:
mean of x
1.982353

endmemo.com © 2024  | Terms of Use | Privacy | Home