简体   繁体   English

如何在R中拟合多元正态分布?

[英]How to fit a multivariate normal distribution in R?

I need to fit a multivaraite normal distribution to each specie in the Iris dataset in R. I saw the mvtnorm package might be useful;我需要将多元正态分布拟合到 R 中 Iris 数据集中的每个物种。我看到mvtnorm包可能有用; however, i want to use the maximum likelihood estimation and not sure how to do so in R. Any ideas?但是,我想使用最大似然估计,但不确定如何在 R 中使用。有什么想法吗?

If you just want to fit a distribution to each species, you might want mvnorm.mle in the Rfast package:如果你只是想适应的分配到每种,你可能想mvnorm.mleRfast包:

install.packages("Rfast")
library(Rfast)
iris.split <- split(iris[, 1:4], iris$Species)
iris.mvnorm <- lapply(iris.split, function(x) mvnorm.mle(as.matrix(x)))
iris.mvnorm[["setosa"]]
# $loglik
# [1] 44.91657
#
# $mu
# [1] 5.006 3.428 1.462 0.246
#
# $sigma
#              Sepal.Length Sepal.Width Petal.Length Petal.Width
# Sepal.Length     0.121764    0.097232     0.016028    0.010124
# Sepal.Width      0.097232    0.140816     0.011464    0.009112
# Petal.Length     0.016028    0.011464     0.029556    0.005948
# Petal.Width      0.010124    0.009112     0.005948    0.010884

The other species are stored in iris.mvnorm[["versicolor"]] and iris.mvnorm[["virginica"]] .其他物种存储在iris.mvnorm[["versicolor"]]iris.mvnorm[["virginica"]]

It seems you are looking for a mixture discriminant analysis (since class labels are known).您似乎正在寻找混合判别分析(因为类标签是已知的)。 In this case, you can use the MclustDA from the mclust package.在这种情况下,你可以使用MclustDAmclust包。

 model= MclustDA(data = iris[,1:4], class = iris$Species)
 summary(model)

However, if you are looking to cluster the data using by fitting multivariate Gaussian mixtures then you can use the Mclust function.但是,如果您希望通过拟合多元高斯混合来对数据进行聚类,那么您可以使用Mclust函数。

  fit = Mclust(data = iris[,1:4], G=3)
  table(fit$classification, iris$Species)

 #   setosa versicolor virginica
 # 1     50          0         0
 # 2      0         45         0
 # 3      0          5        50

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM