## PLOS Subject Keywords: Association Rules

In a previous post I detailed the process of compiling data on subject keywords used in articles published in PLOS journals. In this instalment I’ll be using those data to mine Association Rules with the arules package. Good references on the topic of Association Rules are

- Section 14.2 of The Elements of Statistical Learning (2009) by Hastie, Tibshirani and Friedman; and
- Introduction to arules by Hahsler, Grün, Hornik and Buchta.

## Terminology

Data suitable for mining Association Rules should consist of:

- a set of uniquely identified transactions, where
- each transaction should have one or more items, where
- items are binary attributes.

The derived rules take the form X → Y, where X and Y are disjoint itemsets, each consisting of one or more items. The itemsets X and Y are known as the antecedent (lhs or left hand side) and consequent (rhs or right hand side). The rules should be interpreted as “If X is present then so too is Y”.

An Association Rules analysis aims to identify pairs (or groups) of items that are commonly found together. A shopping analogy would be that bread is often purchased with milk, while peanuts are often purchased with beer. This kind of analysis is not only confined to this sort of “consumption” scenario: it can be applied in any situation where a discrete set of items is associated with individual transactions.

In the context of the data we previously gathered from PLOS, where every article is tagged with one or more subjects, each of the articles is a “transaction” and the subjects are then the “items”. We’ll be deriving rules for which subjects commonly occur together. Or, more specifically, we’ll be generating rules like “If an article is tagged with subject X then it is probably also tagged with subject Y”.

## Transaction Matrix

The arules package derives Association Rules from a Transaction Matrix. The form in which we have the subjects data is particularly convenient for building a sparse matrix (class `ngCMatrix`

from the Matrix package).

For the purposes of the arules package the `ngCMatrix`

needs to have items (in this case case subjects) as rows and transactions (in this case articles) as columns.

There are 185984 articles and 9357 subjects. Next we coerce this into a Transactions Matrix.

Here’s some summary information. We see that the vast majority of articles are associated with eight subjects.

## Generating Rules (Default Settings)

There are two major algorithms for generating Association Rules: Apriori and Eclat. We’ll be using the former here. We’ll try to derive some rules using `apriori()`

with default parameters.

Zero rules! Well, that’s a little disappointing. But not entirely surprising: the default minimum thresholds on support (0.1) and confidence (0.8) are rather conservative. (I’ll explain what support and confidence mean shortly.) We’ll relax them in order to at least generate a decent selection of rules.

## Generating Rules (Relaxed Settings)

Reducing the thresholds on support and confidence to 0.002 and 0.75 respectively results in 35 rules. Lower values for these thresholds are necessary because there is a relatively small degree of subject overlap between the articles in the collection. Not surprising since they are derived from a wide range of disciplines!

Below is some summary information on those rules. We see that the largest rule length (total number of items on lhs and rhs of rule) is only 3.

This seems like a good time to peruse the rules themselves. To see the details we need to use `inspect()`

.

Each of the rules consists of two itemsets, a lhs and a rhs, with the implication that if the lhs itemset is selected then so too is the rhs itemset.

## Rule Metrics

Naturally some rules are stronger than others. Their relative quality is measured by a set of metrics: support, confidence and lift.

### Support

The *support* for an itemset is the proportion of transactions which contain that itemset. The *support* for a rule is the proportion of transactions which contain both the antecedent and consequent itemsets.

The five rules below are those with the highest support. The rule {Cytotoxic T cells} → {T cells} has support of 0.0115386, which means that “Cytotoxic T cells” and “T cells” are present in 1.15% of transactions. Likewise, the rule {Gene regulation,Microarrays} → {Gene expression} has support of 0.0041885, indicating that “Gene regulation”, “Microarrays” and “Gene expression” appear in 0.4% of transactions.

Support does not directly indicate the strength of the rule, just how often the components of the rule are present in the data. However, having a decent level of support for a rule is important because it indicates what proportion of the data contributed to deriving that rule. Obviously if a rule is based on only a few transactions then it is not likely to be very robust.

### Confidence

The *confidence* assigned to a rule is the proportion of transactions which contain both the antecedent and consequent relative to those which contain the antecedent. Equivalently, this is the ratio of the support for the rule to the support for the antecedent. Alternatively *confidence* is the probability of the rhs being present in a transaction conditional on the lhs also being present.

The five rules below are those with the highest confidence. For example, we see that articles with subjects which include “Secondary lung tumors” will certainly also contain “Lung and intrathoracic tumors”. Similarly, articles which have both “Cytokines” and “Cytotoxic T cells” as subjects will very likely also have “T cells”.

### Lift

The *lift* of a rule indicates how much greater the support for the rule is relative to the support for the antecedent and consequent, treated as if they are independent. Equivalently, *lift* is the ratio of confidence in a rule to the expected confidence were the antecedent and consequent independent. A useful interpretation of the lift is the increase in probability for the consequent if the antecedent is present. Alternatively, it’s the ratio of the conditional probability of the consequent given the antecedent to the marginal probability of the consequent. Write out the expression and it’ll make more sense.

A lift of 1 indicates that the antecedent and consequent are independent. In the rules below we see that the presence of “Circadian oscillators” results in a massive increase in the likelihood of the presence of “Circadian rhythms”. Similarly, if both “Malaria” and “Parasitic diseases” are present then the probability of “Malarial parasites” being used increases by over one hundred fold.

## Rule Selection

Before we look at rule selection we’ll generate a more extensive assortment of rules by further lowering the thresholds for support and confidence.

You can use the `subset()`

function to focus on particular rules of interest. What other subjects are commonly associated with “HIV”?

So there is quite a selection of them. We can narrow that down by focusing on those which have a relatively high level of confidence.

Selection criteria are applied in `subset()`

using the operators `%in%`

(does item appear in itemset?), `%pin%`

(like `%in%`

but with partial matching) and `%ain%`

(match all items specified) to operate on lhs and rhs. Arithmetic comparisons are used on the rule metrics.

Here’s another example which indicates that articles with subject “Dementia” will also have either “Alzheimer disease” or “Cognitive impairment” as a subject roughly 50% of the time.

## Symmetry

It might have occurred to you that these rules should be symmetric: if X → Y then surely Y → X too? This is certainly the case. Consider the four rules below, which consist of two symmetric pairs.

The rules {Fungal genetics} → {Fungi} and {Fungi} → {Fungal genetics} form one symmetric pair. Note that the support and lift are the same for both rules, but that the first rule has a higher confidence than the second. This is due to different supports for the antecedent in each case. Whereas “Fungal genetics” is a subject for 509 articles, “Fungi” appears as a subject for 1187 articles. The corresponding values of support are 0.0027368 and 0.0063823 respectively. Since “Fungi” is more than twice as common, the confidence in the second rule is diminished significantly. This emphasises the fact that rules with the highest confidence are those for which the support for the antecedent is almost as high as the support for the rule itself.

## Conclusion

Although the example presented here has been confined to binary (presence/absence) data, Association Rules can also be applied effectively to categorical data, as illustrated in Example 2 of the paper by Hahsler et al. cited above.

Association Rules are a powerful unsupervised learning technique which can be fruitfully applied in data mining. The resulting rules are often instrumental in identifying actionable insights from the data.