Skip to content

Instantly share code, notes, and snippets.

@cheuerde
Last active September 8, 2015 16:04
Show Gist options
  • Select an option

  • Save cheuerde/8dd37d3701799e1121e2 to your computer and use it in GitHub Desktop.

Select an option

Save cheuerde/8dd37d3701799e1121e2 to your computer and use it in GitHub Desktop.
Mixture models in R using mixtools
# Claas Heuer, September 2015
#
# Extremely good vignette for mixtools: https://cran.r-project.org/web/packages/mixtools/vignettes/mixtools.pdf
library(mixtools)
library(ks)
########################
### Gaussian Mixture ###
########################
# some random data
set.seed(100)
N <- 1000
theta <- 0.3
mu1 <- -10
mu2 <- 10
sd1 <- 1
sd2 <- 20
y <- ifelse(runif(N) < theta, rnorm(N, mu1, sd1), rnorm(N, mu2, sd2))
dat = list(N=length(y),y=y, k=2)
# run the model
mod <- normalmixEM(y)
# plot data and estimated densities
plot(mod, which = 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment