直方图上的密度曲线是平坦的 [英] Density curve on histogram is flat

查看:63
本文介绍了直方图上的密度曲线是平坦的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一条曲线,该曲线遵循我的数据直方图的趋势,我环顾四周并尝试了其他人的代码,但仍然得到一条平线.这是我的代码

I am trying to plot a curve that follows the trend of the histogram of my data, I have looked around and have tried out other peoples code but I still get a flat line. Here is my code

hist(Ferr,xlab = "Ferritin Plasma Concentration", ylab = "Frequency", main = "Histogram of Ferritin 
Plasma Concentration", xlim = c(0,250), ylim = c(0,50), cex.axis=0.8, cex.lab=0.8,cex.main = 1)
curve(dnorm(x, mean = mean(Ferr), sd = sd(Ferr)), col="blue", add=TRUE)
lines(density(Ferr), col="red")

如果有人可以帮助我看看我哪里出了问题,那将非常感谢.

If anyone can help me to see where I have gone wrong, that would be great thank you.

推荐答案

与直方图不同,密度函数在整个空间上的积分等于1:

Unlike an histogram, the integral of a density function over the whole space is equal to 1 :

sum(density(x)*dx) = 1

要将密度函数缩放为直方图,可以将其乘以直方图bin的最大值,再除以点之间的距离.

To scale the density function to the histogram, you can multiply it by the maximum value of the histogram bins and divide it by the distance between points.

让我们以 mtcars $ mpg 为例:

Ferr <- mtcars$mpg
d <- density(Ferr)
dx <- diff(d$x)[1] 

sum(d$y)*dx
[1] 1.000851

h <- hist(Ferr)
lines(x=d$x,y=max(h$counts)*d$y/dx)

这篇关于直方图上的密度曲线是平坦的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆