Skip to content

Instantly share code, notes, and snippets.

@KyleOng
Created November 24, 2020 08:13
Show Gist options
  • Save KyleOng/7e0788185c224efb8c07476b462bf406 to your computer and use it in GitHub Desktop.
Save KyleOng/7e0788185c224efb8c07476b462bf406 to your computer and use it in GitHub Desktop.
Gaussian Mixture plot in R
library(ggplot2)
gg.mixEM <- function(EM) {
require(ggplot2)
x <- with(EM,seq(min(x)-1,max(x)+1,len=1000))
pars <- with(EM,data.frame(comp=colnames(posterior), mu, sigma,lambda))
em.df <- data.frame(x=rep(x,each=nrow(pars)),pars)
em.df$y <- with(em.df,lambda*dnorm(x,mean=mu,sd=sigma))
ggplot(data.frame(x=EM$x),aes(x,y=..density..)) +
geom_histogram(fill=NA,color="black",bins=41)+
geom_polygon(data=em.df,aes(x,y,fill=comp),color="grey50", alpha=0.5)+
scale_fill_discrete("Component Means",labels=format(em.df$mu,digits=3))+
theme_bw()+
coord_cartesian(xlim = c(min(x)-1,max(x)),
expand = c(0,0))
}
# Examples
ores = mixtools::normalmixEM(resolution_time_second,
k = 2,
#mean.constr = NULL,
#sd.constr = NULL,
#epsilon = 1e-15,
#maxit = 1000,
#maxrestarts=50,
#verb = TRUE,
fast=FALSE,
#ECM = FALSE,
#arbmean = TRUE,
#arbvar = TRUE
)
summary(ores)
gg.mixEM(ores)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment