How to fit a gaussian to a histogram in R? [duplicate]

家住魔仙堡 提交于 2019-11-28 11:01:32

问题


Possible Duplicate:
Fitting a density curve to a histogram in R
best fitting curve from plot in R

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
...

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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/14511454/how-to-fit-a-gaussian-to-a-histogram-in-r

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