title: Implementing Gaussian mixture MLE using optim() in R
body: <p>I am trying to implement MLE for Gaussian mixtures in R using optim() using R's local datasets (Geyser from MASS). My code is below. The issue is that optim works fine however, returns the original parameters that I pass to it and also says that it has converged. I would appreciate if you can point out where I am going off track. My expectation is that it would at least yield different results 
  if not wildly different. </p>
  
  <pre><code>library(ggplot2)
  library(MASS)
  data("geyser")
  externaldata=geyser$waiting
  x.vector=externaldata
  
  
  MLE.x= function(combined.vector)
  { combined.vector=bigvec
    x.vector = externaldata
    K = k #capital K inside this MLE function, small K defined in the global environment
    prob.vector = combined.vector[1:K] 
    mu.vector =combined.vector[(K+1):((2*K))]
    sd.vector=combined.vector[(2*K+1):(3*K)]
    prob=matrix(rep(prob.vector,length(x.vector)),byrow=TRUE,nrow = length(x.vector))
    mu.sigma=cbind(mu.vector,sd.vector)
    x.by.K=matrix(nrow = length(x.vector), ncol = k)
    for (i in 1:K){
      x.by.K[,i]=dnorm(x.vector,mu.sigma[i,1],mu.sigma[i,2])
    }
    prob.mat=x.by.K*prob
    density=apply(prob.mat,1,sum)
    log.density=sum(-log(density))
    return(log.density)
  }
  
  
  
  ## k=2 set ##
  meanvec=c(50,80)
  sigmavec=c(5,5)
  k=2
  probvec=c(1/3,2/3)
  bigvec=c(probvec,meanvec,sigmavec)
  est.k2.MLE=MLE.x(bigvec)
  z=optim(bigvec,
          fn=MLE.x,
          method = "L-BFGS-B")
  z
  
  
  #### k=3 set #####
  meanvec=c(50,70,80)
  sigmavec=c(5,5,5)
  k=3
  probvec=rep(1/3,3)
  bigvec=c(probvec,meanvec,sigmavec)
  est.k3.MLE=MLE.x(bigvec)
  z=optim(bigvec,
          fn=MLE.x,
          method = "BFGS")
  z
  </code></pre>