[R] Problem in optimization of Gaussian Mixture model
niharika singhal
niharikasinghal1990 at gmail.com
Thu Aug 24 11:29:37 CEST 2017
Hello,
I am facing a problem with optimization in R from 2-3 weeks.
I have some Gaussian mixtures parameters and I want to find the maximum in
that
*Parameters are in the form *
mean1 mean2 mean3 sigma1 sigma2 sigma3 c1 c2 c3
506.8644 672.8448 829.902 61.02859 9.149168 74.84682 0.1241933
0.6329082 0.2428986
I have used optima and optimx to find the maxima, but it gives me value
near by the highest mean as an output, for example 830 in the above
parameters. The code for my optim function is
val1=*optim*(par=c(X=1000, *seg=1*),fn=xnorm, opt=c(NA,seg=1),
method="BFGS",lower=-Inf,upper=+Inf, control=list(fnscale=-1))
*I am running the optim function in a loop and for different initial value
and taking the val1$par[1] as the best value.*
*I am taking this parameter since I want to run it on n number of segments
latter*
and xnorm is simply calculating the dnorm
*xnorm*=function(param, opt = rep(NA, 2)){
if (any(!sapply(opt, is.na))) {
i = !sapply(opt, is.na)
# Fix non-NA values
param[i] <- opt[i]
}
xval= param[1]
seg <- param[2]
sum_prob=0
val=0
l=3
meanval=c(506.8644, 672.8448, 829.902)
sigmaval=c(61.02859, 9.149168, 74.84682)
coeffval(0.1241933, 0.6329082, 0.2428986)
for(n in 1 :l)
{
mu=meanval[seg,n]
sg=sigmaval[seg,n]
cval=coeffval[seg,n]
val=cval*(dnorm(xval,mu,sg))
#print(paste0("The dnorm value for x is.: ", val))
sum_prob=sum_prob+val
}
sum_prob
}
The output is not correct. Since I check my data using*
UnivarMixingDistribution* from distr package and according to this the max
should lie somewhere between 600-800
Code I used to check
mc0=c( 0.6329082,0.6329082,0.2428986)
rv
<-UnivarMixingDistribution(Norm(506.8644,61.02859),Norm(672.8448,9.149168),Norm(
829.902,74.84682), mixCoeff=mc0/sum(mc0))
plot(rv, to.draw.arg="d")
Can someone please help how I can solve this problem?
Thanks & Regards
Niharika Singhal
[[alternative HTML version deleted]]
More information about the R-help
mailing list