First we will conduct an independent two sample t-test i.e. the samples are from two different sets of participants. We simply use the two numeric vectors that we wish to test as the inputs to the
t.test() function. In the example below we can see that the means are significantly different
Input: data <- data.frame(ID=seq(1,25,1), ScoreOne=rpois(25,20),ScoreTwo=rpois(25,35)) t.test(data$ScoreOne,data$ScoreTwo) Output: Welch Two Sample t-test data: data$ScoreOne and data$ScoreTwo t = -8.4501, df = 36.02, p-value = 4.539e-10 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -18.55044 -11.36956 sample estimates: mean of x mean of y 20.80 35.76
The most common way to visualise data relating to a t.test is to use a box plot as this plot type visualises the mean, range, quartiles and overlap of the data. Boxplots are created using the
Input: boxplot(data$ScoreOne,data$ScoreTwo) Output:
If we are comparing the means of data produced by the same participants we use a paired t-test. The only difference to the independent t-test is that we use the argument
paired=TRUE. In the example below we can see that the means of the two numeric vectors that make up our data are not significantly different.
Input: dataP <- data.frame(ID=seq(1,25,1), Age=sample(18:99,25,replace=TRUE), Gender=sample(1:2,25,replace=TRUE), ScoreOne=sample(0:50,25,replace=TRUE), ScoreTwo=sample(0:50,25,replace=TRUE)) t.test(dataP$ScoreOne,dataP$ScoreTwo,paired=TRUE) Output: Paired t-test data: dataP$ScoreOne and dataP$ScoreTwo t = -1.0953, df = 24, p-value = 0.2843 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -14.306582 4.386582 sample estimates: mean of the differences -4.96
Plotting our data as a boxplot confirms that the means are close together and there is a great deal of overlap in the data even though the inter-quartile spread of the data is different.
Input: boxplot(dataP$ScoreOne,dataP$ScoreTwo) Output: