I'm currently working on parameter estimation for a mixture of two normal distributions. I have two parameters I am trying to estimate using the maximum likelihood method: the probability and the population mean. In the equation above, $f_1$ follows a normal distribution with mean 0 and standard deviation 1, while $f_2$ follows a normal distribution with mean $\mu$ and standard deviation 1.
I have already defined the log-likelihood function and also generated a vector $x$ of 100 observations setting $\pi$ equal to 0.25, and $\mu$ equal to 1. I would like to find a combination of these two parameters that generates the highest log-likelihood, but I am not sure how to perform an exhaustive search in two dimensions. (I will later implement an optimizing function, but I would like to perform a grid search first).
Here is the code I have thus far:
loglike = function(pi, mu, x) {
llh = NULL # initializing an empty list
for (i in x) {
f1 = dnorm(i, 0, 1)
f2 = dnorm(i, mu, 1)
llh = c(llh, log(pi*f1 + (1-pi)*f2)) # appending the values to the list
}
return(sum(llh))
}
set.seed(4185)
group = sample(c(1,2), 100, replace=TRUE, prob=c(.25,.75))
rn1 = rnorm(100,0,1)
rn2 = rnorm(100,1,1)
x = rn1*(group==1) + rn2*(group==2)
piSeq = seq(0, 1, .01)
muSeq = seq(min(x), max(x), .01)
Any help or advice is appreciated. Thanks!