 Now in this tutorial I want to show you two alternatives to the chi-square test for categorical variables And those are the g-test so we're gonna have a g-test for goodness of fit And we're gonna have a g-test of independence So the goodness of fit test just like the chi-square goodness of fit test we're gonna have a single Categorical variable and we count how many of each of the sample space values occur and we compare that to what we expect And then in the independence test we're just going to take two categorical variables And we're going to compare them to each other and see if they are independent of each other So not a lot of difference. There's a difference of course in the equation that we use to calculate the g statistic So let's have a look at the g-test for goodness of fit and the g-test for independence So in this short video, let's have a look at these g-test for categorical variables So what are we going to have to do is just import a library desk tools if you haven't installed it Please install that package and then we import it with the library desk tools So the g-tests they are as I mentioned they are Well, we could replace the chi-square test of goodness of fit and the chi-square test of independence with these So let's just first have a look at the g-test of the goodness of fit And I just want to go down when we look at the g-test of independence just to show you What the equation looks like so we're going to have this g statistic. So again, we're going to calculate the statistic That's going to give us a probability density function It forms a curve and we can work out there under the curve to give us a p value So it's two times the sum of this each observed value times the natural log of This ratio of observed to expect it and we calculate that for each value We sum all of them up and we multiply by two. So let's look first have a look at the g-test of goodness of fit So as with the chi-square test of goodness of fit, we have a single variable Categorical or if we just use the discrete values the screen medical and we just interested in the counts not the actual values We can use this as well So imagine that we have observed a hundred and seventy five values So just a simple example for one of two unique sample space data point values for variable and a hundred and ninety four four for another Just we'll just Have this imagine a little experiment and we collect the data there the sample space for this Categorical variable has two values in it and we count a hundred and seventy five of one hundred and ninety of the other But what we expect let's imagine we expected a fifty-fifty split How can we use the g-test of goodness of fit to see if this? Distribution we found a hundred and seventy five versus ninety is that statistically significant from a fifty-fifty split So observed I'm just going to create a vector there of 175 and 90 and the desk tools has a nice little function G test as you can see X equals just your observed table p is what you expected a vector of 0.5 and 0.05 We're not interested in any corrections here and that's going to give us this log likelihood ratio goodness of fit test We see the g statistic there of 0.6 chi-squared Degrees of freedom of one and we see a p-value of 0.4 So that's not statistically significant from what we expected a fifty-fifty split Now with the test of independence That's the exact same as the chi-square test of independence except that we use the g statistic instead of a chi-square statistic And if you watch that video You can just have a quick look. We're using exactly the same scenario where we have the patients in group one and two And those that worse than state the same were improved So exactly the same the same just to show you we only have to create this little matrix for the r bind Function and then just to show you the g test is in the desk tools library Don't have to put that you can only put g tests the observed table there and no corrections And again, we get back our log likelihood ratio g test of independence without correction We see a g statistic there a chi-square degrees of freedom being two and a p-value of 0.01 So again a significant difference so not that different from the chi-square distribution So there's your alternative using the g distribution the g tests for categorical variables Remember that for these tutorials on r that the actual html rendered files are on our pubs And that's what you might see on the screen But these files are also available in their raw form on github and all the links will be in the description below So you can either go to the website and look at our pubs files as they're already rendered Or you can go to github and download those files into your system so that you can use them in our studio yourself So if you like these videos on r, please let me know so that I can make more of these or the subjects that you want me to Cover as far as bi statistics is concerned and the use of r. Please let me know Otherwise, please always remember to subscribe and hit the notification bell so that when new videos come out You will know about it. You can also follow me on twitter because that's where you'll also see that new videos are out