Get startedGet started for free

Binary images

An easy way to understand how distribution that relates to a data frame is to generate samples.

The size of the images are 2 by 2 pixels and the probability of being 1 for this cluster are 0.8, 0.8, 0.2 and 0.9 for the corresponding pixel.

This exercise is part of the course

Mixture Models in R

View Course

Exercise instructions

  • Generate 100 binary images from cluster one. Start by simulating each of the four pixels as a binary sample.
  • Have a look at the generated binary images.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create the vector of probabilities
p_cluster_1 <- c(0.8, 0.8, 0.2, 0.9)
# Create the sample for each pixel
pixel_1 <- sample(c(0, ___), ___, replace = TRUE, 
                  prob = c(1-p_cluster_1[___], p_cluster_1[___]))
pixel_2 <- sample(c(0, 1), 100, replace = TRUE, 
                  prob = c(1-p_cluster_1[___], p_cluster_1[___]))
pixel_3 <- sample(c(0, 1), 100, replace = TRUE, 
                  prob = c(1-p_cluster_1[___], p_cluster_1[___]))
pixel_4 <- sample(c(0, 1), 100, replace = TRUE, 
                  prob = c(1-p_cluster_1[___], p_cluster_1[___]))
# Combine the samples
sample_cluster_1 <- cbind(pixel_1, pixel_2, pixel_3, pixel_4)
# Have a look to the sample
head(___)
Edit and Run Code