如何在R中将高斯拟合为直方图? [英] How to fit a gaussian to a histogram in R?
问题描述
可能重复:
在R中将密度曲线拟合为直方图
我有一组数据,当在直方图中绘制时,它们是高斯分布。我已经从.csv文件中读取了数据,如下所示:
I have a set of data that when plotted in a histogram is a Gaussian distribution. I have read the data in from a .csv file that looks like this:
"values"
1.29989
1.15652
1.27818
1.19699
1.28243
1.19433
1.10991
...
使用 data< -read.csv(〜/ peak.csv)
...因此我创建了一个使用 hist(data $ values)
的直方图。
我希望能够使高斯拟合此直方图,并将拟合函数的sigma和均值与根据数据计算出的sigma和均值进行比较。
using data<-read.csv("~/peak.csv")
... so I create a histogram using hist(data$values)
.
I want to be able to fit a Gaussian to this histogram and compare the fitted function's sigma and mean to the sigma and mean calculated from the data.
我所看到的每个地方都提到 nls
和 glm
但是即使执行?nls
和?glm $ c $之后,我仍无法弄清楚如何使用这些函数使高斯拟合直方图c>。
Everywhere that I have looked mentions nls
and glm
but I cannot figure out how to use these functions to fit a Gaussian to a histogram, even after doing ?nls
and ?glm
. Please help?
推荐答案
如果要使用内置函数,可以这样:
If you want to use the built in functions you can do it like this:
library("MASS")
data<-read.csv("~/peak.csv")
fitdistr(data, "normal")
请记住,对于高斯来说,这只是在计算均值和标准偏差。
Just keep in mind that for a gaussian this is just calculating the mean and standard deviation.
这篇关于如何在R中将高斯拟合为直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!