R superimposing bivariate normal density (ellipses) on scatter plot

徘徊边缘 提交于 2019-12-13 05:49:40

问题


There are similar questions on the website, but I could not find an answer to this seemingly very simple problem. I fit a mixture of two gaussians on the Old Faithful Dataset:

if(!require("mixtools")) { install.packages("mixtools");  require("mixtools") }
data_f <- faithful
plot(data_f$waiting, data_f$eruptions)
data_f.k2 = mvnormalmixEM(as.matrix(data_f), k=2, maxit=100, epsilon=0.01) 
data_f.k2$mu # estimated mean coordinates for the 2 multivariate Gaussians
data_f.k2$sigma # estimated covariance matrix 

I simply want to super-impose two ellipses for the two Gaussian components of the model described by the mean vectors data_f.k2$mu and the covariance matrices data_f.k2$sigma. To get something like:

For those interested, here is the MatLab solution that created the plot above.


回答1:


You can use the ellipse-function from package mixtools. The initial problem was that this function swaps x and y from your plot. I'll try to figure this out and update the answe. (I'll leave the colors to somebody else...)

plot( data_f$eruptions,data_f$waiting)
for (i in 1: length(data_f.k2$mu))  ellipse(data_f.k2$mu[[i]],data_f.k2$sigma[[i]])



回答2:


If you are interested in the colors as well, you can use the posterior to get the appropriate groups. I did it with ggplot2, but first I show the colored solution using @Julian's code.

# group data for coloring
data_f$group <- factor(apply(data_f.k2$posterior, 1, which.max))
# plotting
plot(data_f$eruptions, data_f$waiting, col = data_f$group)
for (i in 1: length(data_f.k2$mu))  ellipse(data_f.k2$mu[[i]],data_f.k2$sigma[[i]], col=i)

And for my version using ggplot2.

# needs ggplot2 package
require("ggplot2")
# ellipsis data 
ell <- cbind(data.frame(group=factor(rep(1:length(data_f.k2$mu), each=250))), 
             do.call(rbind, mapply(ellipse, data_f.k2$mu, data_f.k2$sigma, 
                                   npoints=250, SIMPLIFY=FALSE)))

# plotting command
p <- ggplot(data_f, aes(color=group)) + 
  geom_point(aes(waiting, eruptions)) +
  geom_path(data=ell, aes(x=`2`, y=`1`)) +
  theme_bw(base_size=16)
print(p)



回答3:


Using mixtools internal plotting function:

plot.mixEM(data_f.k2, whichplots=2)



来源:https://stackoverflow.com/questions/28317203/r-superimposing-bivariate-normal-density-ellipses-on-scatter-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!