标记密度图

Labeling a density plot

在下图中,我如何才能只沿 x 轴打印标准差值?

R 打印 5、7、10、13、15、19、20、22、25。

x <- rnorm(3e3, 16, 3)
plot(density(x), type = "l", lwd = 2, col = 254,
main = "Random Variable X", xact = "n")
axis(side = 1, at = c(7, 10, 13, 19, 22, 25),
labels = c("7","10","13","19", "22", "25"))
abline(v = 16, lwd = 2)
abline(v = c(7, 10, 13, 19, 22, 25), col = "darkgreen")

谢谢..

像这样:

x <- rnorm(3e3, 16, 3);
plot(
    density(x), 
    type = "l", lwd = 2, col = 254,
    main = "Random Variable X", xaxt = "n");
axis(
    side = 1, 
    at = mean(x) + seq(-3, 3, by = 1) * sd(x),
    labels = seq(-3, 3, by = 1));

x 轴显示区域 [-3σ, +3σ] 中的 sd 单位。

这是另一种方法。

相同的另一个变体是删除 x <- scale(x) 并具有 y <- dnorm(x, mean(x), sd(x))。我想很容易看出我做了什么。

此方法与使用 density(x) 的区别在于密度的估计方式。

如果您知道参数分布,在这种情况下是正态分布,并且可以估计最大似然估计,mean(x), sd(x),您不妨使用它们来估计密度。

set.seed(42)
rnorm(3e3, 16, 3)

# normalize N(0, 1)
x <- scale(x)
# order so plotting is easy
x <- x[order(x)]
# density 
y <- dnorm(x)
plot(x, y, type = "l", lwd = 2, col = 254, ylab = "density", 
    main = "Random Variable X")