# r - How to determine the correlation between unordered categorical variables and individuals?

I have a matrix with several unordered categorical variables. Each row represents a type of individual. Each column represents the number of times each type of individual was found to be in that particular condition.

Type    coal    cobalt  concrete    copper  gold
A       12      0       0           19      5
B       5       0       0           11      0
C       4       2       0           14      1
D       1       3       15          0       1
E       0       20      2           1       9


My question is very simple: I want to know if there is a correlation between the type of the individual (A, B or C) with a particular condition (copper, gold, etc).

Which test should I use? If possible, I would like to get the answer by using R.

Thanks!

If you have data sets $X_1,\cdots,X_n$ and $Y_1,\cdots,Y_n$, then you can compute their correlation with the following formula:

$$Cor(X,Y) = \frac{\sum (X_i-\bar{X})(Y_i-\bar{Y})}{\sqrt{\sum (X_i-\bar{X})^2\sum(Y_i-\bar{Y})^2}}$$

(where $\bar{X}$ denotes the average value of the $X_i$'s). This is accomplished in $R$ with the following command:

cor(x,y)


That being said, it is unclear what two data sets you are trying to find the correlation for. Finding the correlation between a type (A,B,C) and a condition (copper, gold, etc.) would not make any sense. You could, however, find the correlation between two different types (A and B, for example), or between conditions (copper and gold).

Edit: I think you might want to do a test for independence between categorical variables...if this is the case then this is what you are looking for.

I think it does not make much sense to look for a correlation here.

Wild guess: This looks like the result of a cluster analysis to me.

Read these => as 'corresponds best with'

Cluster...

E => cobalt or gold
D => concrete
C => copper
B => copper
A => copper or coal


Difficult to tell without domain knowledge.

I'm also not sure what you would like to compute - your exemplary data only has one observation for each type, which makes the computation of a co-variation aka correlation impossible.

If you‘re interested in prediction, for example the probability that a new observation with values for coal, cobalt, concrete, copper and gold will be of a certain type, linear discriminant analysis (LDA) might be what you're looking for. LDA is implemented in the MASS package and nicely explained on r-bloggers: http://www.r-bloggers.com/computing-and-visualizing-lda-in-r/

Still, you'll need more observations per Type to use LDA.

I guess what you want to calculate is called mutual information. "It measures the average reduction in uncertainty about x that results from learning the value of y; or vice versa, the average amount of information that x conveys about y", y is the person type and x is the vector of condition counts.

And this is the R package description https://cran.r-project.org/web/packages/entropy/entropy.pdf