Lecture 21 EM in R
This is the R session for week 7. In lecture 19 the EM algorithm was introduced and an ascent property was proved. In example 19.4 the E and M steps were illustrated for a two-component normal mixture. Today we will type the two steps into R functions, check the functions against the hand computation from the example, and then run the functions on a simulated sample of size . We will conclude by considering the problem of label switching and the effect of a bad start. The written reference for this session is appendix B.
21.1 The E and M Steps as R Functions
Throughout, the parameter is the vector and the common standard deviation is known. The E step of example 19.4 computes the responsibilities , the M step computes the updates (19.2), and both involve only vector arithmetic. The function dnorm is also used to compute the normal density. We also need to compute the observed-data log-likelihood . By theorem 19.2, this must increase.
resp <- function(x, theta, sigma) { a <- theta[1] * dnorm(x, mean = theta[2], sd = sigma) b <- (1 - theta[1]) * dnorm(x, mean = theta[3], sd = sigma) a / (a + b)}mstep <- function(x, gamma) { c(mean(gamma), sum(gamma * x) / sum(gamma), sum((1 - gamma) * x) / sum(1 - gamma))}loglik <- function(x, theta, sigma) { sum(log(theta[1] * dnorm(x, mean = theta[2], sd = sigma) + (1 - theta[1]) * dnorm(x, mean = theta[3], sd = sigma)))}We first test our function on the data from example 19.4. For this we use the five observations where and initial value . The results of one iteration of the algorithm are then compared to the responsibilities and iterate and to the two values for the log-likelihood computed by hand.
Everything agrees with the hand computation ( differs in the last digit because the example rounded the intermediate sums), and the last line is the check for exercise 19.3. We now iterate, keeping the parameter and the log-likelihood after each step in a matrix. Before you run the loop, predict the log-likelihood after the second iteration: far above , slightly above it, or equal to it?
The second iteration gains only , and after it nothing moves: the responsibilities are so close to and that each mean is the mean of its own group, and the iteration has reached its fixed point, a root of the likelihood equation by proposition 19.3.
21.2 A Larger Sample
Convergence in two steps is a consequence of the large difference between the two groups. Here we simulate a sample where the groups overlap. We can simulate such a sample by first randomly choosing a group, using rbinom to get with probability . We then simulate the measurements from the normal distribution corresponding to the group, i.e. with means and and . The command ifelse can be used to choose the correct mean for each observation. The box below contains the complete script for simulation and EM iteration, with lines shuffled (except for the body of the loop). Before you can run the script, you have to arrange these lines into a working script. The loop finishes when the log-likelihood increases by less than , as described in section 19.5. The vector ll stores the log-likelihood after each iteration, with the initial value at position .
theta <- c(0.5, q[1], q[2])for (k in 1:1000) { theta <- mstep(x, resp(x, theta, 1)) ll[k + 1] <- loglik(x, theta, 1) if (ll[k + 1] - ll[k] < 1e-8) break}z <- rbinom(n, size = 1, prob = 0.3)n <- 200q <- quantile(x, c(0.25, 0.75), names = FALSE)set.seed(2026)ll <- loglik(x, theta, 1)x <- rnorm(n, mean = ifelse(z == 1, 0, 3), sd = 1)The rule is the one from lecture 9: each line must come after all lines on which it depends, so the order of lines must be seed, n, z and x, quartiles q, start value, first entry of ll, and then the loop. The start values are chosen such that the weight is at and the two means are at the two quartiles. This is a cheap way to split the sample into two groups. Before you try the script, try to answer the following two questions: How many iterations will the loop take? (In the hand example from lecture, two iterations were needed.) Will the values for the log-likelihood increase by the same amount in each iteration, or differently?
The fixed point recovers the true group-1 proportion and the two means to within about two tenths. The loop required iterations, because the responsibilities of the two means were far away from and and because in each iteration the means were only slightly changed. The recorded values are increasing with each iteration, as theorem 19.2 guarantees, but at decreasing rates: , then , , , , as predicted by the geometric convergence of section 19.5. By proposition 19.3 the fixed point is a root of the likelihood equation. To test this, we can use optim, as shown in lecture 18, by maximising loglik with the same starting values as we used in the EM algorithm. If we use the command hessian = TRUE, we can get the standard errors from section 17.3. (The EM algorithm does not provide the standard errors.)
The two methods agree to four decimal places, and the standard errors say that the fitted weight is , with the smaller group’s mean determined only half as well as the larger group’s. At the fixed point the responsibility of group 1 is a decreasing function of which equals where . Before you look at figure 21.1, predict where the crossing lies: at the midpoint of the two fitted means, or above or below this point?

The crossing happens at , well below the mid-point , since the weights are unequal: an observation which is half-way between the means has three times the probability of being from the larger group. The filled and open circles give the true group of each observation, which cannot be known by EM: the responsibilities agree with the truth when the observations are far away from the crossing point, but between and both types of observations can be found and the group cannot be decided. The top panel is also plotted using the script R/S21-mixture.R; the three functions are given in R/S21-hand.R.
21.3 Label Switching and a Bad Start
Section 19.5 lists two ways in which a start can lead EM astray; both cases are represented in the sample. The script R/S21-starts.R encapsulates the loop as a function em and then runs this function with four different starts: the quartile start, the start with the two means swapped, the start with both means equal, and the start with nearly equal means. Before you look at the table, predict the outcome of each row. Where does the start with the swapped means end up? What is the log-likelihood there? What happens to the responsibilities when and thus to the next update?
The start with swapped means ends in the mirror image after the same iterations and with the same log-likelihood: both parameter values lead to the same density, the model is not identifiable in the sense of definition 4.10 and the choice of labelling in the start determines which of the two maxima we reach. The start with equal parameters is the trap. In this case, with , the two normal densities in the responsibility cancel, all are equal to and the M step sets both means equal to the sample mean while leaving the weight unchanged; another step does not change anything and the loop stops after two iterations at , effectively a single normal distribution, with a log-likelihood below the maximum. Theorem 19.2 is not violated: the log-likelihood did not decrease but failed to increase. The resulting resting point is a root of the likelihood equation, by proposition 19.3, but a saddle point instead of a maximum and thus EM will not be able to distinguish between the two. The nearly equal start shows that the trap is narrow: the iteration moves away from the saddle point and then climbs to the maximum, in iterations, as shown in figure 21.2 for the three traces.

For this model, with known common variance, a grid of hundreds of starting values finds only the two mirror-image maxima and the saddle line as stable solutions. In contrast, if the variances were unknown or if there were three or more components, additional local maxima would occur and the procedure from section 19.5 of starting the EM algorithm with several runs and then choosing the run with the highest observed-data log-likelihood becomes essential. For a two-component mixture, the twelve lines of resp, mstep and loglik are all we need. For serious work there are packages: mclust fits mixtures of this kind, and requireNamespace("mclust", quietly = TRUE) reports whether it is installed. Nothing in this session relies on it.
21.4 A Task to Take Home
The following task is in the style of a practical report. The published scripts of this lecture can be used as a starting point. Using your student ID as the seed, choose a weight between and , and choose two means which are two to three units apart. Simulate one sample of size from the mixture with . Run the EM algorithm with three different starting values (the quartile start and two of your choice) and show the fixed point, log-likelihood and number of iterations for each run in a table. Justify which run you would keep and mention if label switching occurred. Finally, use optim with hessian = TRUE to estimate the standard errors for the kept run. For each parameter, show the estimate, the standard error and the interval from corollary 17.3, in three sentences. For each sentence, state whether the interval contains the value you chose. Plot the responsibilities against the data. Write a short paragraph about how many observations have responsibility values between and . Comment on how well-separated the two groups are in your plot. Finally, repeat the whole exercise with the two means only one unit apart, and say in one sentence what changes.
Typing the commands yourself is an important step towards understanding how the R scripts work. Before the next session, please type in the commands from this session, and check that you get the same results as shown in the text.
-
•
The written reference for the R code in this session is appendix B.
-
•
The E-step and M-step from example 19.4 involve only a few lines of vector arithmetic. For five observations, these lines can reproduce the manual computation, and the algorithm converges to the fixed point in two steps.
-
•
For a sample of size with overlapping groups, it takes iterations for EM to converge. The log-likelihood increases in every step, by increasingly small amounts as required by theorem 19.2. The optim function finds the same fixed point as the manual calculation, as predicted by proposition 19.3, and optim can be used to compute the standard errors.
-
•
The responsibility of group 1 is a decreasing S-shaped function of . The function crosses below the mid-point of the means when group 1 is the smaller group. Close to the crossing point it is no longer possible to decide which group a sample belongs to.
-
•
If the labels are swapped before starting the EM algorithm, the algorithm converges to the mirror-image maximum with the same log-likelihood. If the two groups have the same mean, the EM algorithm converges to a saddle point immediately, i.e. a root of the likelihood equation which is not a maximum. To avoid this, the EM algorithm should be started with different values and the best of the resulting runs should be chosen, based on the log-likelihood of the observed data.