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

34 ratings

Johns Hopkins University

34 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

Hi, my name is Brian Caffo, and this is

Â mathematical biostatistics bootcamp two, lecture eight on Chi-Squared tests.

Â Okay, so specifically when we talk about chi-squared testing,

Â there's a lot of different variations of chi-squared testing.

Â Here we're going to be talking

Â about chi-squared testing for contingency tables.

Â and the most classic contingency table test is testing independence.

Â we'll relate that to testing independence of several proportions.

Â We'll go through the natural

Â generalizations to higher order contingency tables.

Â and then we'll talk about Monte Carlo variations

Â to get exact tested independence.

Â we'll finish it off with a discussion of

Â goodness of fit testing, which is a special kind

Â of contingency table test that's useful for testing

Â whether or not data arrive from a particular distribution.

Â Okay, so we've talked about testing in a 2 by 2 table, equality of two proportions.

Â say binomial proportions.

Â And an alternative approach to doing this the so-called Chi-squared testing.

Â And the form for the Chi-squared test is the same.

Â It's the summation of the observed data, the

Â observed cell counts minus the expected cell counts.

Â And I'll talk about what that means later.

Â square divided by the expected.

Â And we will talk about where the motivation for this formula comes as well.

Â So the, here the observed and the re observed counts.

Â The expected or the expected counts under the non-hypothesis, and this sum

Â is over all of the cells of contiguity tables, successes and failures.

Â And in the 2 by 2 table cast for

Â testing a quality proportions, this ones are being I squared.

Â this distribution.

Â I know the null hypothesis has a Chi-squared distribution

Â with one degree of freedom.

Â and it turns out the, the, and what,

Â maybe I'll, I'll assign this somehow as a homework.

Â the Chi-squared test is exactly the square

Â of the difference in proportion score statistic,

Â where you have the The common proportion in the denominator for the standard error.

Â and notice because the, so, so notice,

Â basically the statistic is a, is a distance.

Â It's the

Â distance between the observed cell counts and what would

Â you would expect to, to get under the model.

Â but notice it's squared so there's no directionality.

Â It's just whether it's, things are kind of different than what you would expect

Â to see under the model, so, under the model, under the null hypothesis.

Â so it doesn't test directionality, it doesn't test whether, it,

Â it tests it, it's effectively to not equal to hypothesis.

Â This is always.

Â So, let's go back to our familiar example where we were, we had a treatment.

Â here the treatment is x and y.

Â Let's say x is the treatment and y is the placebo.

Â And we want er, probably more accurately.

Â Let's suppose we're comparing two equally effective treatments,

Â and we want to see if one adds more.

Â side effects in the other.

Â So x and y are two different treatments, and we want to determine whether or

Â not one has more side effects.

Â Here, we were, we assigned 100 people to x and 120

Â people to y, and we're going to treat the binomial, the counts

Â of the number of people with side effects out of the

Â total as if they were binomial, that will be our model.

Â Okay, and here are the rates of side effects in two proportions.

Â p1 for x and p2 for y.

Â And we're interested in whether p1 equals p2.

Â Okay, let's see if we can logic our way through implementing this formula.

Â So the x-squared statistic I've said is the summation of the

Â observed counts, minus the expected, over the expect-, squared over the expected.

Â Well part of it's easy, right?

Â The observed counts o-11, o-21, o-12, and o-22, if we label the cells

Â of the rows and columns of the two by two table that way, then we have those.

Â 44, 77, 56, and 43.

Â Now what about the expected counts? Well, let's kind of think this through.

Â Okay.

Â If the rate of side effects was the same, for the two groups, for the two

Â for the treatments, then our estimate of the common proportion,

Â just like in the denominator of the score statistic, would have to be 121 over 220.

Â Right?

Â And now, we don't know that, so let's, let's use that

Â as our best estimate as for the overall proportion of side effects.

Â Regardless of treatment because under the

Â null hypothesis, the proportions are, or the

Â true proportions are assumed equal. So that's our estimate of that.

Â And then how many people we expect to see with the side effects.

Â well if that's the estimate of this common proportion than

Â 100 times that would be, we would expect to see 55.

Â and so on.

Â And you go through it we would expect to see, out of the

Â 120 receiving the second treatment, we would expect to see 120 times this

Â proportion or 66. I'm rounding here.

Â The expected counts by the way don't have to be integers.

Â They shouldn't be integers. You should carry out the calculations

Â to, y'know, many decimal places.

Â I just, I don't know why I round them, I think for

Â didactic reasons, I rounded them up for images here, for integers here.

Â But in general, the expected counts don't have to be

Â integers in the same way the observed counts must be integers.

Â so any rate, just a reminder, you want to carry

Â these calculations out not, not just round them at 55.

Â Okay and so on, and then, and then

Â 1 minus this probability is then 99 over 220.

Â and that's how many, non-side effects we would, at

Â times 145, is how many non-side effects we would expect

Â in the x treatment and 99 times 120 or

Â 54 is what we would expect in the Y treatment.

Â So these counts here on the right hand side.

Â Represent what we would expect to see under the null hypothesis using

Â our best estimate for the common proportion.

Â So the margins the 100 and 120 are fixed

Â by the design, and the best estimate of our common

Â proportion is, is that one, so this is kind of

Â our best guess is what we would expect to see.

Â And if these counts are very different from the observed count, then

Â that would shed some light maybe that the new hypothesis is not true.

Â Okay, so then our test statistic is 44 minus 55 squared over 55 and so on.

Â add all those up. And it turns out to be 8.96.

Â compare that to a chi-squared with one degree of freedom.

Â And again, we're rejecting for large values, right?

Â Because this is the distance between the observed and the expected

Â counts, we favor the alternity of the further away from the expected

Â counts we are, so the bigger the test statistic is, is going

Â to favor the alternative, so we're going to reject for large values.

Â So lets do pchi

Â squared 8.96.

Â It's one degree of freedom when you have a 2 by 2 table.

Â I am going to give you a general role when you don't have a 2 by 2 table.

Â And we say lower tail goes false, because we want

Â the upper probability, not the lower probability, the result is 0.002.

Â In the other way you can think about this of'course is chi

Â square with one degree freedom, is actually the square of \a standard normal.

Â So it's unlikely for a standard normal to be above two or

Â below minus-two, right?

Â There's only a 5% chance of that happening.

Â So Chi-squared, it's unlikely going to be above four, right?

Â The square of two and the square of minus-two.

Â So that's going to have about 5% probability, so chi-squared over about

Â four is about the same benchmark as a normal of about two.

Â A chi-squared of about nine is

Â about the same as a standard normal for about three.

Â Again, remembering that you're testing both bigger than two and less than

Â two, or bigger than three and less than three, because remember the chi-squared.

Â Always does a two sided test.

Â So in this case, the result is 0.002, there is some evidence

Â to suggest that there is a difference in the rate of side

Â effects between the two treatments, though of course the side effect, the

Â result of the chi squared test doesn't tell you which direction it goes.

Â [INAUDIBLE]

Â Okay. So that's how we do it.

Â And here's some simple R code for executing it in R E, so you don't have to

Â do the calculations with a calculator. so we just create a data matrix.

Â It's this matrix command here, and then chisq.test(dat).

Â And then, you'll notice, if you do this, you

Â don't get exactly the same test statistic that we got.

Â And the reason is because the chi-squared approximation

Â you know, it's the, it's an asymptotic approximation.

Â But the counts are discrete, and you can improve the chi-squared approximation by

Â fudging a little bit, you know, in, in the way that, that, you know,

Â In a way that kind of, if you're doing kind of numerical integration with

Â boxes, you can maybe do a little better with trapizoids, or something like that.

Â It's along that line of thinking.

Â And that boils down to adding a little bit to every cell.

Â And that, that's called a continuity correction, basically accounting for

Â the fact that the cell counts are discon, are, are counts,

Â and it can improve on the asymptotic approximation.

Â So if you actually put in correct equals false, it won't do that continuity

Â and correction, you'll see then that you'll

Â get the exact same answer that we did.

Â You do want to do the, so for didactic purposes, we're

Â not presenting the continuity correction, but when you actually do the test,

Â you want to leave correct equals true on there, because it

Â does yeild a better approximation, that's why it's the default in r.

Â Okay, so let's recap.

Â We're going to reject if the chi-squared statistic is too large or is large.

Â the alternative is always two sided.

Â You know, you're always comparing whether the proportions are different.

Â You do not divide your alpha by two, even though it

Â is a two sided test, remember, we're dividing, the reason we're dividing

Â the alpha by two In the standard Gaussian cases, because you're checking

Â bigger than, you're checking less than, because we've squared the statistic and

Â the chi-squared is only a positive statistic, we don't need to do that.

Â alpha divided by 2 for the quantile.

Â a small chi-squared statistic implies

Â little difference between the observed values

Â and those expected under h nought, so it supports h nought.

Â you can think of the chi-squared statistic as actually distance

Â and then what we'll talk about, and it's really kind

Â of a fun subject I think, the chi-squared statistic and

Â approach generalizes to other kinds of tests and larger contingency tables.

Â It's also one of these phenomenons that often occurs in statistics where

Â the same procedure arises out of several different settings and data structure.

Â And so the interpretation changes but

Â the actual procedure stays the same and we'll

Â go through another one next where we have

Â we think about this problem in a different

Â way we wind up with an identical procedure.

Â And that happens a lot in statistics where even though you think

Â about the problem in a different way, you get the same procedure.

Â The mean is general, is frequently a good estimator you know even if the data

Â is IID exponentional, or IID normal, it's

Â going to estimate the mean into the data well.

Â And so the mean,

Â is, you know has, it pops above over the place.

Â Well you know the same sort of

Â thing happens in Chi-squared testing where you get

Â kind of the equivalencies despite very, very different

Â sampling strategies and, and assumptions underlying the data.

Â And there's a neat com-, computational form.

Â In a two-by-two case, it looks like this, where

Â I'll go, I'll have the notation on the next slide.

Â But where here the n

Â i,j's are the cell counts.

Â And you put a little plus in front of the index, if you're summing over that cell.

Â So these are the margins.

Â And here briefly is the notation that I'm

Â going to be using, where i-j indexes, cell count.

Â I'll call n plus one meaning something over the first index.

Â So that's this margin. N plus two is this margin.

Â And one plus is this margin. And two plus.

Â Its this margin.

Â And if I need to refer to n1 and n2, let's say those are

Â the row margins, and then n'll be the sum, this, this, corner cell right here.

Â The sum of, the total number of observations.

Â So, you know, kind of an interesting fact about the

Â chi-squared statistic if we look at it We transpose the table

Â the statistics doesn't, doesn't actually change its value, which

Â is kind of interesting, right. It, it means it doesn't care about which

Â margin is sort of fixed by the samp, the study design.

Â It, it, it's, it doesn't care about that. and it, and it, and if you errantly was

Â thinking of, of side effects as being the, outcome, you get the same thing as

Â if you were thinking about which treatment they received was the outcome.

Â You are going to put the same test and you know only

Â one of them, they are correct, way in which the experiment was conducted.

Â So that's interesting, and it ties into, its utility then ties into a lot of these

Â instances where, you, you really want to play around with the interpretation

Â of the, of the, what variable is the outcome and what variable is

Â the predictor, and we'll talk about that when we talk about case control studies.

Â It's interesting that in case control studies, some of the fundamental

Â work was done right here at Hopkins by, by person in cornfield.

Â Any rate,

Â the so the, so the Chi Squared statistic, it can rise.

Â You can state a model for which the Chi

Â Squared statistic is kind of the obvious thing to do.

Â if the rows are fixed, so you have binomial, you can do it.

Â If the columns are fixed, which is just

Â maybe a different kind of binomial if you're.

Â You know, again here, we're assuming.

Â Binomial Ness of the data, neither of these cases.

Â But let's imagine if you didn't assume that the, data were in binomial

Â that the rows or columns are fixed.

Â Let's say that you assumed that only the total sample size is fixed.

Â So imagine as an example of that You collected, instead of randomly assigning

Â 100 people to receive the treatment, and randomly assigning 120 of the

Â other people to not receive, to receive the other treatment, imagine if you

Â just happened to go out and collect a bunch of people, and ask

Â them what treatment they had, and ask them whether or not they had side effects.

Â Now, granted, it's a different experiment, they way in which you

Â would interpret the result of a chi squared test would be different.

Â But but there you, you would, you would

Â might think oh, I sampled 220 people, you know?

Â That was really my, the part that was fixed by the design.

Â And you

Â know, however people fell out in terms of s-, side effects and

Â In treatment, well, you know, that's part of the randomness, and, and so I'm

Â going to model that whole thing as if

Â they were multinomial with four possible things.

Â They could be taking treatment x with no side effects, treatment x with

Â side effects, treatment y with no side effects, treatment y with side effects.

Â I think I hit all four, but you see what I mean.

Â There's four possible elements in a two by two table.

Â So in that case, it wasn't fixed.

Â And I'll go through it right now.

Â But you wind up with the same exact test statistic,

Â you wind up with the same exact test statistic if you assume multinomial-ness.

Â And the specific null hypothesis, namely, a test of independence.

Â So that's, I find that cool.

Â And that's why often, people don't, people are a little bit loosey-goosey about

Â their assumptions in multinomial settings, because

Â they kind of apply in different ways.

Â And I do think there's a lot of problem with that,

Â because Yes, the number of the, that comes out from the test statistic is the same.

Â But, it doesn't the interpretation's very different,

Â different, different, experiments lead to very different interpretations.

Â Very different interpretations of the assumption.

Â in, in my just example I just gave you, in one case we randomized a treatment,

Â or in my fictitious case that I'm making

Â up, in one case we randomized the treatment.

Â And in the other case we just went out and sampled people observationally.

Â Those are very different interpretations.

Â I think I would, you know, most people would agree that the

Â randomization would kind of average over confounding effects and other things that

Â on, on observed variables where in the observational

Â case, that, that wouldn't necessarily be the case.

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