[R] "Help On optim"

niharika singhal niharikasinghal1990 at gmail.com
Thu Aug 10 11:54:16 CEST 2017


Hello,



I have some parameters from Mclust function. The parameters are in the form



*parametersDf *



*      mu_1           mu_2          var_mc1    var_mc2        c1
              c2    *

*2   1.357283   2.962736       0.466154    0.1320129      0.5258975
 0.4741025  *

*21  8.357283   9.962736      0.466154    0.1320129      0.5258975
 0.4741025  *



Each row in the above data frame represent the segment and each segment
contain two-mixture component. (Latter I will increase the segment and the
mixture component)



So for every segment with n mixture component I have to find the



*max f(x) with –infinity < x < infinity*



*f(x)=sum_i c_i N(O,mu_i,sigma_i).  *



*Since i need to calculate the derivate f’ and set f’(x)=0  I thought of
using Newton method *



*In R the function I selected for my problem is optim.*



*Below is my code  *





*l = 2 # represent the number of mixture component*

*i=1# represent the segment 1 if i=2 represent the 2 segment*

*parametersDf #which I have posted above*

*xnorm= function(x)*

*{ *



*  #observation= append(segment1, segment2)*

*  mu.vector = parametersDf[1:l] *

*  var.vector =parametersDf[(l+1):((2*l))]*

*  coeff.vector=parametersDf[(2*l+1):(3*l)]*

*  val_df=data.frame()*

*  sum_prob=0*

*  for (j in 1:l){*

*
val=(coeff.vector[i,j]*(dnorm(x,mu.vector[i,j],sqrt(var.vector[i,j]))))*

*    sum_prob=sum_prob+val*

*  } *

*  return(sum_prob)*

*}*

*#segment1*



*val1=optim(parametersDf$mean_mc_1[1],**  fn=xnorm,*

*           method = "BFGS")*



*I am not getting right result from optim function. Is there something I
can do?*


*Thanks in Advance for the help *

	[[alternative HTML version deleted]]



More information about the R-help mailing list