em1.hmm<-function(x, maxiter=200) { ##################################################################################### ## USAGE # em.hmm(x, L) ## ARGUMENTS # x: the observed data # L: num. of components in the non-null mixture # maxiter: the maximum number of iterations ## DETAILS # em.hmm calculates the MLE for a HMM model with hidden states being 0/1. # the distribution of state 0 is assumed to be normal # the distribution of state 1 is assumed to be a normal mixture with L components ## VALUES # fuction 'em.hmm' gives the MLE of model parameters and Lfdr estimates for a HMM # pii: the initial state distribution # A=(a00 a01\\ a10 a11): transition matrix # f0: the null distribution # pc, i from 1 to L: # --probability weights of each component in the non-null mixture # f1: an L by 2 matrix # --specifying the dist. of each component in the non-null mixture # lfdr: the lfdr variables # niter: number of iterations #################################################################################### NUM<-length(x) # precision tolerance level ptol<-1e-4 niter<-0 ### initializing model parameters pii.new<-c(1, 0) A.new<-matrix(c(0.8, 0.2, 0.4, 0.6), 2, 2, byrow=T) f0<-c(0, 1) f1.new<-c(2, 1) diff<-1 ### The E-M Algorithm while(diff>ptol && niter