0
$\begingroup$

I have records such as:

\begin{array}{} \hline \textrm{2024-07-27 21:52:39} \\ \textrm{2024-07-27 21:54:15} \\ \textrm{...} \\ \textrm{2024-07-28 21:58:44} \\ \textrm{2024-07-28 22:01:15} \\ \textrm{...} \\ \textrm{...} \\ \hline \end{array}

And I want to compare the frequency of these records on weekdays vs weekends.

Here is my approach:

  1. Set an interval - for now 15 minutes and count records in these intervals
  2. Make two groups a) weekdays, b) weekends
  3. Resulting data look like:

\begin{array}{c} \hline interval & count \\ \hline \textrm{00:00:00} & 3450 \\ \textrm{00:15:00} & 2144 \\ \textrm{...} \\ \textrm{23:45:00} & 4712 \\ \hline \end{array}

for both weekdays and weekends

Now my questions are:

  1. Should I "normalize" the data, eg. divide the count by 5 for weekdays and by 2 for weekends because right now the gathered data is over different number of days? Or maybe the total number of different days (I always have a whole day) so for example if it was from Tuesday to Sunday only, then I would divide it by 4 (total number of weekdays) and 2 (total number of weekend days)?
  2. I was recommended to use two-sample Kolmogorov-Smirnov Test to find out if the distributions are the same, so is it correct to use as follows?:
weekdays = [3450, 2144, ..., 4712]
weekends = [2147, 1544, ..., 3894]

_, p_value = scipy.stats.ks_2samp(weekends, weekdays)
if p_value < 0.05:
    print("The distributions on weekdays and weekends are not the same.")
else:
    print("The distributions are the same")
```
$\endgroup$
2
  • $\begingroup$ Due to the vagueness of your problem description--you state only you want to "compare" frequencies -- it's not apparent that you need a formal test or that you need normalization. Could you elaborate on your objectives? $\endgroup$
    – whuber
    Commented Aug 19 at 20:36
  • $\begingroup$ Variance of counts is not generally directly proportional to the mean so such "normalizing" changes the variance properties that tests of count proportions typically rely on. That doesn't mean you ignore the exposures though $\endgroup$
    – Glen_b
    Commented Aug 21 at 5:03

1 Answer 1

1
$\begingroup$

As is often the case, it will depend on what you are trying to test/prove.
The Kolmogorov-Smirnov test (KSt) will compare the 2 empirical CDF's; the KSt checks that the 2 distributions are identical in all ways (mean, variance, skewness, etc...), not just overall shape. So a difference in location alone will result in a significant KSt (see e.g. here on CV).
So the first thing I would do is to plot the 2 eCDF's, on the same graph; that will tell you a lot right away; same support? same mode? same shape?Chance are, you will know the answer w/o running any test.
The very next thing I would do is to look at the various descriptive stats (mean, varaince, skewness) for the 2 groups (weekdays, weekends); if they are different enough, you have your answer (e.g. t-test on the difference of means). Now, those descriptive stats will be meaningfull only if you have many weekdays and weekends (not just 5 weekdays and 2 weekends), so that e.g. a t-test will have some power. Otherwise, your data is just not representitave (1 single week...) so there is not much you can do with that.
It seems likely that the total number of records per weekend may be lower (aka mean will be lower), and maybe you do not care about that; you just want to see if the shape of the 2 distributions are the same. Then you should "normalize" the data by doing a location shift on one of them, so the 2 have the same average count per 15 minutes (alternatively, you can normalize so that they have the same median; if the shapes are similar, it will not matter which way you normalize). Then a KSt will tell you if they have the same "shape" (even though the average counts and/or medians are different).
But a KSt could be significant e.g. because on weekdays, the early hours are "heavier" but on weekends, the later hours are "heavier". Which may be what you wanted to find out? But do you really care that they be similar for each 15 minute increment? (i.e. ~ the same relative probability -relative to the total counts per day- in each 15 minute increment?). That seems oddly specific... All you say is that you "want to compare the frequency of these records on weekdays vs weekends". I would interpret this, at least initially, as "total counts per day". So we are back to comparing the counts per day, between weekdays and weekends. A simple t-test (Welch t-test, because the sample sizes will be very different) will do the job (again, assuming that you have many weeks' worth of data).

Note; technically, the KSt is for continuous distributions. But your (binned) data is discrete (counts). Methods have been developed to deal with discrete distributions (e.g. see wiki here). But not all software packages may handle this case properly. As an alternative, with your binned data, you could use a $\chi^2$ test. You would have a 2x96 contingency table, and you would not have to normalize your data (because it is already contingent on the row proportions).

$\endgroup$
2
  • $\begingroup$ I think you misrepresent the KS test, which is for continuous distributions, which these are not (at least once they have been binned). The meaning of "location shift" is obscure. Finally, the traditional and obvious choice, which will work well for comparing frequency distributions, is a chi-squared test. Regardless, the connection to a t-test--how would you even apply a t-test to compare two distributions?--is mysterious. Instead of speculating about what the OP is trying to ask, why not wait for them to explain? $\endgroup$
    – whuber
    Commented Aug 19 at 21:24
  • 1
    $\begingroup$ @whuber, you are indeed correct; as you wrote your comment, I was adding a last "Note" paragraph exactly to this point. And indeed I added the chi-square test. Wrt t-test, it is not to compare the distributions; it is just to compare the means; if they are different, it may be all the OP needs to declare the distributions different. (and if the means are different KSt will be significant -but chisquare may not be...). Take a look at the added paragraph (at very end). $\endgroup$
    – jginestet
    Commented Aug 19 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.