CodeThis is the fifth in a series of posts about Student's t test. The previous posts are here , here, here, and here.
Last time I showed a version of the one-sample t-test in Python. As we saw, the t-statistic is constructed similar to a standard error (like the sem). It is something like a z-score using the expected value for the population mean, and the observed (unbiased) sample standard deviation, then adjusted by multiplying this "z-score" by √n. (The same function, slightly edited, is also given below).
The paired t-test is simply a one-sample t-test done on the differences between paired values, comparing them to the expected difference rather than the expected mean. The paired values arise, for example, in repeated tests of the same individuals. The idea is that variance due to differences between individuals is the same for each value in a single pair.
The two-sample t-test takes account of the expected difference in means:
The code I copied (and massaged) takes the observed sample variance and turns it back into the sum of squares:
There's a super-duper factor which is constructed from the number of values in each sample (and the dependent degrees of freedom):
The t-statistic is as shown. I offer no justification for these procedures. (If you want more theory and don't like your book, start here: pdf). What we will do is test the output from these functions. Today we simply check that R gets the same t-statistic for one (or two) examples. Next time we will construct a test harness that does more. And after that we may do some more testing that shows these functions do give the correct distribution with simulated data.
I feel like I do understand the tests better after this project.
The functions for unbiased variance and standard deviation in
utils.pywere given last time. There's something a bit unusual about the variance calculation, which comes from PyCogent. I'll try to remember to deal with that when I talk about error checking, and "tails", in a future post. The functions here are just the basic t-statistic calculations themselves.
The source module for the t distribution:
was explained here.
Here is the output if the module is run as
R output using the code as given at the end of the listing (and as shown here):