In r, the T-test is implemented with the T.test () command, and different t-tests can be done by setting the options in the Command. The General options for this command are described first:
one, Single Sample t test
By setting the MU option, such as
> T.test (data2,mu=5)
One Sample t-test
Data:data2
t = 0.25482, df = P-value = 0.8023
Alternative hypothesis:true mean is not equal to 5
Percent Confidence interval:
4.079448 6.170552
Sample Estimates:
Mean of X
5.125
second, Homoscedastic and other time of the two-sample T test
By setting the var.equal=true, the calculation merges the variance and does not modify the degrees of freedom, as
> T.test (data2,data3,var.equal = TRUE)
Both Sample t-test
Data:data2 and Data3
t = -2.7908, df = p-value, = 0.009718
Alternative hypothesis:true difference in means are not equal to 0
Percent Confidence interval:
-3.5454233-0.5379101
Sample Estimates:
Mean of x mean of Y
5.125000 7.166667
two-sample t-test with unequal variance
By setting the var.equal=false, the calculations will correct T values and degrees of freedom, such as
> T.test (data2,data3,var.equal = FALSE)
Welch, Sample T-test
Data:data2 and Data3
t = -2.8151, df = 24.564, P-value = 0.009462
Alternative hypothesis:true difference in means are not equal to 0
Percent Confidence interval:
-3.5366789-0.5466544
Sample Estimates:
Mean of x mean of Y
5.125000 7.166667
four, paired t test
By setting the paired=true, it should be noted that the data paired with the T test require two columns of data to have the same length, otherwise it will be an Error. Such as
> T.test (data1,data2,paired = TRUE)
v. Using formula form syntax for T test
If the data structure is a digital vector, then you can directly use the above T.test () settings for analysis, but in many cases, our data will have categorical data, and these categorical data is basically a factor (that is, independent variables, Predictor variables), at this time, we get the data is the following form:
Rich Graze
1 Mow
2 Mow
3 Mow
4 Mow
5 Mow
6 8 Unmow
7 9 Unmow
8 7 Unmow
9 9 Unmow
For this form of data, when we use the t-test, we need to set the data to y~x form, such as
> T.test (rich~graze,data=grass)
Welch, Sample T-test
Data:rich by Graze
t = 4.8098, df = 5.4106, P-value = 0.003927
Alternative hypothesis:true difference in means are not equal to 0
Percent Confidence interval:
2.745758 8.754242
Sample Estimates:
Mean in group mow mean in group Unmow
14.00 8.25
If there are more than two categories of categorical data, you need to set the subset option to specify, as
> T.test (rich~graze,data=grass,subset=graze%in%c ("mow", "unmow"))
Welch, Sample T-test
Data:rich by Graze
t = 4.8098, df = 5.4106, P-value = 0.003927
Alternative hypothesis:true difference in means are not equal to 0
Percent Confidence interval:
2.745758 8.754242
Sample Estimates:
Mean in group mow mean in group Unmow
14.00 8.25
In the example above, we specify "mow" from the Predictor variable of graze, "unmow" two subsets are used for the t-test, where%in% means the subsequent categories are included in Graze.
T test of R language