R/dnorm_mix.R

Defines functions dnorm_mix

Documented in dnorm_mix

dnorm_mix <- function(x, pi, mean, sd, log = FALSE){
  if(length(x) == 1){
    g = length(pi)
    pi = pi/sum(pi)
    if( min(c(pi, sd)) > 0 && length(mean) == g && length(sd) == g){
      aux = 0
      for(j in 1:g){aux = aux + pi[j]*dnorm(x,mean=mean[j], sd = sd[j])}
      if(!log){
        return(aux)
      }else{
        return(log(aux))
      }
    }else{
      stop("The parametric space must be respected.")
    }
  }else{
    h = function(x){dnorm_mix(x, pi, mean, sd, log)}
    return(sapply(x, h))
  }
}
matheuscastro43/finiteMix documentation built on March 30, 2022, 12:49 p.m.