---
title: "Transaction Data and Association Rules"
output:
  pdf_document: default
  html_notebook: default
---

# Example: Identifying Frequently-Purchased Groceries

```{r}
library(arules)
library(arulesViz)
library(DT)
```


## Step 2: Exploring and preparing the data

Load the grocery data into a sparse matrix.

```{r}
groceries <- read.transactions("groceries.csv", sep = ",")
summary(groceries)
```

Look at the first five transactions.

```{r}
inspect(groceries[1:5])
```

Examine the frequency of items.

```{r}
itemFrequency(groceries[, 1:3])
```

plot the frequency of items

```{r}
itemFrequencyPlot(groceries, support = 0.1)
itemFrequencyPlot(groceries, topN = 20)
```

A visualization of the sparse matrix for the first five transactions.

```{r}
image(groceries[1:5])
```

Visualization of a random sample of 100 transactions.

```{r}
image(sample(groceries, 100))
```

## Step 3: Training a model on the data

Default settings result in zero rules learned.  See that no rules are produced.

```{r}
apriori(groceries)
```

Set better support and confidence levels to learn more rules.

```{r}
groceryrules <- apriori(groceries, parameter = list(support =
                          0.006, confidence = 0.25, minlen = 2))
groceryrules
```

## Step 4: Evaluating model performance

Summary of grocery association rules.

```{r}
summary(groceryrules)
```

Look at the first three rules.

```{r}
inspect(groceryrules[1:10])
```

Sort rules by support.

```{r}
top.support <- sort(groceryrules, decreasing = TRUE, na.last = NA, by = "support")
inspect(head(top.support, 10))
```

Sort rules by confidence.

```{r}
top.confidence <- sort(groceryrules, decreasing = TRUE, na.last = NA, by = "confidence")
inspect(head(top.confidence, 10))
```

Sort rules by lift.

```{r}
top.lift <- sort(groceryrules, decreasing = TRUE, na.last = NA, by = "lift")
inspect(head(top.lift, 10))
```

With a data.table

```{r eval=FALSE}
inspectDT(groceryrules)
```

save table as a html page.

```{r}
p <- inspectDT(groceryrules)

htmlwidgets::saveWidget(p, "arules.html", selfcontained = FALSE)

browseURL("arules.html")
```

Read about the arulesViz package [arulesViz](https://cran.r-project.org/web/packages/arulesViz/vignettes/arulesViz.pdf).

Plot support and confidence and support and lift.

```{r}
plot(groceryrules)

plot(groceryrules, measure = c("support", "lift"), shading = "confidence")

```

```{r}
plot(groceryrules, method = "two-key plot")
```

```{r}
subrules <- groceryrules[quality(groceryrules)$confidence > 0.5]

plot(subrules, method = "matrix", measure = "lift")

plot(subrules, method = "matrix3D", measure = "lift")
```

```{r}
plot(groceryrules, method = "grouped")
```

Network plot and Parallel Coodinates plot.

```{r}
subrules2 <- head(groceryrules, n = 50, by = "lift")

plot(subrules2, method = "graph")

plot(subrules2, method = "paracoord")
```



```{r}
oneRule <- sample(groceryrules, 1)

inspect(oneRule)

plot(oneRule, method = "doubledecker", data = groceries)
```

## Step 5: Improving model performance

Sorting grocery rules by lift.

```{r}
inspect(sort(groceryrules, by = "lift")[1:5])
```

Finding subsets of rules containing any berry items.

```{r}
berryrules <- subset(groceryrules, items %in% "berries")

inspect(berryrules)
```


```{r}
plot(berryrules, method = "graph")

plot(berryrules, method = "paracoord")
```

Writing the rules to a CSV file.

```{r}
write(groceryrules, file = "groceryrules.csv",
      sep = ",", quote = TRUE, row.names = FALSE)
```

Converting the rule set to a data frame.

```{r}
groceryrules_df <- as(groceryrules, "data.frame")

groceryrules_df

str(groceryrules_df)
```






