I have a categorical variable column that looks like this

ID Item
1  Apple
1  Orange
2  Apple
2  Pear
2  Orange
3  Apple 

I converted it to a wide dataframe which looks like this

ID Apple Orange Pear
1    1     1     0
2    1     1     1
3    1     0     0

Would it be possible to calculate any form of correlation between Apple and Orange? Such as when someone buy apple, it is likely for them to buy pear? Is pear statistically dependent on apple?


Sure--there are lots of forms of correlation/dependency measures for binary and categorical measures, such as this. Without more information about the application, we can't really direct you to a specific one.

Also, note that binary variables $X$ and $Y$ are only independent if $P(X=1)P(Y=1) = P(X=1,Y=1)$ and this can be tested with, e.g., a chi-square.

  • $\begingroup$ Hi, But Im meauring within the X values Can item Apple be treated as X, and item orange be treated as Y? Also, am i supposed to calculate pairwise contigency table for all my items? I want to see if apple is related to orange when someone buys a basket of items $\endgroup$ – R_abcdefg Oct 10 at 3:47
  • $\begingroup$ @R_abcdefg you sound confused; or at least your concern is unclear. The above answer does deal with your question. Contingency tables can be done pairwise or with more variables; for example, it may be important to consider other variables in looking at the relationship between two variables. $\endgroup$ – Glen_b -Reinstate Monica Oct 10 at 6:36
  • $\begingroup$ @R_abcdefg yep--let X be the variable that's 1 if you buy an apple and 0 if not, and Y similar for oranges. The IDs are your experimental units/observations. And then it makes perfect sense to form a contingency table for 2 variables! $\endgroup$ – Sheridan Grant Oct 11 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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