Repository navigation
R Statistics I: T test
Use the built in data set iris. Type help(iris) in the R console to get information about the data
set.
We want to know if there is a difference in mean sepal length between the species setosa and versicolor.
-
Use the
head()function to look at the first rows of the data. -
Pick out the sepal lengths from setosa and versicolor and store them in two variables:
g1 <- iris$Sepal.Length[iris$Species=="setosa"] g2 <- iris$Sepal.Length[iris$Species=="versicolor"]
We can look at the distributions of the length in the two groups in the density plot below. We see that there seems to be a difference in both the mean and the variance.

You don’t need to do the density plot, but if you like to try the code is here:
plot(density(g1),type="l",col="blue",xlim=c(3,8),main="")
points(density(g2),type="l",col="red")
legend(7,1,c("setosa","versicolor"),col=c("blue","red"),lty=c(1,1))
-
If we don’t know how to do a density plot we can instead check a few things to make sure that a t-test is appropriate.
a. Do a histogram of each group using thehist()function, to check that the length seems to follow a normal distribution
b. Calculate the mean lengths of both groups using themean()function and compare them.
c. Calculate the variance using thevar()function and compare them -
Use the
t.test()function to do a two-sample, two-sided t-test where you test if there is a difference between means in the two groups. Based on your variance calculations, try to decide if you should assume that the variance is equal or not. You also need to decide if your data is paired or not. -
What are your conclusions?