计算连续密度图下的面积 [英] Calculating an area under a continuous density plot

查看:39
本文介绍了计算连续密度图下的面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个绘制了两条密度曲线:

I have two density curves plotted using this:

Network <- Mydf$Networks
quartiles <-  quantile(Mydf$Avg.Position,  probs=c(25,50,75)/100)
density <- ggplot(Mydf, aes(x = Avg.Position, fill = Network))
d <- density + geom_density(alpha = 0.2) + xlim(1,11) + opts(title = "September 2010") + geom_vline(xintercept = quartiles, colour = "red")
print(d)

我想计算给定 Avg.Position 范围内每条曲线下的面积.有点像正态曲线的 pnorm.有任何想法吗?

I'd like to compute the area under each curve for a given Avg.Position range. Sort of like pnorm for the normal curve. Any ideas?

推荐答案

单独计算密度并绘制一个开始.然后你可以使用基本的算术来得到估计.通过将一组小方块的面积相加来近似积分.我为此使用均值方法.长度是两个 x 值之间的差值,高度是间隔开始和结束时 y 值的平均值.我使用了 zoo 包中的 rollmeans 函数,但这也可以使用 base 包来完成.

Calculate the density seperately and plot that one to start with. Then you can use basic arithmetics to get the estimate. An integration is approximated by adding together the area of a set of little squares. I use the mean method for that. the length is the difference between two x-values, the height is the mean of the y-value at the begin and at the end of the interval. I use the rollmeans function in the zoo package, but this can be done using the base package too.

require(zoo)

X <- rnorm(100)
# calculate the density and check the plot
Y <- density(X) # see ?density for parameters
plot(Y$x,Y$y, type="l") #can use ggplot for this too
# set an Avg.position value
Avg.pos <- 1

# construct lengths and heights
xt <- diff(Y$x[Y$x<Avg.pos])
yt <- rollmean(Y$y[Y$x<Avg.pos],2)
# This gives you the area
sum(xt*yt)

这为您提供了十进制符号后面最多 3 位数字的良好近似值.如果你知道密度函数,看看?integrate

This gives you a good approximation up to 3 digits behind the decimal sign. If you know the density function, take a look at ?integrate

这篇关于计算连续密度图下的面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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