给定一个简单的密度直方图和曲线,如下所示,如何检索给定X值的Y值.例如,y值在mean(dat)?
dat<-c(5,7,4,6,4,3,55,6,7,5,4,3,33,44,5,2,33,22)
hist (dat,freq=F)
lines(density(dat), col="red", lwd=2)
Run Code Online (Sandbox Code Playgroud)
谢谢.
您可以使用approxfun()结果density来获得近似密度的函数
dat <- c(5, 7, 4, 6, 4, 3, 55, 6, 7, 5, 4, 3, 33, 44, 5, 2, 33, 22)
hist(dat, freq=F)
lines(d<-density(dat), col="red", lwd=2)
#get density function
dd <- approxfun(d$x, d$y)
dd(mean(dat))
# [1] 0.015039
#plot results
abline(v=mean(dat), lty=2)
points(mean(dat), dd(mean(dat)), cex=1.2, pch=20, col="blue")
Run Code Online (Sandbox Code Playgroud)
