Get startedGet started for free

Bernoulli Mixture Models with flexmix

1. Bernoulli Mixture Models with flexmix

In the previous lesson, we defined the framework for modeling the digits dataset with Bernoulli mixture models. In this lesson, we will estimate the model using `flexmix`.

2. The problem

Recall that we want to find two clusters that can characterize a sample of binary images representing the digits three and six. With this, we could be able to identify new observations as if they belong to cluster one or two.

3. The dataset

The dataset is stored in the data frame `digits`. In order to get the right format for the `flexmix()` function, we need to transform the dataset into a matrix. The dimension of this matrix corresponds to 320 rows or observations and 256 variables, which, recall, represent the pixels. For the purpose of making the visualization easier, here's a simple function `show_digit()` that takes the 256 columns and transforms them into an image, which I did here on the last observation.

4. Fit Bernoulli mixture models

To fit the Bernoulli mixture model with two clusters, we proceed as before. Observe that now we are using a matrix form to let the function know that every column is a different variable. We could have done the same for the bivariate Gaussian Mixture Model, but since we had just two columns we explicitly wrote them down. Now we have 256 columns. The Bernoulli distribution is represented by the function `FLXMCmvbinary` and all the rest remains the same.

5. The proportions

So, let's analyze the results starting with the proportions of each cluster. Using `prior()`, we see that both clusters are almost equally represented in the dataset.

6. parameters() function

The parameters for this model are represented by a probability vector for each cluster, where every element of this vector is the probability of getting a one, or a black pixel. For cluster one, for example, we see that the column vector has 256 elements as expected and from the first six values shown here, it is probable that the first six pixels for this cluster are ones or black.

7. Visualize the component 1

To visualize all the probabilities for each pixel in cluster one, we can make use of `show_digit()`. Recall that the probabilities belong to the range between 0 and 1, so the closer to one, the colour is closer to black. So, for example, if a random image belongs to this cluster, it is then probable that the black pixels from the image should be located in the coloured area shown here.

8. Visualize the component 2

For cluster two, we see a completely different pattern, so if a random image is identified as belonging to this cluster, it is probable that the image will look like the number six.

9. Let's practice!

Now that you have learned how to fit a Bernoulli mixture model using flexmix, let's put it into practice!