6
$\begingroup$

As a follow up to my previous question, I learned that the chi square test can be used to compare expected and observed frequency distributions. But ...

Suppose I have the following data table in CSV, which surveys people on their fruit preferences in three cities (total people surveyed is different in each city):

apples,oranges,grapes,bananas

Acme Acres,5,15,5,15

Redmond,2,3,16,0

Cupertino,9,13,8,4

(1) I can run a chi-square test on each city, to see if my observed frequencies are significantly different from expected (in this case, equally distributed). But is there a test that I can run on ALL THREE cities at once to see if they are different from each other, AND also if they are individually different from the expected equal distribution?

(2) If I can run a test that says there is a difference between cities, and/or if each one is different from the expected distribution, is there an equivalent of the Tukey test where I can see which specific fruits are the ones that are different?

$\endgroup$
1

3 Answers 3

5
$\begingroup$

With this many questions to ask of the data, you are approaching the border between a "test" and a "model" (not that such a sharp distinction exists except in the conceptualization of the problem).

A Poisson regression model, aka log-linear model, can answer these questions for you. The book The Analysis of Cross-Classified Categorical Data by Stephen Fienberg explains this family of models.

$\endgroup$
1
$\begingroup$

I was also trying to understand similar situation. I believe following method can be used for individual comparisons:

You can use proportion testing for each row of the data (against totals). The significance should be corrected using Bonferroni correction for multiple testing.

For example:

         Acme Acres  Redmond  Cupertino  Acme Acres%  Redmond%  Cupertino%  Pvalues  Significant  SigBonferroni
apples        5.000    2.000      9.000       12.500     9.524      26.471    0.166        False          False
oranges      15.000    3.000     13.000       37.500    14.286      38.235    0.127        False          False
grapes        5.000   16.000      8.000       12.500    76.190      23.529    0.000         True           True
bananas      15.000    0.000      4.000       37.500     0.000      11.765    0.001         True           True
Total        40.000   21.000     34.000      100.000   100.000     100.000  

Percentages are column percentages.
P values are obtained by proportion testing for each row.
Significant is True if P<0.05
SigBonferroni indicates significance after Bonferroni correction, 
   i.e. P<0.05/(Number of tests=4) i.e. P<0.012

Above indicates that preference for grapes and bananas is different in these 3 cities, while preferences for apples and oranges are similar in these 3 cities.

Or, transposing the data:

            apples  oranges  grapes  bananas  apples%  oranges%  grapes%  bananas%  Pvalues  Significant  SigBonferroni
Acme Acres   5.000   15.000   5.000   15.000   31.250    48.387   17.241    78.947    0.000         True           True
Redmond      2.000    3.000  16.000    0.000   12.500     9.677   55.172     0.000    0.000         True           True
Cupertino    9.000   13.000   8.000    4.000   56.250    41.935   27.586    21.053    0.108        False          False
Total       16.000   31.000  29.000   19.000  100.000   100.000  100.000   100.000   

Percentages are column percentages.
P values are obtained by proportion testing for each row.
Significant is True if P<0.05
SigBonferroni indicates significance after Bonferroni correction, 
   i.e. P<0.05/(Number of tests=3) i.e. P<0.017

Above indicates that preference for different fruits vary significantly in cities of Acme Acres; and same is true for Redmond. However, in Cupertino, all fruits are similarly preferred.

$\endgroup$
2
  • $\begingroup$ Could you share the code you used to generate these results? $\endgroup$
    – zephryl
    Commented Mar 10, 2022 at 0:06
  • $\begingroup$ It was from a mix of some haphazard code in Python and manual editing on spreadsheet. $\endgroup$
    – rnso
    Commented Mar 10, 2022 at 12:58
0
$\begingroup$

@rnso something like the following should work in R:

library(tidyverse)

dat = read.table(text="location,apples,oranges,grapes,bananas
Acme Acres,5,15,5,15
Redmond,2,3,16,0
Cupertino,9,13,8,4", sep=",", header=T) %>%
  mutate(total = apples+oranges+grapes+bananas) 

res = data.frame(t(dat[-1]))
colnames(res) = dat$location
res$p.value = NA
for(fruit in names(dat)[2:5]) { 
  test.result = prop.test(x=dat %>% pull(fruit), n=dat %>% pull("total"))
  print(test.result)
  res[fruit,"p.value"] = round(test.result$p.value,4)
}
res

        Acme Acres Redmond Cupertino p.value
apples           5       2         9  0.1660
oranges         15       3        13  0.1267
grapes           5      16         8  0.0000
bananas         15       0         4  0.0008
total           40      21        34      NA
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.