Show R code
source('irm.R')Jesse Mu
Last modified: 2016-11-02: 0:00:00 (UTC)
What happens when you want to cluster your data, but the number of clusters is unknown? While some approaches involve fitting several models with a varying number of clusters to your data and comparing model fit statistics, the Bayesian approach is to specify a model which is allowed to dynamically grow the number of clusters as the complexity of the data warrants.
The Infinite Relational Model is the prototypical example of such a model (Kemp et al. 2006).
For this last project, a simple version of Charles Kemp’s Infinite Relational Model (IRM) was coded in irm.R to co-cluster rows and columns of a simple 2-dimensional binary relation.
source('irm.R')As a sanity check, the toy matrix from the original paper is used:
Z = irm(R, sweeps = 1000)
top.n(Z)
#> 122121323 122123424 122121324 122121343 122321424 123121424 122131425 122324525
#> 913 23 14 13 12 10 4 4
#> 123121434 123131434
#> 3 2
plot.R(R, mode.irm(Z))This successfully finds the clusters of rows and columns that correspond to the original paper.
---
title: "Final Lecture: Infinite Relational Model"
author: "Jesse Mu"
date: "November 2, 2016"
format:
html: default
revealjs:
output-file: irm-slides.html
pdf:
output-file: irm-handout.pdf
docx:
output-file: irm-handout.docx
---
<!-- Setup -->
```{r echo=FALSE, message=FALSE}
knitr::opts_chunk$set(fig.align = 'center', message = FALSE)
library(knitr)
library(ggplot2)
library(cowplot)
library(tidyr)
library(dplyr)
```
<!-- Begin writing -->
# The Infinite Relational Model
What happens when you want to cluster your data, but the number of clusters is
unknown? While some approaches involve fitting several models with a varying
number of clusters to your data and comparing model fit statistics, the Bayesian
approach is to specify a model which is allowed to dynamically grow the number
of clusters as the complexity of the data warrants.
The **Infinite Relational Model** is the prototypical example of such a model [@kemp2006].
For this last project, a simple version of Charles Kemp's Infinite
Relational Model (IRM) was coded in `irm.R` to co-cluster rows and columns of a simple
2-dimensional binary relation.
## Demo
```{r}
source('irm.R')
```
### Sanity check
As a sanity check, the toy matrix from the original paper is used:
```{r}
R = rbind(
c(0, 0, 1, 0, 1, 0, 0, 1, 0),
c(0, 0, 0, 0, 0, 0, 1, 0, 1),
c(0, 0, 1, 0, 0, 0, 1, 0, 1),
c(0, 1, 1, 0, 0, 0, 0, 1, 1),
c(0, 0, 0, 0, 0, 0, 1, 0, 1),
c(0, 1, 1, 0, 1, 0, 0, 1, 0),
c(1, 0, 0, 0, 0, 1, 0, 0, 0),
c(0, 0, 0, 0, 0, 1, 1, 0, 1),
c(1, 0, 0, 1, 0, 1, 0, 0, 0)
)
plot.R(R)
```
```{r}
Z = irm(R, sweeps = 1000)
top.n(Z)
plot.R(R, mode.irm(Z))
```
This successfully finds the clusters of rows and columns that correspond to the original paper.
<!--
TODO: add 50 animals data
### 50 animals
-->