[R] Estimating a Normal Mixture Distribution

rolf at math.unb.ca rolf at math.unb.ca
Sat Apr 21 00:26:57 CEST 2007


andreas.danis at univie.ac.at wrote:

        <snip>

> mix.gr<-function(p,x){
> 	p<-p[1]
> 	u1<-p[2]
> 	s1<-p[3]
> 	u2<-p[4]
> 	s2<-p[5]
> 	colSums(attr(lmix2a(x,p,u1,s1,u2,s2),"gradient"))}
> 
          <snip>

The problem is with the position of ``p <- p[1]''.
Afterwards ``p'' is a scalar and so p[2] etc. are NA.
As a result the gradient that is calculated is all NAs.

If you change the code for mix.gr to

mix.gr<-function(p,x){
	u1<-p[2]
	s1<-p[3]
	u2<-p[4]
	s2<-p[5]
	p<-p[1]
	colSums(attr(lmix2a(x,p,u1,s1,u2,s2),"gradient"))}

then your procedure seems to work --- at least it gives an answer
different from the starting values.

                                cheers,

                                        Rolf Turner
                                        rolf at math.unb.ca



More information about the R-help mailing list