Hello and this lecture I go to learn how to perform the chi square test for goodness of fit this term
is used when our variable is nominal with three or more categories and it compares an observed distribution
with a fairer theoretical distribution.
For example I will use the categorical variable education level in this data frame the more it has five
categories.
And first of all I'm going to generate accounts cable for the ball using the function table and I'm
going to name this table my table that.
So we have the for each educational level here.
And now I'm going to run the chi square test with equal theoretical probabilities.
First I'm going to compute the number of columns of the table with the functional land and store it
in a variable and the value of an is of course five.
We have five columns in the table because the variable has five levels and now I'm going to throw the
theoretical proportion in a new variable.
They won and they will be computed as one divided by the number of columns in the table.
So the theoretical proportion will be equal to zero point twenty of person once divided to five.
So we're going to have five theoretical probabilities for all of them equal to the 0.20.
Now that around that time we're tasked with Ponson chi where dots that we have to specify the table
my table in this case and then we have to factor by the proportion and the theoretical probabilities
is with Option B.
And here we're going to use the repeat function.
Why.
Because we want that the theoretical proportions the variable th Propp to be repeated and times five
times.
OK.
So let's clear the console for us and then run the chi where to test with the options with these arguments.
OK so the interpretation is very simple.
The chi square value is seventy one point fifty two with four degrees of freedom and the P-value is
very very low.
It is lower than 5 percent.
So we are going to reject the new hypothesis and say that there at the educational levels do not have
an equal distribution.
So the other probabilities are not equal with the theoretical probabilities in order to get the expected
value.
The residual value as a standardized residual values we are going to use the values expected residuals
and add to the residuals after the function where Test separated by a dollar sign.
As you can see here I'm going to play the console first.
Now let's get the expected value is the fact that counts.
Here they are that computes the residual value.
The difference between the observed data and fact is values.
Here they are.
And now that computer there is a standardized residuals here they are in that table.
Now let's round the Time Square class for the same variable education level with on the theoretical
probabilities that the that my theoretical probabilities are as follows.
30 percent 30 percent to resound 10 percent and 10 percent.
Of course there are some must be 100 percent.
So I'm going to answer these values in the the auction simply using the C function the concatenate function
that clears the console from this.
So the kind where value is one hundred eleven point two or seven with one with four degrees of freedom.
The p value is lower than 5 percent.
So again we reject the null hypothesis and say that we observe porcelains are not equal with the theoretical
or expected proportions.
Again if you want to get the expected counts with this probability the theoretical probabilities in
this case you use the value of x expected.
And here they are they expected columns in the table.
Now if you want some expected counts are lower than 5.
You can of the program to simulate the value how by using this option simulate the value equal to true.
So I'm going to run the kind where test again with equal probability.
So again using the repeat function here.
And with that option simulates the value equal to true.
You probably notice that the execution took a longer time now because of the simulation.
So it said that the kind where given probabilities was obtained was executed with simulated P-value
based on two thousand replicates.
Again the P-value of the kind where test is much lower than 5 percent.
So again the conclusion is the same the observer counts are not equal to the expected
that something you.