## Independent t-test in R

As scientists, we often want to know if the difference between two groups is important or significant.

For example, you may have data on leg strength from students who came to class wearing dress shoes or running shoes. How would you decide if there was a difference in strength between these two groups? How would you quantify the size of this difference?

As one of the most widely used statistical tests in science, the student *t*-test should be familiar to most readers. In this post, we will learn how to perform an independent *t*-test in R. “Independent” refers to the fact that the data we are comparing comes from two independent groups.

### The dataset: Environmental impact of pork and beef

We will first create a fictional dataset on the environmental impact (measured in kilograms of carbon dioxide) of pork and beef production.

```
# Number of animals in each group
n = 30
names=c(rep("pork", n) , rep("beef", n))
# Draw samples from two different populations
value=c(sample(0:500, n, replace=T), sample(100:600, n, replace=T))
# Create a dataframe
data=data.frame(names,value)
```

The data we created is shown in the figure below.

### Difference between two independent groups

Our figure indicates that, for the current sample of beef and pork, beef has approximately 100 kg more environmental impact. We can verify this by using the `summary`

command:

```
summary(data$value[data$names=='pork'])
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0 163.0 271.0 262.0 396.2 476.0
summary(data$value[data$names=='beef'])
Min. 1st Qu. Median Mean 3rd Qu. Max.
115.0 273.0 370.5 353.6 452.0 593.0
```

Our visual inspection was pretty close. The mean value for the pork group is 262 kg whereas it is 353.6 kg for the beef group.

To test whether this difference is statistically significant, we will perform an independent *t*-test. The basic structure of the test is `t.test(group1_data, group2_data)`

. Here is how to apply it to our data:

```
t.test(data$value[data$names=='pork'], data$value[data$names=='beef'])
```

The output of this test is:

```
Welch Two Sample t-test
data: data$value[data$names == "pork"] and data$value[data$names == "beef"]
t = -2.3774, df = 57.304, p-value = 0.02079
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-168.68251 -14.45082
sample estimates:
mean of x mean of y
262.0333 353.6000
```

### Interpreting the output

The output of the *t*-test is quite informative. It provides us with the t-value (`t`

), the degrees of freedom for the test (`df`

), and the `p-value`

. Because the `p-value`

is below the threshold of 0.05, we can report that there is a statistically significant difference in the environmental impact between beef and pork.

The R output also provides us with the 95% confidence interval of the difference between groups as well as the mean of each group. Based on these values, we can conclude that the mean [95% CI] difference in kilograms of carbon dioxide between beef and pork is 91.6 [14.5 to 168.7]. This means that if we carried out 100 experiments, we would expect the mean difference of 95 of these experiments to be between 14.5 and 168.7 kg. An alternative interpretation is that we can be 95% certain that the (true) difference in means between the population of all beef and all pork falls between 14.5 and 168.7 kg.

I am no expert in the field of environmental impact so it is difficult for me to interpret these values (especially since I made them up!). However, the 95% confidence interval seems large. A true difference of 15 kg might be inconsequential and of no scientific interest, whereas a difference of 169 kg might be cause for concern. One way to help readers understand our results would be to contrast them with more familiar forms of environmental impact. For example, would could say that, over the period of a year, the difference in environmental impact between beef and pork is equivalent to having an addition 50 diesel-fulled cars on the road.

When reporting results, we should not simply state there was a significant difference between groups. We should provide the reader with our estimate of the size of the effect (i.e., the mean difference), as well as the uncertainty of our estimate (i.e., the 95% confidence interval). Finally, we should interpret these values for the reader based on previous results, known standards, etc.

Informative article providing good understanding of underlying numbers. Thanks!

LikeLike