Hello, and welcome to a video on calculating odds in risk ratios in Python. Today, we won't be loading in any data.
So let's start with loading our libraries and get right to it. You can see from scypy.stats, we're importing Fisher exact
and we're importing NumPy as NP, so we'll go ahead and load those in. First, let's calculate
the odds and risk ratio manually. Let's use some variables from an example problem. Let's say we have a contingency
table like this, where A equals 40, B equals 60, C equals 30, and D equals
70. You can see we're using a new notation to put the information into these variables this time.
Now first, let's say we want to calculate our odds ratio. We could do this by dividing
A by B over C divided by D. And if you need a visual representation of what that looks like,
I have one here. So A divided
by B over C divided by D. A, B, C, and
D are arranged in a two by two contingency table. And sometimes seeing this makes
it helpful to visualize what we're doing with the math.
We can also find our risk ratio by dividing by the risks, dividing two risks
together, basically. We'll go back over this in a moment below. But for right now, let's
run this code. And you can see we found our odds ratio, which is 1.5 repeating and
our risk ratio, which is 1.3 almost repeating. Now, this will
work the same way if we define each variable separately. We're 40, 60, 30, and 70 each get put
into their own initialized variable. We can find the
odds ratio in a different way this time with a times d divided by b times c.
So a times d divided by b times c will give you the same result
as what we did above. We can also find our risk ratio
by dividing by two risks. So we'll take a divided by
a plus b, because a plus b is the marginal total
of this 40 and 60. C plus d would be the marginal total down here. When it click calculate
here and you can see now, we've got our marginal totals out here,
170 and 130. So this would be 40
divided by 100. Is the risk ratio for successes for unemployed or for employed people
in this case. Let's say that this is a table for success and interviews if you're employed versus
unemployed.
Okay, and then we have risk of C, which is the same thing, which is this
divided by, or yes, divided by 30 plus 70, which is 30
divided by 100. And then we
can find the risk ratio of those two risks by dividing risk of A divided by risk of C. You can also
do this all in one step, which you can see is just combining these two into one line. And
when I run that, we can see they both turn out exactly the same result.
And it is the same result we got above as well. In Python, there's
also a function from the scipy.stats package that we perform this automatically, given an array. An array
in Python is like a list, except it can only store values at the same data type. So if we put numbers into it, they
have to be all numbers. Okay, now the same
is above where we had a, b, and we gave it some information.
This time, we're saying odds ratio, comma, p value, is equal to fischer
exact table. And our table is right here. The reason we're able
to do this is because we know fischer exact, returns two items from it.
So the odds ratio from fischer's test is 1.56. Yep, that's what we were expecting from above.
There is no quick way to do a risk ratio like this, so you will just have to do it by hand in Python, unfortunately.
But if you'd like to use this method to find your odds ratio, you are more than welcome to. All
right, happy coding.