Learn fundamental concepts in data analysis and statistical inference, focusing on one and two independent samples.

Loading...

From the course by Johns Hopkins University

Mathematical Biostatistics Boot Camp 2

35 ratings

Johns Hopkins University

35 ratings

Learn fundamental concepts in data analysis and statistical inference, focusing on one and two independent samples.

From the lesson

Discrete Data Settings

In this module, we'll discuss testing in discrete data settings. This includes the famous Fisher's exact test, as well as the many forms of tests for contingency table data. You'll learn the famous observed minus expected squared over the expected formula, that is broadly applicable.

- Brian Caffo, PhDProfessor, Biostatistics

Bloomberg School of Public Health

Okay here's a, here's another example and we're going to, we're going to do.

Â so this is from Rise's book Mathematical Statistics and Data Analysis, which

Â is another first of all, I have affiliation, and never met Rise.

Â So it's easier for me to say this, but, I, I, love this book.

Â I think it's wonderful, this Mathematical Statistics and Data Analysis book.

Â So, if you are looking for a book recommendation, I like that one.

Â in addition, I really like to address this both, and

Â I'm willing to stipulate my conflict of interest in recommending it.

Â but I do really like it, I read it all the time.

Â so any rate, in this book he there's he had this interesting example.

Â Where he had a bunch of words words taken from some novels, that were well

Â one of them was purported, were two of them were known to be Jane Austin novels.

Â And one of them was, was in questions as to

Â whether or not it was from written by that author.

Â Let's say they found it later.

Â And

Â this maybe other ways you would want to analyse this data for this reason,

Â but you know we want to use it as an an example for the Chi-Squared.

Â So, don't think too hard about

Â specifically how you would analyze this data,

Â because I, I doubt this would be what you would arrive at immediately.

Â But, it, it's not unsensible by the way, it's, it's, reasonable.

Â so

Â so imagine, let's say, book three. I'm just spit balling here.

Â Imagine book three it's, it's book where you don't whether or not

Â it's from the same author, in this case it was Jane Austin.

Â and you want to test whether the word distribution of these words, is equivalent

Â across the three books and you sampled so many words from each of the books.

Â Okay?

Â So that's the setting and let's see if we can figure

Â out some, some expected cell counts to do a Chi-Square test.

Â Okay, so our null hypothesis is that the distribution

Â of these words is the same for every book, and

Â that at least two are different.

Â So in this case, we have kind of

Â a multanomial for every column that we're interested in.

Â We want to test equivalence of

Â those multinomial probabilities across the columns.

Â okay.

Â so, if the book was irrelevant, we would look at these

Â row margins and we'd say the word a should appear roughly 434 over 1017 times.

Â Kay? So our 1,1 cell was

Â 147 but in that book, there were

Â 375, you know, words of this type that were sampled and we're multiplying them

Â times the, what we would expect to see of that word, if book were relevant.

Â And that's 434 over 1,017, one thousand seventeen,

Â and that's how you get that expected cell count.

Â Follow through, take the observed

Â 12 cell.

Â now that's how many time a appeared in book two.

Â It co, it came up one 186 times.

Â How many will we expect out of 440 words?

Â from our estimate viewing book is irrelevant, that

Â would be 434 over 117 times 440 and you

Â would compare that then to 186, to see

Â how much the observed count deviates the expected count.

Â Now then you would move on to the next word, an.

Â Right?

Â So an was disregarding books seen 62 out

Â of 1017 words. and so we would take 375 and

Â multiply it times 62, times, divided by 1017, and

Â compare that to 25, to see if the number of an's in book one,

Â was different than what we'd expect and so on.

Â And you can go through all the calculations and the

Â sum of the observed minus expected squared over expected is 12.27.

Â Degrees of freedom are 6 minus 1 times 3 minus 1.

Â In this case, working out to be 10. now again

Â and, and, and now at this point, I would ask you,

Â as an exercise, to figure out what the chi squared probability is.

Â chi squared p-value is based on 10 degrees of freedom using r.

Â now, I should make some comments about sampling assumptions and these sorts

Â of things about how the reasonableness of modelling the words this way.

Â so, you know, but this, this goes without saying in all of these problems.

Â we're making assumptions on how the experiment was

Â conducted and that the model we're applying is relevant.

Â So in this case, you could probably say, yeah.

Â That's not so bad, about sampling assumptions,

Â if you under, if you were to make some, you know you

Â know, assume that there's, you know, there's lots of words in the book.

Â you know?

Â We're only sampling, you know, 300 or so of these very generic

Â words so that we could, you know, so that modelling that process is multinomial.

Â Yeah. That doesn't seem so bad to me, at least.

Â assuming that's how the experiment was conducted.

Â But you always want to think about that.

Â How does what I'm doing, so the Chi-Squared test, how does

Â its assumptions accurately reflect the way in which the experiment was conducted?

Â You know, anyone can of course just take a

Â table, click some buttons and get a p value.

Â But that p value is supposed to represent a pro-, probability.

Â That probability is supposed to you know quantify

Â the randomness in the experiment.

Â And that randomness is only you know, that, that is only

Â being modelled, through,or that is only

Â being quantified through this statistical model.

Â And so our results are only meaningful insofar

Â as that model is a meaningful reflection of reality.

Â Okay. So here is kind of a funny one.

Â I also got this from Agresti's book.

Â Both these books have lots of wonderful discussions of contingency table stuff.

Â So in this case, they we're rating couples.

Â A husband and wife's ratings of sexual fun.

Â So, N was never, F was fairly often,

Â V was very often, A was almost always.

Â and so let's say they sampled 91 couples and then they cross-classified them.

Â And you know let's let's talk about

Â in this case, you know, kind of the logical way, kin, the, the logical, a

Â logical first question to ask would be, you

Â know, are the ratings independent of one another?

Â Is the wife's rating independent of the husband's rating and so on.

Â And and see you, you can make all

Â the relevant jokes about the experiment at home right now while we move on.

Â Okay, so now in this point, we're going to

Â talk about that the ratings between the row

Â variable, which was husband's ratings and the column

Â variable, which was wife's ratings, that they're independent.

Â Versus the alternative that they're not independent.

Â So let's talk about what happens under independence.

Â The probability that a husband rated n and the wife

Â rated a would, would factor in the probability that the

Â husband rated n times, the probability that the wife rated a.

Â Okay?

Â So, again, we don't have these probabilities,

Â we're going to have to estimate them.

Â But for the Chi-Squared test, we're going to estimate them under the null

Â hypothesis and compare them to what we observe in the cell counts.

Â So let, let me just do one of them.

Â so the first

Â col row probability, is 19 over

Â 91 because if you disregard wife's ratings, the husband rated n 19

Â total times out of 91. and the wife rated

Â n 12 times out of 91, if you disregard the husband's ratings.

Â and so our expected counts under independence, would the

Â probability of that specific cell, 19 over 91 times 12 over 91.

Â Which we can multiply these, because we made

Â the assumption of independence under the null hypothesis.

Â Now we multiply that times the potential, the number 91, the total

Â number of couples, to get 2.51, we're going to compare that to 7.

Â When you do that throughout and you get the

Â expected counts are this formula right here.

Â If you, if you like, the, the, the row total times the column total,

Â then divide it by n instead of n squared, because you multiply by n.

Â so that e-ij is the cell you copy for every cell, but the logic is clear

Â as to where that formula comes from. And the degrees of freedom, are again rows

Â minus 1 times columns minus 1.

Â And I'm going to to let you again now, I'm going to do even less, and

Â I'm going to let you calculate the statistic

Â and compare it to the Chi-Squared distribution.

Â I would note, this is what, I find this fascinating, so I keep repeating it.

Â But it is interesting that in all of the cases, all of the cases that we

Â covered, you could just execute the formula by, or execute the Chi-Squared

Â test by using this formula, the eij equals ni plus times n plus j over n.

Â You wind up in every case with the

Â identical Chi-Squared statistic and I suggest you try it.

Â Go back to some of these other

Â examples and calculate the Chi-Squared statistic that way.

Â so any rate and that's why I often, in textbooks,

Â the Chi-Squared statistic is only presented as this formula right here.

Â but again I, you know, even though the statistics

Â stays the same, the interpretation of the results depends

Â on the design of the experiments and which margins

Â they are fixed by the design, and so on.

Â So, I think that's an important caveat.

Â But none the less, in terms of actually doing

Â this stuff, if you have to program it, for example.

Â you can always just use this simple formula, not have

Â to really spend too much time not thinking about, oh yeah,

Â what's fixed and what's not while you're doing the calculation's.

Â Of course you want to think about

Â that while you're doing the interpretation, but while

Â you're doing the calculations, you just use this

Â formula right here, which is extremely convenient then.

Â Oh and apparently I lied when I said, I'm going to let you do

Â this on your own, because here on the next slide I do it.

Â so, I define a 2 a 4 by 4 matrix here.

Â the x matrix you just use Chi-Squared, chisq.test(x), that will give it to you.

Â Again, remember doing the continuity correction.

Â So if you do it by hand you probably won't get exactly the same numbers.

Â if you do this formula,

Â observe minus expected squared over expected, you get around 17.

Â Degrees of freedom are 3 squared, and the p value works out to be just under 5 %.

Â so couple caveats.

Â I should, I should say a couple caveats.

Â One is, the Chi-Squared approximation is an

Â asymptotic approximation, using the central limit theorem.

Â Now, it may not be clear how the central

Â limit theorem is kicking in here, but it is.

Â And so, you have to worry about whether or

Â not the central limit theorem is a good approximation.

Â But fortunately for you, in a slide or two, we're going to

Â talk about how you can do exact Monte Carlo finite sample approximations.

Â So you, you should get excited about that now.

Â so, so,

Â often in textbooks you'll see them talk about whether the

Â cell counts are large enough to use, a large sample approximation.

Â My recommendation is just to always use the small sample one.

Â In chisq.test, you can do an exact equals true

Â argument and just get the exact small sample test.

Â which, you know, then gets rid of the need to do that.

Â And for the test of independence, you can do it without

Â without actually

Â too much computing.

Â You have to have a pretty big table, to not be able to do it.

Â so for a more elaborate Chi-Square test, you can't do the exact ones.

Â I have an r-package called Exact LogLin

Â Test, which does kind of crazier distributions.

Â but you know, it's not maybe the most trivial r-package to use.

Â and then there's the software called StatExact, where they do

Â quite a few exact versions of contingency table tests.

Â So any way, but all the Chi-Squared tests, in terms of

Â comparing them to the Chi-Squared distribution, that is an asymptotic test.

Â It relies on the central limit theorem.

Â You are you know, you can, you can use these

Â rules that say, the cell counts have to be so large.

Â But in reality, as with all Chi-squared test, with

Â all asymptotic approximations, you're putting your faith in the

Â idea that the, the the cell count, the,

Â the asymptotic's have kicked in on your behalf.

Â but the, you know, checking that the cell counts are large is

Â a, is a way to give yourself some hope that, that's true.

Â I had one other point I wanted to, to make.

Â so let's see, I think I made the point that the that the test, all the tests if

Â we've used this last Chi-Squared independence formula, we would've

Â gotten the same Chi-Squared statistic for all the tests.

Â In every case the degrees of freedom, is

Â always rows minus 1, times columns minus 1.

Â oh, last thing, yeah, now I remember what I wanted to say.

Â So where

Â in the world does this observed minus expected

Â squared over expected, where does that statistic come from?

Â It actually comes from the Poisson distribution.

Â and the, it turns out that the, you know, the expected

Â value, the expected, are, are, the sort of expected cell counts.

Â And it turns out in the Poisson, the the expected

Â value is the variance, as well of a Poisson random variable.

Â So you can kind of think of this as each o is a Poisson

Â count minus it's mean and then divided by

Â the standard deviation and if you square all that.

Â Right?

Â Then you wind up with this statistic, o minus e squared over e.

Â so you might think of this guy, the, the, the o minus e squared over

Â e, one of the elements of the sum, as being like a little z statistic.

Â And then, you're adding up a bunch of z statistics, squared.

Â which e, a z statistic squared is a Chi-Squared

Â 1, so when you're adding them up, you get a Chi-Squared.

Â now because we're estimating components of the expected

Â counts, then we lose degrees of freedom that way.

Â And if you want to do a careful accounting of how the

Â asymptotic work, works, then you have to a, account for it.

Â But that's where this formula comes from. It comes from the Poisson distribution.

Â and it really, you think of it as a bunch of squared

Â z statistics.

Â and of course, the asymptotic's are a little bit more delicate

Â than that, but, but honestly not that much more delicate than that.

Â So that's where that formula comes from.

Â Okay, so just to rehash these equal distribution

Â tests, like we did for the word counts.

Â That yields the same thing as the

Â independence tests, they're, they're all the same tests.

Â And, you know, under the same, under, under kind of the,

Â the, the sort of similar modeling

Â assumptions I'm sorry, similar testing assumptions.

Â You know, if, if your model's kind of binomial or multinomial where the row

Â total's fixed, binomial or multinomial where the column total's fixed.

Â The total sample size is fixed, and

Â you assume a multinomial and test independence.

Â None are fixed and you do a Poisson model

Â and you test some form of row versus column additivity.

Â All these wind up in exactly the same Chi-Squared

Â statistic the same p-value, the same rejector not resolved.

Â so and if this bothers you, basically the best

Â thing I, you know, I tried, I think throughout

Â the lecture to, to describe this on numerous occasions.

Â But if it's still bothering you and gnawing at you

Â what I would say is, this is really common in statistics.

Â Mathematically equivalent results, are applied in different settings.

Â They have different interpretations, but the

Â actual statistic, the mathematical results are equivalent.

Â So that's how I like to, to rationalize it to

Â myself, that all of these things are coincidentally the same.

Â So, some final comments on the asymptotic test.

Â The Chi-Square result require is an asymptotic test, so

Â it requires that, the something be going to infinity.

Â you know, in the multinomial, the multinomial case.

Â It's the, overall sample size has to go to infinity.

Â If you have, you know, multinomial columns, then I all of the

Â samp, all of the column totals have to be going to infinity.

Â And there's various strategies

Â for checking whether or not this, you, you,

Â you're close enough for the asymptotic's to be reasonable.

Â but we'll talk about an exact test, that will

Â get rid of the need to think about that.

Â The degrees of freedom are always rows minus 1, columns minus 1.

Â And this, what we'll talk about now is

Â the generalizations of Fisher's exact test can be used.

Â or, you know, this other thing, the continuity corrections

Â can be used that will make the asymptotic approximations

Â accurate, even for relatively small sample size type problems.

Â So let me let me show you how you can actually

Â use Monte Carlo to calculate an exact P-value for contingency tables.

Â Imagine if we got the, the individual data points.

Â Not just the contingency table, but the individual data points.

Â So for the first couple, it was nn. Where is the second couple it was nn.

Â For the third couple it was nn, for the fourth couple it was nn and so on.

Â And then oh, here's a couple that was fn and so on.

Â And here I've clearly sorted them in some way.

Â But this is the raw data.

Â If you were to, if you were to then take this data and create the counts of the

Â number of nn's, fn's, vn's, an's and so on, ff's, af's and so on.

Â You, you would get exactly the contingency table in the

Â from the couple of slides ago. And here's the interesting fact.

Â So this is the, the husband's data and the wife's data.

Â So, think about it.

Â If, if they were independent then the matching the pairs of husband wife's would

Â be irrelevant whether or not, you know,

Â you were talking about this specific couple.

Â Or you just had a husband and a wife their answers

Â should, it should matter whether you line them up

Â together with their correct husband and wife, or not.

Â So, so what you can do, is either take the, the, the wife,

Â row here I have, or the husband row here.

Â Either way or both but that would be unnecessary and then just permute it.

Â And then what you get is a realization

Â of the contingency table, where we've operated under the

Â assumption that, you know, which particular member of

Â the couple, the pairing of the couple is irrelevant.

Â In other words, that husband and

Â wives are, are independent.

Â But notice if we were to permute that, that data

Â and, and reconstruct the contingency table.

Â We, we would still have the same

Â number of husbands answering n and husbands answering

Â f, wives answering n, wives answering f, wives

Â answering b, wives answering a and so on.

Â So in other words, this procedure would constrain the margins, but would permute

Â the interior of the table, which is exactly what Fisher's exact test did.

Â Right?

Â And this is exactly the same thing as the Monte Carlo

Â version of Fisher's exact test.

Â It's just a general, you know, with more possible outcome

Â values for the, for the row and the column variables.

Â So you do this and you re-calculate the contingency table.

Â You calculate the Chi-Squared statistic for each permutation and the percentage of

Â times it's larger than the observed value, is a so called exact P-value.

Â And in R it's pretty easy to do this, because you do chisq.test

Â (x), simulate.p.value = TRUE). And then it does this exact Monte Carlo

Â version of the test, which is really, which is really really neat.

Â So this is just a generalization of Fisher's exact test.

Â and yeah, it's a very nifty little result and it has a lot of intuition to it.

Â Right?

Â It, it makes sense to think that, well, under the null hypothesis

Â of independence, we should be able to permute the couple.

Â They, you know, permute which spec-, specific pair

Â it was and, and recalculate the contingency table.

Â And we should get roughly the same you

Â know, discrepancy between the observed and the expected counts.

Â notice, you know?

Â I said use the Chi-Squared statistic.

Â So we're using the Chi-Squared statistic, but we're

Â not, we have an exact small sample P-value.

Â This P-value is,

Â is valid regardless of the size of the data.

Â So of course then it tends to be a little bit conservative.

Â but it's, so it's it's using the

Â Chi-Squared statistic but it's not using the

Â asymptotic, chi the central limit theorem to

Â compare your statistic to the Chi-Squared distribution.

Â I would say there's other choices for the test statistic and

Â because the way in which we're calculating our, our null distribution

Â is through this permutation process.

Â We could use whatever statistic we want right here.

Â And so, you know, the Chi-Squared distribution, the

Â Chi-Squared statistic is not necessarily a bad choice.

Â So any way, this is an interesting way to get an exact P-value for contingency table

Â test, where you're interested in looking at things

Â like independence between the rows and the columns.

Â Coursera provides universal access to the worldâ€™s best education,
partnering with top universities and organizations to offer courses online.