Example 1: Chi-square goodness of fit test for the political candidates example

Ho: Probability of cand 1= 1/3, Probability of cand 2= 1/3, Probability of cand 3= 1/3

Ha: At least 1 of the probabilities is different from what we suspect/expect.

votes_3_candidates= c(61,53,36) #these are the observed counts

probvector= c(1/3, 1/3, 1/3)

Are all the expected counts greater than or equal to 5?

See next:

chisq.test(votes_3_candidates, p= probvector)$expected
## [1] 50 50 50

Yes, all the expected counts are >=5. Therefore, we can apply the test in its original form (without any corrections)

# correct= False because we showed that all the expected counts are >=5

chisq.test(votes_3_candidates, p= probvector, correct= FALSE)
## 
##  Chi-squared test for given probabilities
## 
## data:  votes_3_candidates
## X-squared = 6.52, df = 2, p-value = 0.03839

The PV is less than alpha (0.05); therefore, we reject Ho and support Ha. So, we can claim that at least one probability is different from what we supposed (expected). Therefore, the candidates are NOT equally preferred.

What candidate’s preference is farther away from the expectations?

When Ho is rejected and Ha supported, the analyst usually wants to know what categories are farther away from the expected probabilities. In other words, what categories are more different than what we expected. To do so, you can compute each cell contribution to the chi-square statistic. See next:

chisq.test(votes_3_candidates, p= probvector, correct= FALSE)$residuals
## [1]  1.5556349  0.4242641 -1.9798990

The largest contribution in absolute value comes from cell 3 (candidate 3) and is negative. We can state that candidate 3 is a lot less preferred by voters than we expected him/her to be.

Also, since cell 1 (candidate 1) has a large positive value, we can claim that he/she is much more preferred than we expected.

Example 2: Chi-square test for independence. Gender and Brand

matrix_gender_brand= matrix(c(95,41,50,114), nrow=2, byrow=TRUE, dimnames= list(c("Could","Could not"),c("Male","Female")))

matrix_gender_brand
##           Male Female
## Could       95     41
## Could not   50    114
matrix_gender_brand
##           Male Female
## Could       95     41
## Could not   50    114

Is the validity assumption met? (Are all expected counts >= 5?)

chisq.test(matrix_gender_brand)$expected
##               Male   Female
## Could     65.73333 70.26667
## Could not 79.26667 84.73333

ALL Eij (i.e., the expected counts) are above 5; thus, the assumption for the validity of the test is met. No need for correction (correct= FALSE)

Let’s run the chi-square test for independence:

chisq.test(matrix_gender_brand, correct = FALSE)
## 
##  Pearson's Chi-squared test
## 
## data:  matrix_gender_brand
## X-squared = 46.135, df = 1, p-value = 1.104e-11

p-value = 1.104e-11

p-value = 1.104 * 10^-11

The PV is less than alpha (0.05); therefore, we reject Ho and support Ha. So, we can claim that gender and brand awareness are dependent. In other words, the brand awareness depends on the person’s gender.

Because we rejected Ho (this next analysis DOES NOT make sense when we do NOT reject Ho), we can ask the following question: Which gender is related to higher and lower brand awareness?

chisq.test(matrix_gender_brand, correct = FALSE)$residuals
##                Male    Female
## Could      3.609777 -3.491392
## Could not -3.287214  3.179407

We can see that Male and Could have a positive strong contribution (same thing for Female and Could Not). Therefore, we can conclude that males have more brand awareness of the products being advertised than females.

Computing Cramer’s V

chi_square_value = unname(chisq.test(matrix_gender_brand, correct = FALSE)$statistic)

sqrt(chi_square_value/(sum(matrix_gender_brand)*min (ncol(matrix_gender_brand)-1,nrow(matrix_gender_brand)-1)))
## [1] 0.392151

The value of Cramer’s V indicates that Gender and Brand Awareness have a weak to moderate association.