R/qfun.R

#' qfun
#'
#' \code{qfun} is the Q function in E-step (with negative sign).
#'
#' @export
#' @param pars a vector of length 45. c(alpha, beta, trans)
#' @param X    a list of vectors of observed states x
#' @param E    a vector of normalizing constant for each observed chain in X
#' @param L    a list of matrix L from \code{computeL}
#' @param H    a list of matrix H from \code{computeH}
#' @return     A scalar, the (negative) value of the target function
#'                that would later be minimized.
#'
#' @examples
#' df <- uORF
#' X <- L <- H <- list()
#' E <- c()
#' for (i in 1:2){
#'   X[[i]] <- df[[i]]$x
#'   RNA <- df[[i]]$RNA
#'   E[i]=df[[i]]$E;   trans=df[[i]]$trans;
#'   a=df[[i]]$v;      b=df[[i]]$v/df[[i]]$m
#'   la <- forwardAlg(X[[i]], RNA, trans, a, b, E[i])
#'   lb <- backwardAlg(X[[i]], RNA, trans, a, b, E[i])
#'   L[[i]] <- computeL(la, lb)
#'   H[[i]] <- computeH(X[[i]], RNA, trans, a, b, E[i], la, lb)
#' }
#' pars <- c(df[[1]]$v, df[[1]]$v/df[[1]]$m, df[[1]]$trans)
#'
#' qfun(pars,X,E,L,H)

qfun <- function(pars, X, E, L, H){
  rho_u <- pars[43]; rho <- pars[44]; delta <- pars[45]
  logA <- log(c(1-rho_u-rho, rho_u, rho, 1-delta, delta))
  Q = 0
  for (i in 1:length(X)){
      Q <- Q + sum(colSums(H[[i]])*logA)
  }
  Q <- Q - qemiss(pars[1:42],X, E, L) # since qemiss() returns with negative sign
  return(-Q)
}
shimlab/riboHMM2 documentation built on May 19, 2019, 6:23 p.m.