R在散点图上叠加二元正态密度(椭圆) [英] R superimposing bivariate normal density (ellipses) on scatter plot
问题描述
网站上也有类似的问题,但是我找不到这个看似非常简单的问题的答案.我在旧的忠实数据集上混合了两个高斯:
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
我只是想为均值向量data_f.k2$mu
和协方差矩阵data_f.k2$sigma
所描述的模型的两个高斯分量叠加两个椭圆.要获得类似的东西:
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:
对于那些感兴趣的人,这里是创建上面图的MatLab解决方案.
For those interested, here is the MatLab solution that created the plot above.
推荐答案
您可以使用软件包mixtools
中的ellipse
功能.最初的问题是该函数从绘图中交换x和y.我将设法弄清楚这一点并更新answe. (我将颜色留给其他人...)
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]])
这篇关于R在散点图上叠加二元正态密度(椭圆)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!