# Performing a two sample t-test for equal means on subset of a dataset

If I have a dataset that, of among other variables, contains the gender and wealth for a sample of a population with a year variable that represents the year the data about the individual was collected that can take on values 1996, 2001 and 2014, with some, but not all, individuals appearing in one, two, or all three of these collection years, can I perform a two sample t-test for equal means on the mean wealth of men vs the mean wealth of women using just the the sample surveyed in the most recent year (2014)?

• Because your test is restricted to 2014, it does not matter that some of the same individuals appear several times in the entire dataset. However, in what you have shown, you have only about 8 males and 2 females (who have very different wealth from each other). I would hesitate to draw any profound conclusions from such sparse 2014 data. This is especially true because no information is given about how the data were obtained. Nov 18, 2020 at 18:52

Because the same people appear in your data more than once, you have what is called a repeated measures analysis. A t-test doesn't apply because the formula for the standard error of the mean (the denominator in the t-test) leads to a biased estimate when you have repeated measures. In most cases with repeated measures, the estimate for the standard error will be too small, and your t-test will be more likely to make you conclude that there is a difference when a difference doesn't exist.

You can still test whether there is a difference between men and women using a repeated measures ANOVA. Repeated measures lets you specify fixed effects (male vs female) and mixed effects (subject or person id).

Most of the basic statistical packages will let you do a repeated measures analysis. SAS has a REPEATED statement in PROC ANOVA, and there's a guide to implementing in R here: https://www.datanovia.com/en/lessons/repeated-measures-anova-in-r/

• But OP asks only about 2014. And, how would you handle the non-normal data? Nov 18, 2020 at 19:13
• Non-normal isn't the worst assumption to violate. If you really care about the mean, then the ANOVA is robust to violating the normal distribution assumption. A bigger concern is whether it's really the mean you want to test. Income data tends to be skewed, so the mean is not a good measure of central tendency. You might consider Moody's median test. I'm not a fan of Wilcoxon in this case because rejecting the null means that either the variance, or the shape, or the means are different, which is not the same as testing the mean. Nov 19, 2020 at 13:55
• Also, if you're limiting yourself to 2014 to get around repeated measures, that's really not necessary. Using the repeated techniques that are available is relatively straightforward. Nov 19, 2020 at 13:56

I see only the following data for 2014.

m = c(2.6, 3.5, 4.8, 2.4, 1.3, 2,8, 1.6, 1.1, 1.2)
f = c(17.3, 1.1)


The overall data for all years do not seem to be normal, and even the few observations for males in 2014 are sufficiently far from normal to fail a Shapiro-Wilk normality test.

shapiro.test(m)

Shapiro-Wilk normality test

data:  m
W = 0.79789, p-value = 0.01367


Thus a two-sample Welch t test does not seem appropriate. The two observations for women are the highest and tied for lowest among all 2014 observations. There are not enough data for a successful two-sample Wilcoxon rank sum test.

wilcox.test(m, f)

Wilcoxon rank sum test with continuity correction

data:  m and f
W = 9.5, p-value = 1
alternative hypothesis: true location shift is not equal to 0

Warning message:
In wilcox.test.default(m, f) :
cannot compute exact p-value with ties


So, although the repeated appearances of the same subjects in different years would not keep you from doing a two-sample test for 2014, such a test is futile because of sparseness of the data for 2014 and the lack of knowledge whether the data are a random sample from any population.