将线段添加到密度图
我想在图表中添加一条线,该行指示了平均值:
X1 <- rnorm(100)
# Kernel density estimates
density_X1 <- density(X1)
# Compute mode
mode_X1 <- density_X1$x[which.max(density_X1$y)]; mode_X1
# Compute mean
mean_X1 <- mean(X1)
# Create plot
plot(density_X1, main = "Kernel density estimates", xlab = "X1", ylab = "density")
# Add line for mode
segments(x0 = mode_X1, y0 = 0, x1 = mode_X1, y1 = density_X1$y, col = "red", lty = 1, lwd = 1)
# Add line for mean
segments(x0 = mean_X1, y0 = 0, x1 = mean_X1, y1 = , col = "red", lty = 5, lwd = 2)
我需要输入段(y1 =)
以确保上限是内核密度图的什么值?
I would like to add a line to my plot that indicates the mean:
X1 <- rnorm(100)
# Kernel density estimates
density_X1 <- density(X1)
# Compute mode
mode_X1 <- density_X1$x[which.max(density_X1$y)]; mode_X1
# Compute mean
mean_X1 <- mean(X1)
# Create plot
plot(density_X1, main = "Kernel density estimates", xlab = "X1", ylab = "density")
# Add line for mode
segments(x0 = mode_X1, y0 = 0, x1 = mode_X1, y1 = density_X1$y, col = "red", lty = 1, lwd = 1)
# Add line for mean
segments(x0 = mean_X1, y0 = 0, x1 = mean_X1, y1 = , col = "red", lty = 5, lwd = 2)
What value do I need to enter for segments(y1 = )
to ensure that the upper bound is the kernel density plot?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要从离散点的密度估计值以某个X值计算Y值,您将需要进行某种插值。一种方法是使用
大约
的线性插值:其中,
x
和y
给出要插值的点,xout 插值完成的X值的(向量)(请参阅
of code>
),因此代码完全是:
To calculate the y-value at some x-value from a density estimate at discrete points, you will need to do some sort of interpolation. One way is to use linear interpolation with
approx
:where,
x
andy
give the points to be interpolated, andxout
the (vector of) x-values where the interpolation is done (See?approx
)So altogether the code is:
您也可以使用
Abline
喜欢:output:
You could also use
abline
like this:Output: