# Test whether two samples have different modes?

I have a dataset with timed observations of an event across the day, in two different places. I am particularly interested in whether the peak in observation is significantly different between the two places. To give more details, we suspect that location 2 has these events happening way later than in location 1.

Here's an hypothetical example of the data:

Location 1 Location 2
13:40 15:35
14:27 17:12
... ...

The way I thought of testing this hypothesis is to the smooth the observation data of two locations using kernel density estimation (KDE). After that I could test whether the two density curves have different modes. However, I can't seem to find any test for differences in modes. Does anybody known of a test for that?

I tough of testing whether the mean time is different between locations, but a increase in mean hour might not represent an increase in mode, especially if the distribution is not symmetric or if it has events concentrated in particular hours with long intervals in between. Does anybody know of an alternative test I can use to test this hypothesis?

• Did you visualize the distributions to see what they look like? For example, if they look normal and you fail to reject the hypothesis of normality, you could compare the means as the mean of a normal distribution is also its mode. Aug 5, 2021 at 9:24
• Yes. Unfortunately, they are not normal. I have multiple locations to do this comparison with, and in some cases they are "bimodal" (although with one mode being greater than the other). Aug 5, 2021 at 9:33

You need to consider the the shapes of the two sample distributions of modes. If the two samples have similar shapes, then you could use a nonparametric test, such as the Wilcoxon rank sum test. (Also it would be best to use hours and fractions of an hour for your data (rather than hours:minutes.)

Consider the fictitious modes in vectors x1 and x2 summarized below:

summary(x1); length(x1); sd(x1)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
1.650   5.210   8.885   8.038   9.902  15.640
 30         # sample size
 3.159502   # sample standard deviation
summary(x2); length(x2); sd(x2)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
3.490   8.405  10.060  10.132  12.560  16.910
 35
 3.061555

boxplot(x1, x2, horizontal=T, col="skyblue2",
pch=20, names=T) Boxplots show samples of roughly the same shape, for which it seems reasonable to compare medians as metric for location. Sample x1 tends to peak before 10 AM, while x1 tends to peak around 10AM.

Then a nonparametric two-sample Wilcoxon rank sum test can be used to compare locations. For my fictitious data the modes show significantly different locations during the day at the 2% level.

wilcox.test(x1, x2)

Wilcoxon rank sum test

data:  x1 and x2
W = 347, p-value = 0.01878
alternative hypothesis:
true location shift is not equal to 0


A comparison of plots of the empirical CDFs (ECDFs) of the two samples shows that x2 stochastically dominates x1 (i.e., tends to have larger values). The blue ECDF for x2 plots to the right of (thus below) the ECDF for x1.

plot(ecdf(x1), lwd=2,
main="ECDFs of 2 Samples of Modes")
lines(ecdf(x2), col="blue", lwd=2, lty="dotted") Perhaps your real data with times of modes will show similar results.

Note: Here is the R code used to sample my fictitious data.

set.seed(805)
x1 = round(rbeta(30, 4, 7)*24, 2)
x2 = round(rbeta(35, 5, 7)*24, 2)

• It's interesting that we can use medians as a surrogate to modes. However, I'm curious on what is "roughly the same shape". Perhaps you can give a counter-example? Is there any situation where you wouldn't reccomend using medians as a replacement to modes? Perhaps if the distribution is bimodal? Aug 8, 2021 at 9:05
• Example: One sample strongly skewed left, the other strongly skewed right--or roughly symmetrical. // Main question is why you want to use modes, which may be ill-defined for samples. Aug 8, 2021 at 16:04