Tag Archives: R

Market Basket Analysis in R

Introduction

Hundreds and thousands of transactions occur every day in a supermarket, while a customer would buy multiple products in each transaction. For example, it may look like this in the database: {Transaction1: Product1, Product3, Product4, Product8, Product9}. In a larget data set of transactions, purchase patterns, i.e. some products are always bought together, can be examined based on product association analysis, also called Market Basket Analysis.

Key Concepts of Market Basket Analysis

The basics of market basket analysis
The basics of market basket analysis, source: UofT

Basically, there are four key concepts of Market Basket Analysis:

  • Rule: a rule expresses the incidence across transactions of one set of items as a condition of another set of items, i.e. X => Y;
  • Support: the support for a set of items is the proportion of all transactions that contain the set;
  • Confidence: the support for the co-occurrence of all items in a rule, conditional on the support for the left-hand set alone;
  • Lift: the support of a set conditional on the joint support of each element;
Continue reading

R Basic Takeaways

While learning R, I was always thinking to make some notes and some of the concepts really took me more time than what I expected. That is why I am planning to make a Cheat Sheet for R so that I can always come back to check the key points instead of searching online every time. Therefore, I will first make a summary, in this post, of the key points from the books I read and update it when necessary. Hopefully, I can make an R Cheat Sheet based on this in the end.

Asking for help

Asking for help in R

Inspecting Data

Continue reading

Transportation and Assignment problems with R

In the previous post “Linear Programming with R” we examined the approach to solve general linear programming problems with “Rglpk” and “lpSolve” packages. Today, let’s explore “lpSolve” package in depth with two specific problems of linear programming: transportation and assignment.

1. Transportation problem

Transportation problem, Frederick & Mark (2014, p.95)

Usage:

lp.transport(cost.mat, direction="min", row.signs, row.rhs, col.signs,
               col.rhs, presolve=0, compute.sens=0, integers = 1:(nc*nr))
Continue reading

Quadratic Programming with R

Optimization of a quadratic function

Quadratic programming problems can be solved with “quadprog” package in R and the key point is to find the matrix notation of quadratic programming problems:

Example

Let’s figure out how to do it with an example of “Applying Nonlinear Programming to Portfolio Selection”:

Please note that, this example involves three variables (x1, x2, and x3). If you want an example of two variables (x1 and x2), please check my another post: Another Quadratic Programming Example with R.

Continue reading

Maximum Flow Problem with R

Maximum flow is one of the Network Optimization problems in Operations Research and can be solved in R by “igraph“, which is a network analysis package.

6.2 A CASE STUDY: THE BMZ CO. MAXIMUM FLOW PROBLEM

The BMZ Co. distribution network from its main factory in Stuttgart, Germany, to a distribution center in Los Angeles.

The function to solve maximum flow problem in igraph package is called max_flow.

Usage:

max_flow(graph, source, target, capacity = NULL)
Continue reading