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

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

问题描述

我使用此绘制了两条密度曲线:

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)

我想针对给定的平均位置范围计算每条曲线下的面积.法线曲线有点像范数.有任何想法吗?

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函数,但是也可以使用基本程序包来完成此操作.

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天全站免登陆